How can I syncronize two PWM on STM32 using the Embedded Coder Support Package

30 views (last 30 days)
I need to generate two PWM signals with variable Duty cycle controlled by reading from two ADC, and that is simple. The problem is that I need to introduce a fixed phase difference between the two signals. I tryed using triggered subsystems, but it does not seem to work. The two PWM are on two different timers, should I use two different channels of the same timer ? Should I use a third timer in a master slave configuration ?

Accepted Answer

Tridib
Tridib on 31 Oct 2025 at 9:41
It is better to use two different channels on the same timer. This way, both signals are controlled by the same hardware and will always be in sync.
Set the timer to PWM mode and choose the frequency you want. Assign each PWM output to a different channel. To set the phase difference between the signals, just adjust the “Pulse” or “Compare Value” for each channel. You can also change the duty cycle of each channel in real time by reading values from the ADC and updating the compare registers in your Simulink model.
If you try to use two separate timers, it is much harder to keep them perfectly in sync, since their counters might not line up exactly. If you need more control, you can use a master-slave timer setup.
Hope this helps!

More Answers (1)

Shivam
Shivam on 11 Nov 2025 at 3:28
As per my understanding, you are trying to synchronise two timer modules with a fixed phase shift between the counters.
If you use channels of same timer module, they will use the same counter, due to which they will be synchronised with each other and it is not possible to have a phase difference between them.
You should use different timer modules. You need to configure one of the timer as a master timer and another timer as a slave timer. For the slave mode timer, you need to select proper trigger source for starting the conuter.
Lets understand this by an example. Suppose that you are using TIM1 and TIM2 modules and channel-1 as PWM output on both the modules (on STM32G431RB board).
Configure TIM2 in slave mode and set the trigger source to "ITR0". This will start the counter of TIM2 at the update event of TIM1. Here, the update event of TIM1 is generated on counter overflow.
Before TIM2 counter starts, program a pre-defined counter value to its CNT register, which is depended on the amount of phase shift you need between two timer modules. This can be done by using custom code programed in "Systems output" block. The "step" generators are programmed such that first the counter value is loaded into TIM2 counter register, then the counter of timer TIM1 is enabled.
Below given figure shows the custom code programmed in system outputs block:
How to select the initial value to be programmed in the TIM2 counter register?
Here, the period is 5000 counts. So 5000 counts corresponds to 360 electrical degrees (equivalent to 2*pi radians). So, if we want the phase shift of 180 degrees between counters of two modules, the initial value to be programmed in the TIM2 counter register should be 5000/2 = 2500. Here, its programmed to 3750 counts..
I this way you can find the initial value to be programmed based on the desired phase shift.
I hope it helps!

Products


Release

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!