Electronics > Projects, Designs, and Technical Stuff
STMF303 phase shifted PWM
(1/3) > >>
uer166:
I'm making a 2-phase boost with peak current mode control with an STM32F303. I'm having a hard time generating the 180-degree phase shifted, cycle-by-cycle current limited PWM for Timer1 channel1 and Timer1 channel3. I put them on the same advanced timer thinking I could use the Asymmetric PWM mode, or perhaps the Combined PWM mode.

I'm having a hard time finding a mode that does what I want.. I also tried synchronizing Timer 4 with Timer 1, thinking I can just use separate timers and sync their counters 180 degrees apart? Still nada, a bit stuck.. Knew I should have just used a STM32G4 from the beginning, but this seemed like an easy task.

Another problem is I can't figure out how to clear the 2 different PWM channels separately, Timer1 seems to have only one clear source for the whole thing.
Siwastaja:
Should be trivial, maybe you are missing something?

Some copy-pasta which works for me, giving 180 deg phase shift, on STM32F7 advanced control timer. I think these timers are pretty much the same between the products?


--- Code: --- // TIM8:
// Create a 50% duty square wave on CH2&CH2N, and another one 180deg apart on CH3&CH3N
TIM8->CR1 = 0UL<<7 /*Auto-reload ARR register is NOT buffered*/ |
0b00UL<<5 /*Edge-aligned mode*/ |
1UL<<4 /*downcounter*/;

// PHA = CH2, PHB = CH3   Positive channel = Hi-side FET, Negative channel = Lo-side FET
// PWM mode 1 "0110": active when   CNT<CCR
// PWM mode 2 "0111": inactive when CNT<CCR
// WARNING: Highest mode bit is elsewhere in the register

TIM8->CCMR1 = 0UL<<11 /*CH2 Pre-load OFF*/ |
0b110UL<<12 /*CH2 PWM mode 1*/;
TIM8->CCMR2 = 0UL<<3 /*CH3 Pre-load OFF*/ |
0b111UL<<4 /*CH3 PWM mode 2*/;

TIM8->CCER = 1UL<<4 /*CH2 enable*/ |
1UL<<6 /*CH2N enable*/ |
1UL<<8 /*CH3 enable*/ |
1UL<<10 /*CH3N enable*/;

TIM8->BDTR = 1UL<<15 /*Main Output Enable*/ |
1UL<<11 /*OSSR, In off-state, the idle states are driven*/ |
1UL<<10 /*OSSI, same thing*/ |
10UL /*Deadtime, 8-bit value must begin with 0, max 127*/;
             
TIM8->ARR = 10000;
TIM8->CCR2 = 5000;
TIM8->CCR3 = 5000;

TIM8->BDTR |= 1UL<<15;  // Main Output Enable
TIM8->CNT = 0;
TIM8->EGR = 1UL; // Generate update event
TIM8->CR1 |= 1UL; // Enable counter

--- End code ---
uer166:
I think it's trivial for 50% duty cycle, in your case you're making one PWM mode 1 and the other mode 2, not quite sure if that works for, say 20% duty PWM or 80% duty PWM while maintaining 180-degree phase shift, I will certainly try. I think I finally found a way to synchronize different timer counters, turns out the trigger connections between timers are hard-wired and need to look at the interconnect matrix to figure it out..
fourtytwo42:
I also failed with this series of chips, it seems the apparently Arm IP doesn't have the concept of push-pull operation.
If your sold on Arm either find some rare part somewhere that doesn't use the same IP block (I failed on that too), use a PLD to implement a proper PWM externally or give up on Arm and use Microchip PIC24EP/DSPIC33 motor control series (MC) like I did, warts and all but at least Microchip understand the concepts required.
capt bullshot:

--- Quote from: uer166 on May 30, 2020, 03:18:45 am ---I'm making a 2-phase boost with peak current mode control with an STM32F303. I'm having a hard time generating the 180-degree phase shifted, cycle-by-cycle current limited PWM for Timer1 channel1 and Timer1 channel3. I put them on the same advanced timer thinking I could use the Asymmetric PWM mode, or perhaps the Combined PWM mode.

I'm having a hard time finding a mode that does what I want.. I also tried synchronizing Timer 4 with Timer 1, thinking I can just use separate timers and sync their counters 180 degrees apart? Still nada, a bit stuck.. Knew I should have just used a STM32G4 from the beginning, but this seemed like an easy task.

Another problem is I can't figure out how to clear the 2 different PWM channels separately, Timer1 seems to have only one clear source for the whole thing.

--- End quote ---

One tip: If you want to do phase shifted PWM on STM32 timers, you must use different timers for the different phases. So e.g. TIM1 for the first phase, TIM2 for the second (or TIM8 if you need the dead time generation capabilities). The timer counters can be preloaded to achieve the phase shift, then the timers can be started synchronously - read the manuals for details. You can't generate multi-phase PWM using one timer only. Might be different with the high resolution timers from other derivatives of that family, but I'm not familiar enough with those to give you a simple answer.

Navigation
Message Index
Next page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod