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 (see Figure below). 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 is 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 1 kHz frequency will give us a 1 millisecond period.
Using PWM gets very interesting because there are many applications for it. Simplest of all is driving some kind of low-pass filter. By driving a simple resistor-capacitor (RC) low-pass filter circuit (see Figure below) 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.” The circuit diagrams below the pulses (in the Figure above) 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. Below is a graphical representation of the different PWM generating options in the 8-Bit family of PIC MCU.
On many 8-bit PIC MCU devices the 10-Bit PWM is a standard peripheral. It can a stand-alone peripheral or incorporated into the Capture/Compare/PWM (CCP) peripheral.
The stand-alone PWM is available on many newer devices and sometimes in addition to the PWM incorporated into the CCP or ECCP perpheral.
On some devices there is a Capture Compare PWM (CCP) peripheral that has the PWM peripheral incorporated as part of the peripheral.
On newer devices, an Enhanced CCP (ECCP) adds even more capability than the CCP.
On newer devices there are 16-Bit PWM peripherals which offer more resolution for adjusting the duty cycle of the PWM signal.
Some devices include a programmable Switch Mode Controller that can produce a PWM signal designed for controlling a switch mode style power supply.
In addition to the standard PWM modules, some peripherals can modify a PWM signal and produce complimentary outputs. The CWG: Complimentary Waveform Generator and the COG: Complimentary Output Generator offer this capability. There are different generations of theses modules as well and are typically added to the latest PIC MCU devices.
As the CWG and COG peripherals have evolved, updated features such as more Auto Shutdown Sources, Outputs and Modes have been added. Deadband timing timing options have been added as well. The table below summarizes these improvements. Please refer to the device data sheet for the specific features available on the PIC MCU device you are using.