2025-11-21 6:24 PM
Hi I am trying to create two phase-shifted PWM waves on an STM32L4 TIM1 using asymmetric mode. From what I understand, I have to:
enable output on GPIO channels 1 and 3
enable output compare 1,2,3,4 and output compare 1,2,3,4 as asymmetric PWM mode
Then, I set
CCR1 = 0, CCR2 = ARR, and CCR3 and CCR4 to the outputs provided by the function below.
Then I only enable channel 1 and channel 3 as outputs..?
So output compare 1 and 2 work together to produce the first PWM on channel 1. And output compare 3 and 4 work to produce the second PWM on channel 3.
This is what I understand from the graph on page 754 in the RM. But it is not allowing me to change the phase shift while still maintaining 50 % duty cycle on both waves. When I print my CCR3 and CCR4 values, they are exactly ARR apart, which would make me think that they should produce a 50% duty cycle wave. But they don't.
I'd appreciate any help!
static void tim_phase_shift(uint32_t ARR, float phase_deg, uint32_t *CCR3, uint32_t *CCR4)
{
uint32_t halfwave = ARR + 1U;
uint32_t period = 2*halfwave;
float phase_ticks_f = (phase_deg / 360.0f) * (float)period;
uint32_t phase_ticks = (uint32_t)(phase_ticks_f + 0.5f); // round
if (phase_ticks >= period)
phase_ticks -= period;
// start of the 50% window, wrapped into [0, halfwave-1]
uint32_t start = phase_ticks;
// end of the 50% window is half a virtual period later
uint32_t stop = (phase_ticks + halfwave);
*CCR3 = start;
*CCR4 = stop;
printf("ARR = %d\n", ARR);
printf("CCR3 = %d\n", start);
printf("CCR4 = %d\n", stop);
},