How can I syncronize two PWM on STM32 using the Embedded Coder Support Package
Show older comments
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
More Answers (1)
Shivam
on 11 Nov 2025
0 votes
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!
Categories
Find more on STMicroelectronics Discovery Boards in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!