cancel
Showing results for 
Search instead for 
Did you mean: 

Generate 2 PWMs with same frequency & duty, 90 degree phase shift, using one timer

Bhavani
Associate

I am using the STM32H753VGT6 microcontroller and need to generate two PWM signals with the same frequency and duty cycle, with a 90-degree phase shift between them, using a single timer. Is this possible? If yes, how can it be achieved?

9 REPLIES 9
Uwe Bonnes
Chief

This can not be done.

AScha.3
Super User

If you know, where the channels have to switch on and off (and calculate this correct),

can be done on advanced timer TIM1 + 8 , with setting : Combined PWM mode .

If you feel a post has answered your question, please click "Accept as Solution".
TDK
Super User

Combined PWM mode will let you have 2 PWM channels with arbitrary duty cycle and offset. They need to be the same frequency.

If ARR = 999, then CCR1 = 0, CCR2 = 100 and CCR3=250, CCR4=350 would be 10% duty cycle, 90 degree offset on channels 1 and 3. All timers with 4 channels can do this.

 

If you feel a post has answered your question, please click "Accept as Solution".
Uwe Bonnes
Chief

How will this work if > 75 duty cycle and 90 degree offset is requested? 

@Uwe Bonnes You can still do it but there would be a potential discontinuity when you switch settings. Change the polarity of the channel and set CCRx in reversed order.

If you wait for the right cycle within the PWM you could do it without a discontinuity, maybe.

If you feel a post has answered your question, please click "Accept as Solution".
Gyessine
ST Employee

Hello @Bhavani 
with a single timer, I don't think there is a clean way to generate two PWM signals that simultaneously keep exactly the same frequency, exactly the same duty cycle, and a constant 90° phase shift. You can force an initial 90° shift with something like this

 __HAL_TIM_SET_COUNTER(&htim1, 0);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
while (__HAL_TIM_GET_COUNTER(&htim1) < 250) { }
 HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);

, but from the next period onward both channels are driven by the same counter and same compare value, so they naturally re‑align in phase. To maintain a permanent 90° shift you must either use two synchronized timers (which allows same duty and frequency) or stay with one timer but accept that the two channels will have different duty cycles.
BR
Gyessine

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

mwalt.3
Associate III

Just for my general knowledge , what means 90° phase shift ? does it mean the period of the second channel starts at the middle of the period of the first one ?

 

it means the second channel starts 25% of the period after the first channel starts. The rising edges are 25% of a period width apart.

Still think this is perfectly doable with a single timer. OP seems to have disappeared.

If you feel a post has answered your question, please click "Accept as Solution".
mwalt.3
Associate III

ok , doable  :

- a simple tough : split the period in 4 ones , starting and ending at each rising or falling edge of both channels ; filling each of the 4 periods with 0 or 100% duty (the principle is that one can do everything with STM32 timers)
- I don't remember if one can "buffer" the polarity , ie. a polarity inversion only takes effect at the start of a period (like buffered period or duty cycles) . In that case one can split the period in 2 ones , set the periods and the duties according the need , invert or restore the second channel ...