cancel
Showing results for 
Search instead for 
Did you mean: 

Push button: is there a need for software debouncing?

M7890.1
Associate III

Hi. I am trying to trigger and external interrupt using a push button (rising edge trigger and use internal pull down). When the push button is pushed, the variable cnt should be incremented and the pulse width of the PWM shall be changed according;y. When I try to introduce a small delay (I tried 250ms, 5ms and 1ms) in my ISR, it seems like the interrupt can only be triggered for the first time. If I remove the delay, the interrupt can be triggered every time but some of them was triggered by the bouncing which is not intended. How should I handle this?

Below are my code:

// from stm32f1xx_hal_gpio.c
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
  /* EXTI line interrupt detected */
  if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)
  {
    HAL_GPIO_EXTI_Callback(GPIO_Pin);
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
  }
}
uint16_t cnt = 0;
uint16_t pulse_width;
 
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
 
	if(GPIO_Pin == GPIO_PIN_1){
		char data[10] = {'\0'};
 
		pulse_width = 1000 + cnt*100;
		TIM2->CCR1 = (uint16_t)(pulse_width/20000)*65536;	// 65535 is ARR value
 
		lcd_clear();
 
		sprintf(data, "Count:%d", cnt);
		lcd_put_cur(0,0);
		lcd_send_string(data);
 
		sprintf(data, "PW:%dms", pulse_width);
		lcd_put_cur(1,0);
		lcd_send_string(data);
 
		if (cnt<10){
			cnt++;
		}
 
//		HAL_Delay(1);
	}
 
}

3 REPLIES 3

This is a lot of stuff to have in an interrupt/callback.

Perhaps monitor/record entry information, and print in foreground task.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

There are a few ways to do this. Set up a timer and ignore presses for 50ms after the initial press, or while the button is still pressed. You can get bounces when the button is released as well.

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

You can always add a capacitor in pararell with your input to debounce your signal. (harware low pass filter).

0693W00000Dq43yQAB.pngI used 220nF because it was a very strong mechanical bounce, you should try 10nF and maybe also a series resistance.0693W00000Dq42CQAR.gif 

we dont need to firmware by ourselves, lets talk