2018-06-14 08:37 PM
Working on an STM32F103, using PWM to generate waveforms. The timer is in One Pulse mode and uses the Repetition Count Register to clock out the required signals. Based on user selection, I am stopping and starting the output using
HAL_TIM_PWM_Stop_IT()/HAL_TIM_PWM_Start_IT()
. There are multiple timers in a master-slave configuration, and interrupts are being used to interface with a DAC, so there are many things that must remain in sync.As expected, stopping and re-starting the timer does not affect the timer state, notably the counter
CNT
. That's fine as I can simply zero this register before restarting, to ensure the output is in sync. The problem is that the timer is stopped at some arbitrary point in time at which the repetition counter is at some arbitrary value, and when the output is restarted, the repetition counter appears to resume from this arbitrary value, thus introducing a short delay or phase shift in the output, proportional to the remaining count. So the first run of the output after powerup is always correct and in sync:This shows all the outputs going from off to on, as expected.
However, second and subsequent times we stop and start the PWM output, we see the output starts but then stops unexpectedly leaving a variable length gap right after, then resumes and continues correctly:
Each time the device stops and restarts, the gap is of a variable length. I am manually resetting the
TIMx_CNT
register before enabling the PWM output. The varying size of the gap, which appears to have a maximum size, is completely consistent with the repetition count not starting from 0.In the reference manual (RM0008) in Section 14.4.13 describing
TMx_RCR
, it mentionsREP_CNT
, and ''Figure 52: Advanced control timer block diagram'' has boxes showing the ''REP Register'' and ''Repetition Counter'', but I can't find any more info on the subject, or any way to access this in the register map. This appears to be the *actual* running repetition count which would be reset by the value from RCR.How can we clear REP_CNT to avoid these glitches? Any insights appreciated! Thanks -
:: Gavin
#stm32 #pwm #timing #waveform #clock #timerSolved! Go to Solution.
2018-06-15 12:25 AM
2018-06-15 12:25 AM
JW
2018-06-15 03:24 AM
Jan,
I added the following code before restarting the timer PWM outputs and it worked:
TIM1->EGR |= (1 << 0); // Bit 0: UG
I was too focused on trying to modify/reset the state, and I didn't realise you could trigger timer events in software in this way. Very useful to know.
Thanks so much! Cheers -
:: Gavin