STM32 Complementary PWM & Dead-Time (Code Examples)

In this tutorial, we’ll discuss The STM32 PWM Input Mode, how to configure and use the PWM input mode in STM32 microcontrollers, and how to measure an input 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 Complementary PWM Output
  2. STM32 PWM Dead-Time Insertion
  3. STM32 Complementary PWM Output Example
  4. STM32 PWM Dead-Time Complementary Output Example
  5. Wrap Up

STM32 Complementary PWM Output

The STM32 Advanced-Control Timers (like TIM1) support a special mode called Complementary PWM Output Mode. In this mode, each timer channel can output two PWM signals each of which is a complement (inverse) of the other.

In other words, If you’ve set up Timer1 channel 1 to be a complementary output channel, you’ll have 2 output pins (TIM1_CH1 & TIM1_CH1N). A 70% duty cycle signal will appear on the CH1 channel, while a 30% duty signal will appear on the CH1N which will only go HIGH during CH1 is in LOW state.

This mode is extremely useful for driving MOSFETs half-bridge, gate drivers, inverters, DC-DC converters, BLDC motor control, and many more applications.

Here is what the PWM complementary signals would look like.

STM32 Complementary PWM & Dead-Time Insertion

While the BLUE signal is low, the GREEN PWM signal is HIGH, and vice versa.

❕ Note

The advanced control timer can also insert a dead time (both channels are LOW) during that time. This is extremely important for half-bridge MOSFET drivers to prevent shoot-through currents. Which happens at the edge when both channels’ states are swapped.


STM32 PWM Dead-Time Insertion

To prevent shoot-through currents in half-bridge MOSFET driver applications, we need to insert a dead band (dead time) in the complementary PWM output signal.

Typically, a dead time of (0.252.5µs) is sufficient for 20kHz PWM drive applications. More dead time is a lost power output potential that the system could’ve achieved. Less dead time is also a wasted power going through the MOSFETs at state transitions in the form of shoot-through currents.

So there is always a sweet spot for setting up the dead time value that suits your application. In some cases, we may even need to implement dead-time compensation algorithms that would complicate the control algorithm for the sake of achieving a bit higher output power.

Here is what the dead time looks like in a complementary PWM output signal.

STM32 Complementary PWM & Dead-Time Insertion


STM32 Complementary PWM Output Example

In this example project, we’ll configure our STM32 microcontroller’s Timer1 to enable the Complementary PWM output on CH1. We’ll set the PWM output signal’s duty cycle to 50%, and the frequency to 20kHz which is a common value for many control systems across different domains.

#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 complementary PWM outputs on CH1/CH1N.

STM32 Complementary PWM Output Example Code

To achieve a 20kHz PWM signal, we need to set the Auto-Reload Register (ARR) to 3599. Using the equation below:

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.

To set the duty cycle to 50%, we just need to write 0.5×3599 (≈1800) into the Pulse configuration input field as shown in the above screenshot.

#3

Go to the Clock configuration page and select a clock source to give you the maximum SysClk of 72MHz (in my case). The higher the clock rate, the easier it is to measure high-frequency PWM input signals.

STM32 Clock Tree Configuration CubeMX

#4

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 Complementary PWM Example Code Project

Here is The Application Code For This LAB (main.c)

STM32 PWM Input Mode Example Testing

Here are the output signals on the TIM1_CH1 & TIM1_CH1N pins as shown on my DSO. The signals are complementary to each other, the frequency is 20kHz, and their duty cycle is 50%.

STM32 Complementary PWM Outputs (20kHz With 1us Dead-Time)


STM32 PWM Dead-Time Complementary Output Example

This example project is exactly the same as the previous one except for the insertion of a dead time (1µs) in the complementary PWM output configurations.

To add a dead time of 1µs, we need to calculate the digital value as follows:

Dead Time = Value / FCLK

1µs = Value / 72MHz

Therefore, the digital value we need to write is 72

You can write it in the configuration tab in CubeMX as shown below.

STM32 PWM Dead Time Example Configuration

Alternatively, we can modify the MX_TIM1_Init() initialization function, by changing the value in the following line of code:

STM32 PWM Dead Time Example Testing

Here are the complementary PWM output signals (20kHz, 50% DC) with a dead time of 1µs exactly as we’ve configured it.

STM32 PWM Dead-Time Complementary Outputs (20kHz With 1us Dead-Time)


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 Complementary PWM Output Mode, how it works, and how to configure the STM32 advanced control timer to generate complementary PWM signals with configurable frequency, duty cycle, and dead time insertion.

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