In this tutorial, we’ll discuss The STM32 Edge-Aligned PWM Mode, how to configure and use the Combined 3-Phase PWM Outputs Mode in STM32 microcontrollers, 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 Edge-Aligned PWM (3-Phase Combined Output)
- STM32 3-Phase PWM (Edge-Aligned)
- STM32 Edge-Aligned 3-Phase PWM Example
- Wrap Up
STM32 Edge-Aligned PWM (3-Phase 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.
Edge-Aligned PWM output mode is widely used in power electronics & control systems applications like motor control, converters, etc. This mode can also be used to generate 3-phase PWM combined output signals in a similar way to the Center-Aligned PWM Mode. However, Center-aligned is more preferred.
Center-aligned PWM 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.
We’ve discussed the Center-Aligned PWM Mode in great detail in a previous tutorial. In this tutorial, we’ll only focus on the Edge-Aligned 3-Phase PWM output generation.
STM32 3-Phase PWM (Edge-Aligned)
Edge-Aligned PWM mode in STM32 microcontrollers can be used to generate 3-phase PWM signals where all channels start their pulses simultaneously at the counter’s reset (rising or falling edge).
Here is what the PWM Edge-Aligned Signal looks like. Given that the ARR=8, then a CCRx value of 4 is 40% duty cycle, CCRx=8 is 80% duty, and a CCRx value of 9 or more gives you a 100% duty cycle signal.
The signal diagram above shows the “Upcounting Mode” for the edge-aligned signal generation. However, we can also configure it to operate in a “Downcounting Mode”, if needed.
In Downcounting Edge-Aligned Mode (PWM mode 1), the reference signal OCxRef is LOW as long as TIMx_CNT > TIMx_CCRx else it becomes high. Therefore, a 0% PWM duty cycle is not achievable in that mode.
STM32 Edge-Aligned 3-Phase PWM Example
In this example project, we’ll configure our STM32 microcontroller’s Timer1 to enable the 3-Phase Edge-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, the calculated values are as follows.
The ARR value will be 3599, and the prescaler (PSC) is 0.
For duty cycle “pulse” duration, we’ll write the following values:
- 25% Duty Cycle = 900 Ticks (pulse)
- 50% Duty Cycle = 1800 Ticks (pulse)
- 75% Duty Cycle = 2700 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 Edge-Aligned PWM 3-Phase Example Code
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 (Edge-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 = 900; // For PWM CH1 sConfigOC.Pulse = 1800; // For PWM CH2 sConfigOC.Pulse = 2700; // 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, Edge-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 Edge-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.
ALLAH BLESS U BROTHER,