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
- STM32 Complementary PWM Output
- STM32 PWM Dead-Time Insertion
- STM32 Complementary PWM Output Example
- STM32 PWM Dead-Time Complementary Output Example
- 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.
While the BLUE signal is low, the GREEN PWM signal is HIGH, and vice versa.
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.25–2.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 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.
To achieve a 20kHz PWM signal, we need to set the Auto-Reload Register (ARR) to 3599. Using the equation below:
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.
#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)
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 Complementary PWM Outputs 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_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); while (1) { } } |
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 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.
Alternatively, we can modify the MX_TIM1_Init() initialization function, by changing the value in the following line of code:
1 | sBreakDeadTimeConfig.DeadTime = 0; // Change This to 72 |
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.
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 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.