Pulse Width Modulation (PWM) on 8-bit PIC® Devices
Pulse Width Modulation (PWM) is a common peripheral on almost every PIC® MCU device. PWM is a way to use a digital output to vary the on, or high time, to create a variable output or square wave (as seen in the accompanying figure). If the PWM signal runs at a fixed frequency, then changing the high time of the signal will also change the low time of the signal. The amount of time the signal remains high is typically called the pulse width. That pulse width relative to the period of the signal is called the duty cycle. The period of the signal is defined as the time from one rising edge to the next rising edge of the square wave signal and is inversely proportional to the PWM frequency. The period can easily be calculated by using the formula: Period = 1/Frequency. A frequency of 1 kHz will give a 1 millisecond period.
It is interesting to use PWM as there are many applications for it. Simplest of all applications is driving some kind of low-pass filter. As seen in this figure, by driving a simple Resistor/Capacitor (RC) low-pass filter circuit directly from the microcontroller’s PWM port, the variable pulse width will be averaged out by the capacitor’s charge and discharge rate through the resistor.
In a digital world, the high pulse can be considered “on” and the low pulse considered “off.” In the figure above, the following circuit diagrams, the pulses show what the low-pass filter is doing. The arrow shows the direction of the current. The bottom portion shows the charging and discharging voltage of the capacitor. The shape depends upon the frequency of the signal, and the values you select for the resistor and capacitor. The important thing to notice is the dashed line running through the charge/discharge signal. That dashed line is the average voltage across the charged capacitor. Even though the microcontroller is typically driving an output at five volts or zero volts only, you would see about 2.5 volts across the capacitor if you measured it with a voltmeter. This can be considered a simple Digital-to-Analog Converter (DAC).
Pulse Width Modulation Options
PIC MCU devices have several different versions of the PWM peripheral. It depends on the device and what features are included on that device. Here is a graphical representation of the different PWM generating options in the 8-bit family of PIC MCU.
10-Bit
On many 8-bit PIC MCU devices, the 10-bit PWM is a standard peripheral. It can be a stand-alone peripheral or incorporated into the Capture/Compare/PWM (CCP) peripheral.
PWM
The stand-alone PWM is available on many newer devices and sometimes in addition to the PWM incorporated into the CCP or ECCP peripheral.
CCP
On some devices, there is a Capture Compare PWM (CCP) peripheral that has the PWM peripheral incorporated as part of the peripheral.
ECCP
On newer devices, an Enhanced CCP (ECCP) adds even more capability than the CCP.
16-Bit PWM
On newer devices, there are 16-bit PWM peripherals that offer more resolution for adjusting the duty cycle of the PWM signal.
PSMC
Some devices include a programmable Switch Mode Controller that can produce a PWM signal designed for controlling a switch mode style power supply.
CWG/COG
In addition to the standard PWM modules, some peripherals can modify a PWM signal and produce complementary outputs. The Complimentary Waveform Generator (CWG) and the Complimentary Output Generator (COG) offer this capability. There are different generations of these modules as well and are typically added to the latest PIC MCU devices.
CWG
COG
COG/CWG Features
As the CWG and COG peripherals have evolved, updated features such as more auto shutdown sources, outputs, and modes have been added. Deadband timing options have been added as well. The accompanying table summarizes these improvements. Please refer to the device datasheet for the specific features available on the PIC MCU device you are using.