Skip to main content
Associate III
November 30, 2024
Question

external interrupt

  • November 30, 2024
  • 14 replies
  • 2696 views

 

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?

This topic has been closed for replies.

14 replies

XooMAuthor
Associate III
December 1, 2024

Are you tired of my questions? 

XooMAuthor
Associate III
December 1, 2024

Is there any mistake in my code guys?

AScha.3
Super User
December 1, 2024

OK, you can use the exti for things like buttons ( I do it also) - but you should use some hardware, that avoids useless bouncing: here a simple cap, maybe 10 or 100 nF ( Input to GND), keeps fast signal away and gives a good signal for a button press.

Next is then some delay, if needed; maybe you want a minimum time like 400 ms for a pressed button.

To do this, just have in exti a global variable set: Button_1_press=1; 

And in main do anything then ...and have also the check for the delay, to be ready for the next press after some time:

If(Button_1_press==1){

If(Button_1_pressed==0) Button_1_pressed =Hal_get_tick();

If(Hal_get_tick() - Button_1_pressed > 400 ){

Button_1_pressed = 0; Button_1_press =0; 

}

}

That's it...

If you feel a post has answered your question, please click on " Best Answer ".
gbm
Super User
December 1, 2024

No EXTI => just SystTick interrupt (already firing in every project).

EXIT => EXTI + SysTick interrupt + (optionally) resistor and capacitor.

So why should anyone use EXTI for buttons?

 

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
gbm
Super User
December 1, 2024

The main mistake is the use of EXTI for button sensing. Do it inside SysTick ISR. I shown the solution long time ago - search for it.

The only reasonable case for buttons and EXTI is the wakeup from deep sleep modes.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
XooMAuthor
Associate III
December 1, 2024

What exact words should I search with?

XooMAuthor
Associate III
December 1, 2024
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++; 
 	 	 	 }
 }
}

I still can't figure out why this code isn't working. No matter how fast or slow I press the button, it doesn't work. The button variable is definitely not increasing.

XooMAuthor
Associate III
December 1, 2024
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.

AScha.3
Super User
December 1, 2024

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 on " Best Answer ".