PICkit™ 3 Starter Kit User’s Guide
DS41628B-page 68 2012 Microchip Technology Inc.
3.9 LESSON 8: PULSE-WIDTH MODULATION (PWM)
3.9.1 Introduction
This lesson does not rely on any of the previous lessons, but does use the same coding
techniques and information learned thus far.
In this lesson, a PIC MCU generates a PWM signal that lights an LED with the POT
thereby controlling the brightness.
3.9.2 Hardware Effects
Rotating the POT will adjust the brightness of a single LED.
3.9.3 Summary
Pulse-Width Modulation (PWM) is a scheme that provides power to a load by switching
quickly between fully on and fully off states. The PWM signal resembles a square wave
where the high portion of the signal is considered the on state and the low portion of
the signal is considered the off state. The high portion, also known as the pulse width,
can vary in time and is defined in steps. A longer, high on time will illuminate the LED
brighter. The frequency or period of the PWM does not change. A larger number of
steps applied, which lengthens the pulse width, also supplies more power to the load.
Lowering the number of steps applied, which shortens the pulse width, supplies less
power. The PWM period is defined as the duration of one complete cycle or the total
amount of on and off time combined.
PWM resolution defines the maximum number of steps that can be present in a single
PWM period. A higher resolution allows for more precise control of the pulse width time
and, in turn, the power that is applied to the load. In this lesson, the program will be
using 10 bits of resolution – the maximum allowed.
The term duty cycle describes the proportion of the on time to the off time and is
expressed in percentages, where 0% is fully off and 100% is fully on. In this situation,
a lower duty cycle corresponds to less power applied and a higher duty cycle
corresponds to more power applied.
3.9.4 New Registers
3.9.4.1 BOTH
3.9.4.1.1 All
The PIC16F1829 has two CCP modules and this lesson will use CCP2, while the
PIC18F14K22 only has one CCP module, so it will use CCP1.
It is recommended that the reader refer to the Capture/Compare/PWM section in the
data sheet to learn about each register above. This lesson will briefly cover how to
setup a single PWM.
Figure 3-8 summarizes how the PWM waveform is setup:
TABLE 3-28: NEW REGISTERS FOR BOTH DEVICES
Register Purpose
CCPXCON Setup of the “compare-capture-PWM” module
PRX The PWM period is specified by the PRx register of Timer2/4/6
CCPTMRS Selects what timer module is used in association with the PWM
CCPRXL Upper 8 bits (MSb) of PWM
TXCON Timer control register