This is only a preview of the July 2010 issue of Silicon Chip. You can view 28 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "An Intelligent 12V Fan Controller":
Items relevant to "PICAXE Project Development System":
Items relevant to "Lab-Standard 16-Bit Digital Potentiometer":
Items relevant to "Dual-Tracking ±19V Power Supply, Pt.2":
Purchase a printed copy of this issue for $10.00. |
Programming PICs:
How It’s Done
Many SILICON CHIP projects include a PIC
microcontroller as the central component.
But how do you program the PIC if you’re
not buying a kit or if you want to upgrade
the firmware to a later version? Here’s a
step-by-step guide to doing it yourself.
By MAURO GRASSI
F
OR MANY ELECTRONICS enthusiasts, programming the PIC
micros that are now used in so many
SILICON CHIP projects is something of
a mystery. If you buy a kit, it usually
doesn’t matter because the micro will
be supplied pre-programmed. Alternatively if you’re building a project from
scratch, then you’ve either got to pay
someone to do it for you or learn how
to do the job yourself.
A similar problem arises if the PIC
fails and has to be replaced or if you
want to update the firmware to a later
version. It’s not uncommon for the
firmware to be revised after a project
has been released, usually to add
extra features but also sometimes to
fix any bugs.
Fortunately, programming PICs
is straightforward although you do
require a low-cost PIC programmer
(more on this shortly). By following the
simple steps outlined in this article,
you will be able to program almost any
Microchip microcontroller, including the popular 16F, 18F, dsPIC33F,
PIC24F and PIC32 families.
A few basics
PIC microcontrollers have onboard
non-volatile memory (FLASH) that
stores the program code (some also
have onboard EEPROM for storing
persistent data). This program code
is referred to as the “firmware”. The
term “non-volatile” simple means that
the firmware remains in memory even
when the power is turned off.
Programming (or reprogramming)
can be done in at least two ways: (1)
through “ICSP” (In Circuit Serial Programming) or (2) through “RTSP” (Run
Time Self Programming). The latter
option requires a “bootloader” program
to be running on the target device. A
bootloader is a separate program that
can write a new firmware image onto a
microcontroller. The firmware image is
supplied to the bootloader usually via
Table 1: Programming & Power Supply Connections
Line
Function
Connect To PICKit3 Line
PGD or PGED
Programming Data Line
Pin 4 (PGD)
PGC or PGEC
Programming Clock Line
Pin 5 (PGC)
Bar-MCLR/VPP
Programming Voltage
Pin 1 (bar-MCLR/VPP)
VDD
Device Voltage
Pin 2 (VDD)
VSS
Device GND
Pin 3 (GND)
AVDD
Analog Device Voltage
Pin 2 (VDD)
AVSS
Analog Device GND
Pin 3 (GND)
26 Silicon Chip
a USB or serial connection, or from some
other storage medium (eg,
a memory card).
In most cases, however, a bootloader will not be used. It adds
complexity to the code and the
bootloader itself needs to be initially
programmed. As a result, many micros
are programmed using ICSP.
Some projects include the necessary
ICSP interface as part of the design (ie,
it’s incorporated into the PC board).
Otherwise, you will need to remove
the PIC micro from its socket and
program it externally.
What’s needed
Assuming you have your target
device (ie, the PIC micro), you will
need the following: a PIC programmer, a PC with suitable software and
a breadboard if your programmer does
not have a ZIF (Zero Insertion Force)
socket.
The programmer we recommend
to start with is the PICKit3, which
is a low-cost USB programmer (and
debugger) from Microchip. There are
other more expensive programmers
available and they usually have advanced features that the PICKit3 lacks,
such as bulk programming and better
debugging options.
That said, the PICKit3 is suitable for
all hobby work and for prototyping
and development. It’s portable, inexpensive and can even provide power
siliconchip.com.au
11
32
Vdd
100nF
10k
Vdd
MCLR
(ICSP)
1
1
2
3
40
IC1
PGD/RB7
PIC18F4550
39
PGC/RB6
Vss
SC
4
PGD
PGC
AUX
Vss
12
2010
Vdd
GND
5
6
NC
Vpp/MCLR
31
PROGRAMMING EXAMPLE
Fig.1: this diagram shows a typical connection to a
PIC18F4550. The pin-outs for different devices will
vary and you should consult the relevant data sheet.
to your target board (through the USB).
The PICKit3 can program almost
all Microchip microcontrollers and
its firmware can be upgraded to enable support for future devices. The
device itself is based on a Microchip
microcontroller and it can update its
own internal firmware using RTSP.
This is an important feature because
the programming algorithm for a future
device may change (different Microchip microcontrollers have different
programming requirements).
No ZIF socket
The PICKit3 does not have a ZIF
socket. Instead, you connect its three
programming lines plus two power
supply lines to the target PIC. That’s
five lines in all and this is simple to
breadboard up on a prototype board.
The programming lines are PGC,
PGD and bar-MCLR/VPP. PGC is the
clock line, PGD is the data line and
bar-MCLR/VPP is the reset (Master
Clear)/Programming voltage line. Note
that these lines are common to all
Microchip micros, so if you are using
a different programmer, it also will
have these lines.
Fig.2: this window from MPLAB shows the which programmers support a selected device (here, a PIC18F4550). A green dot indicates that the device is supported.
lent) does not have a ZIF socket, so a
breadboard adaptor is used to connect
to the target device. The PICKit3 has
six lines (of which five are used) and
these can be accessed using a single
inline 6-way pin strip.
In our case, we made a custom PC
board to accept this pin strip (see photos) but it can just as easily be made
up on a small piece of Veroboard (0.1inch grid). As shown in the photos, the
6-way pin strip is soldered to the top
of the board.
Two additional pin header strips are
then soldered to the underside of the
board – one to make the connections
between the PICKit3 programmer and
the breadboard and the second to ensure that the assembly is stable when
the PICKit3 is connected.
Step 2 – Making The Hardware Connections: install the PIC micro (henceforth
referred to as the “target device”) on
the breadboard, then download its
datasheet from the Microchip website.
TOP VIEW
We are assuming here that you have
a DIP (dual in-line package) device.
The datasheet contains the pinout
information you will need to connect
the programmer to the target device.
Although there is variation in the
pinouts across and within Microchip
families, the lines listed in Table 1 (if
present) must be connected.
Note that, depending on the device,
these lines may be labelled differently.
For example, some devices have multiple PGC/PGD pairs, which are then
labelled with a numerical suffix (eg,
PGED1, PGEC1).
If there is more than one pair, any
pair can be used for programming.
However, only one can be used for
debugging (this is set by writing the
configuration words of the microcontroller). We are not going to cover
debugging using the PICKit3 here.
A typical programming connection
diagram (ie, device to PICKit3) is
shown in Fig.1. It uses a 10kΩ resistor
BOTTOM VIEW
Step-by-step programming
Let’s now go through the programming procedure step-by-step. We’ll
assume that you’re using a PICKit3
programmer and a breadboard.
Step 1 – Making The Breadboard Adaptor:
as mentioned, the PICKit3 (or equivasiliconchip.com.au
A 6-way pin header is required to connect the PICKit3 to the breadboard. You
can make one up on a small scrap of Veroboard.
July 2010 27
The PICKit3 programmer is available from
Microchip Direct – www.microchip.com/
The catalog number is PG16430) and the
price is $US44.95 plus postage.
This photo shows how the PICKit3
is connected to the breadboard
(and to the target device) via a pin
header socket.
Fig.3: this “Output” window shows the PICKit3’s startup sequence. The error
given means that the PICKit3 did not recognise any device, in this case because
power had not yet been applied.
Fig.4: when power is applied to the target device via the PICKit3, it recognises
the device and shows its ID revision number. This also indicates that commun
ication between MPLAB and the target device (via the PICKit3) is good.
to pull up the bar-MCLR/VPP line to
the supply rail VDD and some 100nF
bypass capacitors on the supply lines.
Avoid using capacitors on the VPP,
PGC and PGD lines, as these will affect the high-speed digital signalling
on these lines.
In addition, the PGC and PGD
lines have internal pull downs in the
PICKit3, so don’t use pull ups on these
lines. As shown in Fig.1, they are con28 Silicon Chip
nected directly to the PICKit3.
Note that the supply rail for different microcontrollers will be different. Usually devices with an “L” in
the part number are low voltage (eg,
18LF1320).
The power for programming can
be supplied by the PICKit3 itself but
you have to enable the power output
separately, as described later. For now,
it’s just a matter of connecting the
relevant pins on the target device to
the adaptor on the breadboard using
wire jumpers.
Note that the PICKit3 can supply
up to 30mA at between 1.8V and 5V
to the target device, which should be
sufficient for programming (note: this
may be insufficient for debugging if
using the PICKit3 as the only power
source). Note also that no separate
system clock is required for programming – the clock signal supplied by
the host (ie, the PICKit3) via the PGC
line is sufficient.
Step 3 – Installing PC Software: suitable
PC software is required to drive your
programmer. Many programmers are
supplied with their own software
while the PICKit3 uses MPLAB. This
IDE (Integrated Development Environment) is free and can be downloaded
from the Microchip website at www.
microchip.com
MPLAB is a 32-bit Windows application but will also run on 64-bit
Windows versions. The screen grabs
shown in this article are from MPLAB
v8.50. Future versions may be different.
Step 4 – Connecting The PICKit3: the
PICKit3 connects to your PC using
a USB cable (supplied with the programmer). Assuming you have already
installed MPLAB, the driver will be
found and installed automatically as
soon as the PICKit3 is connected.
Once that is done, you can connect
the PICKit3 to your target device using
your breadboard adaptor. Note that
Pin 1 of the PICKit3 is indicated by a
white arrow (see photo).
Step 5 – Programming: once the connecsiliconchip.com.au
tions are complete, MPLAB is used
for programming via the PICKit3 (it
can also be used for development
and debugging). You will need the
new firmware program for the target
device and this is usually supplied
as an Intel HEX file (extension .hex).
The programming steps are as follows:
• Start MPLAB, then go to Configure
-> Select Device and select the correct
device type. For example, if you are
programming a PIC18F4550 microcontroller you need to select it here.
Whether or not PICKit3 can program
your device will be indicated by either
a green or yellow dot (see Fig.2). A
green dot means that the target device
is supported, while a yellow dot means
beta (ie, not fully tested) support. A red
dot means the device is not currently
supported and you won’t be able to
use the PICKit3 to program that device.
• If the device is supported, enable
the programmer by going to Programmer -> PICKit3. This will bring up a
dialog box similar to Fig.3.
• Enable the PICKit3 to provide
power for your device. To do this, go
to Programmer -> Settings, click the
Power tab and check that the correct
voltage for your target device is indicated in the voltage group box. Usually
MPLAB will set this to the default for
the selected device. If there is a range
of valid voltages, the lowest will be
selected.
If the voltage is incorrect, change it
to the correct value.
Now click the “Power Target Circuit
from PICKit3” checkbox to enable the
PICKit3 to power the device and click
OK. If all is well, the PICKit3 will recsiliconchip.com.au
Fig.5: this screen grab from MPLAB shows the contents of the program memory.
The HEX file is parsed and the program memory is then loaded with the firmware image.
Fig.6: the “Output” window now shows the result of programming the device, in
this case a PIC18F4550 device. MPLAB will program and verify the image. The
“Programming/Verify complete” message indicates all is OK.
ognise the device, as shown in Fig.4.
Here, the last line shows the Device ID
(Silicon) revision, indicating that communication between your PC and the
target device (via the PICKit3) is good.
• Go to File -> Import. An Open File
Dialog will appear and select the “All
Load Files” in the Files Type field.
This includes the “.hex” file extension.
• Navigate to the new firmware file
and open it. MPLAB will now decode
the HEX file. You can see the contents
of the memory by going to View ->
Program Memory, as shown in Fig.5.
• Finally, go to Programmer -> Program to program the HEX file to your
target device. MPLAB will erase the
device, then program it and finally
verify the image. If all goes well, you
should see a dialog as shown in Fig.6.
Your device is now programmed and
SC
probably running.
July 2010 29
|