cancel
Showing results for 
Search instead for 
Did you mean: 

external interrupt

XooM
Associate III

 

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?

14 REPLIES 14
XooM
Associate III

Are you tired of my questions? 

XooM
Associate III

Is there any mistake in my code guys?

gbm
Lead III

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
XooM
Associate III

What exact words should I search with?

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 "Accept as Solution".

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

Why? It's a question of: how often is a button used and is other program in some way time critical; If button is used maybe once every two days, it's better to need no time in systick, every ms, all the time, but only when a button press comes. That's it - if you do just some menu functions with many buttons and using them often, then your way of checking it in systick i would also prefer.

If you feel a post has answered your question, please click "Accept as Solution".
XooM
Associate III

This is a button I use frequently. The system needs to work by pressing this button continuously. Sometimes once a second, sometimes after 5 minutes..

XooM
Associate III
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.