STM32 Edge-Aligned PWM (3-Phase) Example Code

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

  1. STM32 Edge-Aligned PWM (3-Phase Combined Output)
  2. STM32 3-Phase PWM (Edge-Aligned)
  3. STM32 Edge-Aligned 3-Phase PWM Example
  4. 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.

STM32 Edge Aligned PWM

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.

❕ Note

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:

STM32 PWM Frequency Formula - STM32 PWM Frequency Equation

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)
STM32 3 Phase PWM (Edge-Aligned) Configuration

#3

Go to the Clock configuration page and select a clock source to give you the maximum SysClk of 72MHz (in my case).

STM32 Clock Tree Configuration CubeMX

#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)

In the auto-generated MX_TIM1_Init() function, I’ve modified the initialization pulse values using the following numbers:

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.

STM32 3 Phase PWM (Edge-Aligned) Example


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 NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ 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.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

1 thought on “STM32 Edge-Aligned PWM (3-Phase) Example Code”

Leave a Comment