Push button: is there a need for software debouncing?
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);
}
}