Skip to content

Pulse Width Modulation (PWM)

Published On:
Oct 4, 2012
Last Updated:
Jul 11, 2024

Operating Modes

PWM hardware peripherals may not support every one these modes.

Edge-Aligned (aka Asymmetric)

Edge-aligned, the most common (and usually the default) operating mode for a PWM peripheral, is when one edge of the switching from on/off is aligned with the edge of of the PWM period. In it’s most basic form it is simple to implement, requiring a counter, and two registers, one for resetting the counter (which determines the PWM period), and another for comparing with the count value, and switching the output if it’s greater than/less than/some logical expression (this sets the duty cycle).

Centre-Aligned (aka Symmetric)

Centre-aligned PWM is good for motor control as it generates fewer harmonics in the output voltage and current than asymmetric PWM. Some centre-aligned PWM hardware peripherals implement this by using a counter which changes direction every cycle. It counts up for the first cycle, down for the second, and then repeats. Doing this effectively reduces the PWM frequency by 2. So to arrive back at the same PWM frequency as when in asymmetric mode, you have to half the period (in terms of clock cycles). This reduces your duty cycle resolution.

Complementary Outputs

Some PWM peripherals have a complemntary output feature. This is one PWM timer in hardware controls two output pins. One pin is the normal PWM signal, the other is the inverse of this. Complementary outputs are useful for driving H-bridges and other motor control circuits. Usually these peripherals will also support an adjustable programmable dead-time to prevent shoot-through.

Diagram showing how deadtime is applied to the standard and complementary PWM signals to prevent shoot-through.

One example is the STM32 advanced motor control timers which are present in many of the STM32 MCU families including the STM32G series1.

A Warning About PWM And Oscilloscopes

When viewing a PWM output on an oscilloscope, you can experience what is called aliasing, and oscilloscope seems to show that the PWM signal is stopping and then restarting at a rate much slower than the PWM frequency.

An oscilloscope aliasing problem which appears to show that the 15kHz PWM signal turning on and off (in reality it is continuous).

While your PWM could actually be doing this, more often than not what you are seeing is an artefact caused by the digital sampling of the oscilloscope. The following picture shows a 15kHz PWM signal appearing to stop and start every 10ms or so. I can assure you that the PWM signal is fine, it is the Rigol 100MHz oscilloscope showing the wrong thing.

Footnotes

  1. STMicroelectronics. AN4013 - Application note - Introduction to timers for STM32 MCUs. Retrieved 2024-07-11, from https://www.st.com/resource/en/application_note/an4013-introduction-to-timers-for-stm32-mcus-stmicroelectronics.pdf.