STM32 3 Phase PWM (Center-Aligned) Example Code

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

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

STM32 3 Phase PWM (Center-Aligned) Mode1, 2, 3

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

❕ Note

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.

❕ Note

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:

STM32 PWM Frequency Formula - STM32 PWM Frequency Equation

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)
STM32 3 Phase PWM (Center-Aligned) Example Code 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 Three Phase PWM Example Code (Center-Aligned Mode3)

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, center-aligned and perfectly synchronized with each other.

STM32 3 Phase PWM (Center-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 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.

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?

Leave a Comment