In this tutorial, we’ll discuss The STM32 3-Phase PWM (Center-Aligned) Mode, how to configure and use the Combined 3-Phase PWM Outputs Mode in STM32 microcontrollers, the difference between the STM32 PWM Center-Aligned Modes (Mode1, Mode2, Mode3), and how to set the PWM signal’s duty cycle & frequency with code example and a full test project. Without further ado, let’s get right into it!
Table of Contents
- STM32 3-Phase PWM Combined Output
- STM32 3-Phase PWM (Center-Aligned)
- STM32 3-Phase PWM Center-Aligned Example
- Wrap Up
STM32 3-Phase PWM Combined Output
The STM32 Advanced-Control Timers (like TIM1 & TIM8) support a special mode called Combined 3-Phase PWM Output Mode. In this mode, 3 PWM output channels are generated and controlled in a synchronized manner. This mode is extremely useful for driving MOSFETs half-bridge, gate drivers, inverters, DC-DC converters, BLDC/PMSM motor control, and many more applications.
The 3-Phase PWM Combined output channels can operate in Center-Aligned or Edge-Aligned modes. Each mode has its own applications but at the end of the day, you can achieve similar results using any of which and it gets down to how your control algorithm is modeled and implemented.
Here is what the PWM Center-Aligned Signal looks like. Given that the ARR=8, then a CCRx value of 4 is 50% duty cycle, CCRx=7 is 87.5% duty, and a CCRx value of 8 which is equal to the ARR register gives you a 100% duty cycle signal.
STM32 3-Phase PWM (Center-Aligned)
Center-Aligned PWM is a widely used technique in motor control and power electronics applications because it reduces switching losses and electromagnetic interference (EMI). Unlike edge-aligned PWM, the center-aligned mode could be a bit harder to set up and configure.
Center-aligned is preferred for multi-phase motor controls and power electronics applications. It generates less EMI noise since the turn-on and turn-off times are slightly different for each phase. However, in edge-aligned PWM, all the phases turn on at the same time which causes a large current spike and its associated harmonics.
In this tutorial, we’ll only focus on the Center-Aligned 3-Phase PWM output generation and we’ll get to the edge-aligned mode in an upcoming tutorial. The Center-Aligned PWM has 3 modes to choose from: (Mode1, Mode2, and Mode3). We’ll discuss each of these in this section hereafter.
1) STM32 3-Phase PWM Center-Aligned Mode1 (Up Counting)
In Mode 1, the timer counter starts from zero and increments until it reaches the auto-reload value (ARR). When the counter matches the compare value (CCR), the output state is toggled.
2) STM32 3-Phase PWM Center-Aligned Mode2 (Down Counting)
In Mode 2, the timer counter starts from the auto-reload value (ARR) and decrements to zero. When the counter matches the compare value (CCR), the output state is toggled. This mode provides output waveforms similar to that of Mode 1 but reverses the timing sequence.
3) STM32 3-Phase PWM Center-Aligned Mode3 (UpDown Counting)
In Mode 3, the timer counts from 0 up to the ARR value then down to 0 again within a single timer period (cycle). This makes it a bit different in the way we configure the PWM signal’s frequency and duty cycle. Since it only needs half the value that we’ll calculate using generic PWM formulas due to the fact that the counter is counting twice (up and down).
Writing to the counter while running in center-aligned mode is not recommended as it can lead to unexpected results, and signal glitches/artifacts. In some control systems, this can be dangerous to do.
The safest way to use center-aligned mode is to generate an update by software (setting the UG bit in the TIMx_EGR register) just before starting the counter and not to write the counter while it is running.
The Center-Aligned PWM Mode 3 is counting up and down in the same cycle. Therefore, the way we calculate the (ARR value for frequency control and the CCR for duty cycle control) is going to be different. We’ll do the calculations as usual using this calculator tool. However, we’ll “halve” the calculated values and write them to the corresponding registers.
STM32 3-Phase PWM Center-Aligned Example
In this example project, we’ll configure our STM32 microcontroller’s Timer1 to enable the 3-Phase Center-Aligned PWM outputs on channels CH1, CH2, and CH3. We’ll set the PWM output signal’s duty cycles to (25%, 50%, and 75% respectively), and the PWM frequency to 20kHz which is a common value for many control systems across different domains (in motor control, lighting systems, etc).
#1
Open STM32CubeMX, create a new project, and select the target microcontroller. For me, it’s (STM32F103C8T6 / BluePill)
#2
Configure the advanced-control timer (TIM1) as follows to enable the center-aligned mode 1 PWM outputs on channels (1, 2, and 3).
To achieve a 20kHz PWM signal, we need to set the Auto-Reload Register (ARR) to 3599. Use the equation below, or use this calculator tool:
Given that the desired FPWM is 20kHz and my FCLK is 72MHz, let the prescaler PSC = 0.
Then by solving for the ARR value, we’ll get 3599. However, as mentioned earlier in the center-aligned Mode 3, we should use only half the calculated value for ARR or CCR registers to control the PWM signal’s frequency and duty cycle properly. Therefore, we’ll write 1800 in the ARR register to achieve a 20kHz PWM.
Similarly, for duty cycle “pulse” duration, we’ll write the following values:
- 25% Duty Cycle = 450 Ticks (pulse)
- 50% Duty Cycle = 900 Ticks (pulse)
- 75% Duty Cycle = 1350 Ticks (pulse)
#3
Go to the Clock configuration page and select a clock source to give you the maximum SysClk of 72MHz (in my case).
#4
Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.
STM32 Three Phase PWM Example Code (Center-Aligned Mode3)
Here is The Application Code For This LAB (main.c)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * LAB Name: STM32 3-Phase PWM (Center-Aligned) Example * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include "main.h" TIM_HandleTypeDef htim1; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_TIM1_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_TIM1_Init(); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); while (1) { } } |
In the auto-generated MX_TIM1_Init() function, I’ve modified the initialization pulse values using the following numbers:
1 2 3 | sConfigOC.Pulse = 450; // For PWM CH1 sConfigOC.Pulse = 900; // For PWM CH2 sConfigOC.Pulse = 1350; // For PWM CH3 |
STM32 3-Phase PWM Outputs Example Testing
Here are the output signals on the Timer1 PWM Channels (1,2, and 3) pins as shown on my DSO. There are our 3-phase PWM signals at 20kHz with (25%, 50%, and 75%) duty cycles, center-aligned and perfectly synchronized with each other.
Required Parts For STM32 Examples
All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:
QTY. | Component Name | Amazon.com | AliExpress | eBay |
1 | STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz) | Amazon | AliExpress | eBay |
1 | Nucleo-L432KC (ARM Cortex-M4 @ 80MHz) | Amazon | AliExpress | eBay |
1 | ST-Link V2 Debugger | Amazon | AliExpress | eBay |
2 | BreadBoard | Amazon | AliExpress | eBay |
1 | LEDs Kit | Amazon & Amazon | AliExpress | eBay |
1 | Resistors Kit | Amazon & Amazon | AliExpress | eBay |
1 | Capacitors Kit | Amazon & Amazon | AliExpress & AliExpress | eBay & eBay |
1 | Jumper Wires Pack | Amazon & Amazon | AliExpress & AliExpress | eBay & eBay |
1 | Push Buttons | Amazon & Amazon | AliExpress | eBay |
1 | Potentiometers | Amazon | AliExpress | eBay |
1 | Micro USB Cable | Amazon | AliExpress | eBay |
★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★
Download Attachments
You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.
Wrap Up
In conclusion, we’ve explored the STM32 3-Phase PWM Center-Aligned Output Mode, how it works, and how to configure the STM32 advanced control timer to generate 3-Phase PWM signals with configurable frequency and duty cycle.
You can build on top of the provided example code project and integrate it into your system. You can also check the rest of the tutorials in this series to learn more about other features of the STM32 PWM.