Skip to main content
Riaan Laubscher
Associate II
March 13, 2019
Solved

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

  • March 13, 2019
  • 2 replies
  • 2517 views

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?

This topic has been closed for replies.
Best answer by Tesla DeLorean

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.

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
March 13, 2019

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
March 13, 2019

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