cancel
Showing results for 
Search instead for 
Did you mean: 

Why does a timer operating in interrupt mode generate an update event immediately after intialization?

Riaan Laubscher
Associate II

Hello

I have TIM3 set up like so:

  • ABP1 Timer clock = 84 MHz
  • Prescaler = 8399
  • Period 9999
  • Update event selected as trigger
  • NVIC enabled for TIM3 global interrupt

This gives me a timer interrupt every 1 second. But it appears as though the callback HAL_TIM_PeriodElapsedCallback is called immediately after I call HAL_TIM_Base_Start_IT(&htim3) and then in intervals of 1 second thereafter.

Why does the period elapse immediately after starting the timer? Is there a way to get around this?

1 ACCEPTED SOLUTION

Accepted Solutions

UPDATE is CNT==0

So both the initial load, and the clock after CNT=ARR will trigger

Not sure it is avoidable, short of clearing and waiting for CNT>0 and then enabling the interrupt, or special casing the first interrupt call.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

UPDATE is CNT==0

So both the initial load, and the clock after CNT=ARR will trigger

Not sure it is avoidable, short of clearing and waiting for CNT>0 and then enabling the interrupt, or special casing the first interrupt call.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

The "library" functions for setting up timers tend to set TIMx_EGR.UG to force any prescaler change to apply immediately (see for example TIM_Base_SetConfig() called from all the HAL_TIM_XXXX_Init() functions; but the same could be found in the respective functions of SPL too).. This of course has the effect of setting TIMx_SR.UIF thus the interrupts gets fired as soon as it's enabled.

The solution is to clear TIMx_SR after calling the _Init() function and before enabling the interrupts. I don't use Cube so can't help with the exact Cube/HAL incantation for that.

JW