2021-08-21 05:26 AM
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);
}
}
2021-08-21 08:48 AM
This is a lot of stuff to have in an interrupt/callback.
Perhaps monitor/record entry information, and print in foreground task.
2021-08-22 11:58 PM
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.
2021-08-23 01:26 AM
You can always add a capacitor in pararell with your input to debounce your signal. (harware low pass filter).
I used 220nF because it was a very strong mechanical bounce, you should try 10nF and maybe also a series resistance.