Skip to main content
Associate II
September 29, 2026
Solved

GPIO pushbutton debounce using HAL_NVIC_DisableIRQ, newbie question

  • September 29, 2026
  • 6 replies
  • 59 views

Hi all, i have very simple gpio pushbutton debounce code, but 50% of the time it still gets a double-bounce (led toggles twice), i.e. the debounceCount is double incremented as the interrupt service routine is mysteriously called twice. How can this be if the interrupt is immediately disabled?.  Is the switch bounce sometimes so quick that two presses are queued up before the interrupt is disabled?

Within main() , if the button is pressed, there is a while(1) if(butIsPressed) {butIsPressed=FALSE; HAL_Delay(1000); and then HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);

debounceCount++;

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3); // Toggle The Output (LED) Pin

butIsPressed = TRUE;

}

 

Best answer by mƎALLEm

The problem is that the EXTI pending bit is cleared as soon as it enters the EXTI IRQ handler.

Regarding the software mechanism need more elaborate one using a timer to filter the bounce.

You can review this thread: 

 

 

6 replies

mƎALLEm
ST Technical Moderator
September 29, 2026

Hello,

That won’t work as you didn’t manage to disable the EXTI line and manage the EXTI pending bit. If you reach the HAL_Delay(), the bounce phenomena will occur again and the EXTI interrupt will fire and interrupt the HAL_Delay execution and the EXTI callback will be called. Need another software mechanism or add a capacitor on the GPIO pin to decrease the impact of the bounce and filter the glitches.

Did you add a capacitor on the pin?

Example of implementation:

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
John123Author
Associate II
September 29, 2026

Many thanks, there is no capacitor (yet); what would the correct software mechanism look like - are you saying that HAL_NVIC_DisableIRQ doesn’t actually disable the interrupt. What is the general approach for truly disabling the interrupt. Thanks for helping, John.

mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
September 29, 2026

The problem is that the EXTI pending bit is cleared as soon as it enters the EXTI IRQ handler.

Regarding the software mechanism need more elaborate one using a timer to filter the bounce.

You can review this thread: 

 

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
John123Author
Associate II
September 29, 2026

Thanks everyone.

mƎALLEm
ST Technical Moderator
September 29, 2026

If your question has been answered please mark as Best Answer the post that answered the most your question.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.