cancel
Showing results for 
Search instead for 
Did you mean: 

external interrupt

XooM
Senior

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if (GPIO_Pin == GPIO_PIN_1) 
        {
    	 uint32_t current_time = HAL_GetTick();  
    	 	   if ((current_time - last_interrupt_time) > 500)
    	 	 	 {
    	 		 last_interrupt_time = current_time;  		 	 
                         Button++;  
    	 	 	 }
        }
}

 

 

  while (1)
  {

	  if (Button== 2) {  
	          Button= 0;    
	       	  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14,GPIO_PIN_SET)
	      }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

 

 

 

Hello. The code I wrote with external interrupt does not work.

If I press the button with intervals shorter than 500ms, it should not detect these button bounces..
If I press the watch button for more than 500ms, I want it to detect it.
Even if I press the watch button for more than 500ms, the value of the button variable does not increase.

 

If I just write the code below, the button variable increases, but because of the button bounces, even though I want it to increase by +1, it increases like +5 to +10.

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
       if (GPIO_Pin == GPIO_PIN_1) 
        {
    	
    	 Button++;
    	 	 	
        }
}

 

Why aren't my codes at the beginning of the message working?

14 REPLIES 14
XooM
Senior
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
       if (GPIO_Pin == GPIO_PIN_1) 
        {
    	
    	 Button++;
    	 	 	
        }
}

If I write only this code into EXTI then the Button variable increases uncontrollably.

You have no cap parallel to the button... still?

So don't ++ , it's just nonsense.

Except you want to count how many times it's bouncing. 

 

If you feel a post has answered your question, please click "Accept as Solution".

Is Button declared properly, with volatile attribute?

 

Basic button handling in SysTick ISR:

https://community.st.com/t5/stm32-mcus-products/external-interrupt-and-button-debounce/m-p/722909

No capacitor needed. ;)

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

I don't have a capacitor.

 

volatile uint8_t button_state = 0;      // Butonun kararlı durumu
volatile uint8_t button_debounce_flag = 0;
volatile uint16_t debounce_counter = 0; // Debounce için sayaç
#define DEBOUNCE_THRESHOLD 50           // 50 ms debounce süresi

void SysTick_Handler(void)
{
    // SysTick ISR
    if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == GPIO_PIN_RESET) // Butona basıldı mı?
    {
        if (debounce_counter < DEBOUNCE_THRESHOLD)
            debounce_counter++;

        if (debounce_counter >= DEBOUNCE_THRESHOLD && button_debounce_flag == 0)
        {
            button_state = 1;             // Butona basıldığını kaydet
            button_debounce_flag = 1;    // Daha fazla işlem yapmayı engelle
        }
    }
    else
    {
        debounce_counter = 0;            // Sayaç sıfırlanır
        button_debounce_flag = 0;        // Tekrar algılama açılır
        button_state = 0;                // Butonun bırakıldığını kaydet
    }
}

 while (1)
    {
        if (button_state)
        {
            // Butona basıldığında yapılacak işlemler
            HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); // Örnek: LED toggle
          
        }
    }

 

it doesn't work.

The code you may find by following the link form my post above does work.

Your code is toggling the LED indefinitely while button_state is nonzero.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice