AnsweredAssumed Answered

TIMER Update CNT value while running

Question asked by Daniel Studer on Nov 7, 2017
Latest reply on Nov 8, 2017 by Daniel Studer



I have a problem with the Timer2 on STM32F767.

I use the Timer2 as simple timer in Countermode Down. With this timer I want to realize a countdown to start another process once after the underflow interrupt has occured. So I initialized the Timer as countdown without autoreload as following:


  TimHandle_Trigger.Init.Period                       = countDown_us - 1;
  TimHandle_Trigger.Init.Prescaler                  = uwPrescalerValue;
  TimHandle_Trigger.Init.ClockDivision            = TIM_CLOCKDIVISION_DIV1;
  TimHandle_Trigger.Init.CounterMode            = TIM_COUNTERMODE_DOWN;
  TimHandle_Trigger.Init.RepetitionCounter    = 0;
  TimHandle_Trigger.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;


Next I start the timer to count down for example from 10 seconds to zero. Now I have to adjust/synchronize the counter value CNT (with a value I receive by RealTime Ethernet Protocol), therefore I have to check the actual CNT value and then adjust this value with my received one, if there is an offset.


For this I want to have a simple function to rewrite to the CNT value to adjust the counter. But here is the problem. This is not working. I did this as following:


inline void adjust_timer2(uint32_t countDown_us)


    /* Update Disabled to reinitialize counter value */
    TimHandle_Trigger.Instance->CR1 |= TIM_CR1_UDIS;
    /* Set new countdown value in us */
    TimHandle_Trigger.Instance->CNT = countDown_us;
    /* set UG bit for Update generation to reinitialize counter value */
    TimHandle_Trigger.Instance->EGR |= TIM_EGR_UG;
    /* Update Enabled */
    TimHandle_Trigger.Instance->CR1 &= ~TIM_CR1_UDIS;

    /* clear all interrupts */
    TimHandle_Trigger.Instance->SR = 0;



Debugging this, I can see in the register of the timer2 it generates an update interrupt immediately and I get an update interrupt. After this I get a second update interrupt but not after the time I set the new CNT value. The second interrupt occours just after the first one.


What could be the problem here? Do I have to reload the CNT value on an other way, while the Timer2 is already running?


Thanks for any help.