cancel
Showing results for 
Search instead for 
Did you mean: 

Modify PWM frequency on the fly desynchronize master and slave timers

Cruvix
Associate II

Hello,

I'm facing a problem with a master timer and slave timer on Nucleo-STM32H723ZGT6U. To better understand my issue, I will present you my project.

Project presentation

My aim is to make two PWM signals like this :

Cruvix_1-1706024085810.png

The red signal is named "synchro arch" and the orange one "synchro pulse". Both have their own duty cycle, the period of synchro arch is 100 times the synchro pulse period and there is a shift (delay) between the two signals.

The duty cycle of synchro arch, the duty cycle of synchro pulse, the delay and the period of both timers are programmable using UART commands.

MCU configuration

My MCU use two 32 bits timers (TIM2 and TIM23) and the USART3 to work. For the clock, I used the external clock of the ST-Link (HSE 8.33MHz).

Timers configuration

To generate those signals, I configure the timer 2 as master timer in PWM mode. The TRGO signal used is the channel 2 of this timer. The timer 23 is a slave timer (Trigger slave mode) in PWM mode too, and triggered on TRGO rising edge of the master timer.

Tests

When I run my program, everything is fine, the period of synchro arch and synchro pulse is correct, the delay is correct and the duty cycles are corrects with these defaults values :

  • synchro pulse period = 500µs OK
  • synchro arch period = 100 * synchro pulse period = 100 * 500µs = 50ms OK
  • synchro pulse duty cycle = 30% OK
  • synchro arche duty cycle = 30% OK
  • delay between synchro arche and synchro pulse = 20µs OK

Then, I test to send command using UART to see if I can modify those parameters on the fly : I can modify the duty cycle of both signals and modify the delay without problems.

My issue

As nothing is perfect :,) I'm facing a problem when I try to modify on the fly the period of the signal. I enter my custom UART command to modify the period "PERIOD SP = 503", I isolate the command (PERIOD SP) and I isolate the parameter (503µs). But here, the two timers (master and slave) are desynchronized :o

I also notice that when I set period multiples of 100 like 100µs, 200µs, ..., 500µs I have no problem, the timers are synchronized, but otherwise (503 µs for example) the timers are desynchronized.

Please do you have any idea to fix this problem ? I attach the code of the timer module to help you.

Timer module description

In this part I describe the philosophy of the attached code of the timer module. First, there are two functions startPWMGeneration and stopPWMGeneration. In the main module, when I receive commands, I stop the PWM generation, execute a setter function (like setSyncPulsePeriod) and restart the PWM generation with startPWMGeneration. In setter functions, I write into timers registers like ARR, CCR1 and CCR2.

Hypothesis

After some researches before write this post, I have some hypothesis :

  • Does set HSE frequency to 8.33MHz in clock configuration in CubeMX is a good idea ?
  • Need I clear/set a register's bit before/after setting the ARR registers ?
  • Need I enable some timer interrupts ? (my two timers are free running timers, so I don't enable any interrupts)
  • Does my macros to calculate the ARR registers and CCR registers return expected results ? (in debug mode, the registers are coherent)

Thanks in advance for your answer.

Regards.

Cruvix

10 REPLIES 10
Cruvix
Associate II

Oh my bad, I completely forgot to describe signals on the scope, sorry.

The blue signal is the rising edge of synchro arch and the yellow one the rising edge of the first synchro pulse. Here, there is a delay of 20µs. For others synchro pulse it is the same, they oscillate between 2 positions exactly.

Thank you for the code snippet!