standalone software. This means you might have multiple
programmers connected to your computer's USB ports,
along with the MPLAB IDE, and the PICkit 2 stand-alone
applications all running simultaneously; so this could
cause confusion on which tool is being used by the
MPLAB IDE and which one is used by the GUI standalone
application. When you open the stand-alone software, the
screen will ask which PICkit 2 you want to use, as seen on
the right side of Figure 6. I have two PICkit 2s connected
to my PC. Notice also in Figure 6 that they have unique
names. You can give each PICkit 2 a unique name
through the "Calibrate VDD and Set Unit ID…" selection
under the Tools menu. This is the same menu where you
select the UART or Logic Tool. This Unit ID name will then
get stored in the internal PICkit 2 memory so it will stay
with the unit. I also put a small piece of tape with the
number on the programmers case, so that I can keep
them straight.
To use multiple PICkit 2s with the MPLAB IDE, you
need to update to the MPLAB software version 8. 15,
which was recently released. You may also have to download the latest control software, depending upon how old
your current version is. The MPLAB IDE will ask you which
PICkit 2 it should work with. That window is also shown
on the left in Figure 6. Therefore, I can easily have three
PICkit 2s for many projects. One is used for programming
and debugging from the MPLAB IDE, one for UART
communication, and one for a logic analyzer. You can buy
three of these for far less than the cost of an oscilloscope.
If you need to monitor more than three signals, then you
can use even more of them in Logic Tool mode.
The Logic Tool has cursors built in, so you can
measure the pulse width. As you can see in Figure 5, the
X cursor is blue and the Y cursor is purple. You use the
left mouse button to set the X position and the right
mouse button to set the Y position. The screen shows the
time difference between them. The time measurement in
Figure 5 is the pulse width of our PWM signal, which
measures 20. 8 milliseconds — not 20, as the PAUSE
command created. The extra 0.8 is from the delay in the
HIGH and LOW commands. I used Channel 3 on the
PICkit 2 connector and the trigger was set to a rising edge
on Channel 3 in the Trigger portion of the Logic Tool
window. The red trigger line shows that the signal was
actually captured on the rising edge. As you may have
noticed, the Logic Tool is a great addition to the PICkit 2.
HARDWARE PWM
Now, back to the PWM explanation. Driving a PWM
signal is easy, but most of the time you’ll want to do other
things in your program, in addition to creating a PWM signal.
As soon as you try to do something else, you will be affecting
the signal timing. This is why many PIC MCUs have a hardware PWM built in. Some have more than one. The hardware
PWM runs on its own in the background once it is set up,
allowing you to write code to run separately in your main
loop. You can modify the setup for a particular duty cycle
or frequency in your main loop, but the hardware PWM will
continue running even when your main loop is running a
PAUSE command or some other delay section of code.
The PIC16F690 included with Microchip’s PICkit 2
Starter Package has a hardware PWM port on-chip. It’s
called the ECCP or Enhanced Capture Compare PWM.
The datasheet has a detailed explanation but, based upon
reader feedback, the datasheets tend to confuse the
beginner. I’ll try to simplify it here. Figure 7 shows the key
diagrams I captured from the datasheet to help me explain
the hardware PWM setup.
The block in Figure 7 labeled FIGURE 11-3 is the
block diagram of the hardware PWM circuitry. Though the
details are small, you can hopefully see that the operation
is based on the TMR2 (or Timer 2) within the PIC16F690
MCU. I added the block labeled FIGURE 7-1 because
this shows a key component left off of the PWM block
diagram — the TMR2 prescaler. This divides the oscillator
down, prior to feeding Timer 2 with its heartbeat.
The block labeled FIGURE 11-4 shows the definition of
the PWM output. You can see the diagram definitions are
similar to Figure 2, described earlier. The details to note in
this block are the equations for the end of the pulse width
and the end of the period. When the TMR2 value equals
the PR2 register value, the signal starts over. Therefore, the
PR2 value determines the frequency of the PWM signal.
The block labeled EQUATION 11-1 shows the formula for
calculating the PWM period. It’s a straightforward equation:
PWM Period = (PR2 + 1) * 4 * Tosc *
(TMR2 Prescale Value)
SETTING THE PWM FREQUENCY
■ FIGURE 5. PICkit 2 Logic Tool.
■ FIGURE 6. PICkit 2 selector windows.
Tosc is the period of the oscillator you are using to
run the PIC16F690 MCU.
Tosc is equal to 1/Fosc,
where Fosc is the oscillator
frequency at which the
PIC is running. PWM
period is also 1/PWM
frequency. You will typically
know the frequency you
want, but not the period.
Therefore, reworking the
formula to solve for PR2
is more useful:
18
September 2008