common applications. If the PWM signal were connected
to an LED circuit, the brightness of the LED could be
changed by controlling the PWM signal’s duty cycle.
However, this is where the design techniques get tricky.
The speed at which the human eye can detect light plays
a factor. Therefore, you must match the frequency of your
PWM signal to what you want the LED to look like. If you
drive the LED on and off too quickly using the PWM
signal, then the LED will look like it never turned on. If you
drive it too slowly, then all you will see is a blinking light.
The trick is to get the frequency right and then adjust the
duty cycle. If you get this set up properly, then you can
control the brightness of the LED by just varying the duty
cycle in whatever increments you want. I’m sure you have
seen applications where an LED is controlled this way.
A typical one is lighting in an automobile. If you turn the
dimmer knob, you can change how bright or how dim the
instrumentation panel backlighting is. Most vehicles use
PWM to control the brightness of LEDs or even light bulbs
behind the displays. A light bulb’s brightness can also be
controlled by this method (see Figure 3).
Motor control is another application for PWM. By
controlling the ground circuit of a motor with a transistor
driven by a PWM signal (see Figure 3), you can control
the motor’s speed. This is another tricky design that
requires you to match the frequency of the PWM signal to
the motor characteristics, but this is very efficient.
LISTING 1: Simple PWM.
TRISC.5 = 0
' Set PORTC bit5 to Output
' Set PORTC bit5 high
' delay for 20 msec
' Set PORTC bit5 low
' delay for 80 msec
' loop back to main label
period is actually slightly more than 100 milliseconds,
because of the time required to execute the HIGH, LOW,
and GOTO commands, but these are minor. As you can
see, the short snippet of code produced a 20% duty cycle
signal at 10 Hz.
PICKIT 2 LOGIC TOOL
CREATING A PWM SIGNAL
So, how do you create a PWM signal with a
Microchip PIC? It turns out to be very easy, and you have
multiple options for doing so. As usual, I focus on the
beginner in this column, and microEngineering Labs’
PICBASIC PRO compiler is my choice for creating the PIC
software. The sample version (which limits you to 31
commands) will once again be enough to create a PWM
example. If all you want to do is create a PWM signal,
then the code snippet included here will get the job done.
(See Listing 1.)
The software simply drives PORTC pin 5 high and
then delays 20 milliseconds. The software then drives
PORTC pin 5 low and delays 80 milliseconds. The GOTO
command makes this a continuous loop. The results are
shown in Figure 4. I
captured the signal
on a digital scope
and then cut a
section of the
picture to show
only the waveform.
I then added the
upon the scope
settings. The grid is
at 25 milliseconds
per division. The
For years, I did work like this without an oscilloscope.
Much of my work was by trial and error, until I finally was
able to purchase a low-cost digital scope. Oscilloscopes
have come down in price as electronics have become less
expensive, but I have an even lower-cost option for you —
Microchip's PICkit 2 programmer. That's correct, I said the
PICkit 2 programmer, which I've suggested in previous
articles for following along with this column. The PICkit 2
tool is a great development programmer for PIC MCUs,
and now I have another reason to suggest you use it. In
fact, I have two additional reasons.
Back in my November '07 column, I showed how to
use the built-in UART Tool to capture data and send it to
Excel. That was one reason. The PICkit 2 designers added
another great tool called the Logic Tool to the interface
software that runs on your PC. The Logic Tool is a simple
three-channel logic analyzer. A logic analyzer is similar to
an oscilloscope, except that it just shows logic-level square
wave signals. Since that is what we are working with here,
I thought I would introduce this great feature. Figure 5
shows the same waveform as Figure 4, only as captured
on the PICkit 2 Logic Tool. (Figure 10 shows a picture of
the actual hardware, with dual programmers in action.)
The MPLAB IDE software has also been updated to
allow you to work with multiple PICkit 2 tools. The Logic
and UART Tools are entered through the PICkit 2's
■ FIGURE 3. Simple
■ FIGURE 4.
September 2008 17