2024-11-30 07:40 AM - edited 2024-11-30 07:42 AM
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?
2024-11-30 09:27 PM
Are you tired of my questions?
2024-12-01 12:11 AM
Is there any mistake in my code guys?
2024-12-01 12:31 AM - edited 2024-12-01 12:36 AM
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.
2024-12-01 01:04 AM
What exact words should I search with?
2024-12-01 01:50 AM
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...
2024-12-01 06:11 AM - edited 2024-12-01 06:12 AM
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?
2024-12-01 08:04 AM
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.
2024-12-01 08:52 AM
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..
2024-12-01 09:36 AM
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.