Skip to main content
Gustavo.Laureano
Senior
May 29, 2017
Question

Is safe update CNT, ARR and CCR with the timer stopped?

  • May 29, 2017
  • 2 replies
  • 777 views
Posted on May 29, 2017 at 18:30

Hello,

I am using PWM 2 mode to generate step pulses for a step/direction interface (and counting pulses thru the timer update interrupt),

But sometimes I am loosing the first step because of the preload of the CCR1 (update happens but the step was not given)

After reading the RM, I found that I can update the CCR value directly when I disable the preload (OC1PE), but the RM also warns about undefined behavior in PWM mode:

0690X000006078aQAA.png

So, for testing I used the following code for start the pulsing:

/* RESET the Preload enable bit for channel1 */
(&XTIMPERIOD)->Instance->CCMR1 &= ~TIM_CCMR1_OC1PE;
__HAL_TIM_SET_COUNTER(&XTIMPERIOD, 1); /* Reset period counter value */
__HAL_TIM_SET_AUTORELOAD(&XTIMPERIOD, (1000000/(uint32_t)actual_step_freq)-1); /* Adjust pulse period to step period */
__HAL_TIM_SET_COMPARE(&XTIMPERIOD, TIM_CHANNEL_1, (500000/(uint32_t)actual_step_freq) ); /* Adjust toggle period to step period/2 */
/* Set the Preload enable bit for channel1 */
(&XTIMPERIOD)->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
HAL_TIM_PWM_Start(&XTIMPERIOD, TIM_CHANNEL_1); /* start toggling */
HAL_TIM_Base_Start_IT(&XTIMPERIOD);�?�?�?�?�?�?�?�?�?�?�?�?

And is working flawlessly, but I want to confirm that this is safe to be used in this way(remembering that everytime I execute this code the timer & the PWM output will be stopped)

(for doing the acceleration and decceleration Iam also changing the ARR and CCR on the fly inside the interrupt, but in this case with the preload enabledand knowing that the changes will be applied only after the next update event/interrupt)

Thank you.

#ocxpe #oc1pe
    This topic has been closed for replies.

    2 replies

    Gustavo.Laureano
    Senior
    August 28, 2017
    Posted on August 28, 2017 at 12:28

    Standing with my question, is OK to disable the preload and update the CCR value with the timer stopped?

    waclawek.jan
    Super User
    August 28, 2017
    Posted on August 28, 2017 at 13:33

    Yes, but you can also force an update whenever you want using TIMx_EGR.UG