Skip to main content
SWenn.1
Senior III
December 30, 2022
Solved

STM32L476 Timer: on button push, load Compare Register with debounce value, then generate IRQ?

  • December 30, 2022
  • 14 replies
  • 3893 views

Hello....

I have TIM1 clocking at 1ms. It rolls over at 1000 counts (ie 1 second). Within the pushbutton IRQ I add 50 to the timer register and disable the GPIO IRQ. I would also like to enable a compare IRQ here as well, such that after the counter counts 50ms it generates a compare IRQ at which point I will re-enable the GPIO IRQ and set a flag for main.

 

I used LL code to set the timer and disable the GPIO IRQ but don't know exactly what registers to touch to enable the compare IRQ....can someone please tell me how to do this?

 

I do not want to have a timer that gets enabled in the GPIO IRQ and counts for 50 then generates an interrupt as I want to make use of a continuous timer that is running at 1ms and rolls every second as this is available for other purposes.

 

Thanks

 

This topic has been closed for replies.
Best answer by Piranha
htim1.Instance->CCR1 |= (htim1.Instance->CNT + 50);

You are ORing the numbers and ignoring overflows.

htim1.Instance->SR &= ~TIM_SR_CC1IF;

Another unnecessary and broken RMW (read-modify-write) operation. And clearing of that flag should already be done in the HAL internally.

Also take a note that RMW operations and probably that code in general are not interrupt safe. Such code requires all of the related interrupts to be at the same priority level and similar code cannot be used from non-interrupt context outside of a critical section.

14 replies

gbm
Super User
January 1, 2023

Oh, of course you may and can use as many different interrupts for the simple task of checking the button state as you want. There are some other candidates besides EXTI: USB and ADC being the most obvious ones. I just said there is no point in using EXTI because in any case you MUST also use timer interrupt, and you can achieve the same effect with significantly reduced source code size and complexity by using ONLY the timer interrupt, without engaging EXTI at all.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice