This is only a preview of the November 2015 issue of Silicon Chip. You can view 31 of the 96 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 "Open Doors With This Fingerprint Access Controller":
Items relevant to "A Universal Loudspeaker Protector":
Items relevant to "A Cheap Programmer For The PIC32 Microcontroller":
Purchase a printed copy of this issue for $10.00. |
Cheap programmer
for the PIC32
microcontroller
By Robert Rozée, M.E. (EEE)
Want to build a recent SILICON CHIP project that uses a PIC32?
Great . . . but the prospect of programming a PIC32 can initially
seem daunting. This minimal programmer will upload firmware
into a PIC32 quickly and simply at very low cost.
W
ITH THE DEMISE of parallel and
“real” serial ports on PCs, getting
code into Microchip’s PIC processors
has in recent years become more complicated and expensive. Way back in
the 1990s, you only needed a few resistors and a transistor to build a lowcost PIC programmer, and hobbyists
around the world spent many a joyful
hour creating interesting gadgets with
these little computers.
Sadly, those simple programmers no
longer work with today’s USB to serial
bridge adapters and a PICkit 2 or PICkit
3 became essential. Then there is the
large and somewhat complex MPLAB
X IDE (400MB download) needed to
drive Microchip’s programmer.
These are somewhat daunting requirements for a user looking to program a single PIC for the first time.
Compare this to the popular Arduino
platform. Every Arduino board comes
with a serial bootloader pre-installed,
hence code can be uploaded directly
via a serial or USB port without the
need for a specialised programmer.
The software to write, compile and
upload this code (the Arduino IDE)
is “only” an 80MB download. Which
was great – for a while.
But then Geoff Graham released the
Micromite – a complete BASIC computer on a PIC32 chip in a 28-pin DIP
(SILICON CHIP, May 2014). It is significantly more poweful than most Arduinos but the higher cost of programming
equipment is a drawback. While the
blank chip for a Micromite costs less
than US$5, uploading the Micromite
firmware required a programmer costing as much as 10 times more. That
isn’t a big deal if you plan to program
PICs frequently but it’s a bit expensive
as a one-off.
Hence a group of intrepid mem-
The PIC32 Processor Family
The PIC32 family of processors, made by Microchip Technology Inc, are powerful
32-bit system-on-chip (SoC) devices containing everything required to implement
quite complicated computers that are as powerful as the first IBM PCs. This from
a chip that is available in a 28-pin DIP, costs less than US$5, and can run from a
pair of AA cells.
A number of SILICON CHIP projects in recent years have made use of PIC32
devices, including: the Colour Maximite (September 2012), GPS Tracker (November
2013), Micromite (May 2014, January 2015), and Nixie Tube Clock (February 2015).
The Micromite (PIC32MX170 chip + 47μF capacitor and 10kΩ resistor) is
especially notable as it is programmed in BASIC, allowing the hobbyist to write
simple code to realise almost any project without need for any sort of development
environment beyond a serial terminal.
76 Silicon Chip
bers from “The Backshed Forums” set
about trying to create a cheaper, simpler alternative.
The end result
At the end of this (somewhat lengthy)
effort lay a new set of extensions to
pic32prog, an existing open source
(GPL) command-line utility written
and maintained by Serge Vakulenko. It works with a range of commercial PIC programmers – including the
PICkit 2 (but not 3). The latest release
of pic32prog is now also able to use an
Arduino Nano, using a protocol dubbed
“ascii ICSP”. Together with a few resistors and a 3.3V zener diode, this is sufficient to upload firmware to a PIC32.
The total cost? Less than that of a single PIC32MX170 chip.
The Arduino hardware and “ascii
ICSP” protocol are needed as an intermediate step. This is because, even
though a modern PC has lots of computing power, limitations in the way
USB is implemented prevent direct
access to the control pins (RTS, CTS,
DSR, DTR) of a USB-to-serial bridge at
any reasonable speed.
Early attempts that connected directly between serial port pins and the
target PIC32 resulted in programming
times of several hours or extreme unreliability, whereas with an Arduino
Nano in between, this time is cut to
just a few minutes.
The “ascii ICSP” protocol is very
simple, accepting single-character
siliconchip.com.au
USB CABLE
FROM HOST
ICSP TO
TARGET
3 x 100 Ω
D13
D12
+3.3V
D11
Vcc
D10
GND
USB PORT
AREF
A0
D9
A1
D8
A2
D7
A3
D6
A4
DISABLE RESET
FROM
USB PORT
D4
A6
D3
A7
3.3k
A
ZD1
3.3V
1W
PGD
PGC
NC
ZD1
A
D2
K
GND
RST
RST
GND
RxD
VIN
TxD
SERIAL
I/O
Fig.1: the circuit is based on an Arduino NANO module.
All you have to do is add a few resistors, a 3.3V zener
diode and some pin headers. Power is supplied from the
USB host (ie, a PC) via the Arduino Nano’s USB port.
LOW-COST PIC32 PROGRAMMER USING AN ARDUINO NANO
Hardware
The hardware is simple and cheap,
the most expensive part being an Arduino Nano, widely and cheaply available via eBay. Three 100Ω resistors and
a 3.3V zener diode create a switchable
3.3V supply for the PIC32 being programmed, capable of delivering up to
50mA ((5V - 3.3V) ÷ 33Ω) to the device.
This load is shared across three pins
on the Arduino, each capable of delivering 20mA.
Two more resistors (3.3kΩ) provide
pull-ups for open-collector outputs
that drive the two programming pins
on the target PIC32 – PGC (clock) and
PGD (data). These two pull-ups go to
the 3.3V supply – while the Arduino
Nano is a 5V device, most of the PIC32
siliconchip.com.au
VIN
29
IN
OUT
+5V
REGULATOR
GND
RESET/PC6
SCL
SDA
RESET
AREF
RESET
+5V
+5V
GND
4
17
+3.3V
D1
1
2
3
4
5
MINI
USB-B
20
TX
LED
VccIO
3.3V
OUT
λ
MOSI/PB3
CBUS1
23
16MHz
D+
7
8
RXD
TXD
ATMEGA
328P
(32TQFP)
22
USB
INTERFACE
16
D–
CHIP
15
SCLK/PB5
MISO/PB4
CBUS0
VUSB
RX
λ LED
5
1
TXD/PD1
A6
A5
A4
A3
A2
A1
A0
PB1
XTAL1/PB6
PB0
XTAL2/PB7
PD7
PD6
RXD/PD0
A7
PB2
22
19
28
27
26
25
24
23
PD5
ADC7
PD4
ADC6
PD3
ADC5/PC5/SCL
PD2
ADC4/PC4/SDA
TXD/PD1
ADC3/PC3
RXD/PD0
17
16
15
14
13
12
11
10
3
2
1
32
31
30
D13
D12
D11/
PWM
D10/
PWM
D9/
PWM
D8
DIGITAL I/O
POWER
ASCII commands from a host computer (running pic32prog) and converting these into the clock (PGC) and
data (PGD) signals needed to program
a PIC32. Lower case letters d, e, f, & g
cause clocked output of the bit pairs
00, 01, 10, and 11 respectively, while
upper case letters D, E, F & G clock out
the same pairs, then read back one bit.
Other commands carry 4-bit pairs
encoded as a single letter, assert and
release the reset (-MCLR) pin and turn
on/off the 3.3V supply to the target
PIC32. Between PC and Arduino, serial communications occur at a brisk
115,200 baud.
ANALOG INPUTS
SC
20 1 5
K
3.3k
D5
A5
+5V
DISABLE 328P
FOR USE AS
A SERIAL
BRIDGE
ARDUINO
NANO
MCLR
D7
D6/
PWM
D5/
PWM
D4/
PWM
D3/
PWM
D2/
PWM
D1/
TXD
D0/
RXD
ADC2/PC2
ADC1/PC1
ADC0/PC0
Fig.2: block diagram of the Arduino Nano module. It’s based on an Atmel
ATmega328P microcontroller chip and a USB interface chip.
pins should never have over 3.3V applied. No pull-up is fitted to the opencollector -MCLR (reset) output, as a
10kΩ pull-up should normally be present at the PIC32 end.
In operation, programming data is
exchanged with the PIC32 in a 4-phase
cycle. In phases 1 & 2, data (2 bits) is
written to PGD. In phase 3, the direction of PGD is reversed to allow a single data bit to be read back from the
PIC32, then in phase 4 the direction
of PGD is reversed again.
Uploading the 250kB of Micromite
firmware to a PIC32MX170 processor
involves sending over three million
November 2015 77
D13
USB
PORT
D12
+3.3V
D11
AREF
D10
D9
17
A0
25
ATMEGA
328P
A3
JUMPER
SHUNT
MCLR
PGD
D6
1
A4
D8
D7
9
A1
A2
D4
A6
D3
D2
A7
RST
100Ω
100Ω
100Ω
D5
A5
+5V
Fig.3: install the parts on the Veroboard and
connect it to the Arduino Nano as shown
here. Note that the tracks at the righthand
end of the board are all connected together.
ARDUINO
NANO
3V3
PGC
GND
RST
GND
RxD
VIN
TxD
Vcc
3.3k
3.3k
ZD1
GND
COUNTERBORE OR CUT ALL TRACKS
AT THIS COLUMN OF HOLES
characters out the serial port and receiving half a million.
Building it
The circuit is built on a small section of Veroboard attached to the side
of the Nano, with a 5-way cable running up to a standard 6-pin ICSP plug.
If desired, a LED and a 3.3kΩ series
resistor can be connected across the
3.3V zener diode to show when the
supply is turned on.
A 3-way header should also be fitted to the Arduino Nano board to al-
low linking its reset pin to ground or
+5V. If the Arduino’s reset pin is linked
to ground, the onboard ATmega328P
processor is disabled and the board
can be used as a simple USB to serial
bridge (via the “serial I/O” connector shown on the schematic). If reset
is linked to +5V, the firmware on the
Arduino is protected from being overwritten and the programmer will be detected more quickly when pic32prog
is launched. Do NOT install a jumper
on these pins yet.
Note: do not press the onboard re-
Typical Programming Session
C>pic32prog -d ascii:com5 mm47b32.hex
Programmer for Microchip PIC32 microcontrollers, Version 2.0.174
Copyright: (C) 2011-2015 Serge Vakulenko
(ascii ICSP coded by Robert Rozee)
Adapter:
Processor:
Flash memory:
Boot memory:
Data:
Erase:
Loading PE:
Program flash:
Program boot:
Verify flash:
Verify boot:
Program rate:
. OK1 OK2 - ascii ICSP v1E
MX170F256B
256 kbytes
3 kbytes
258692 bytes
(90mS) done
1 2 3 4 (LDR) 5 6 7a (PE) 7b 8 v0301
####################################### done
####### done
####################################### done
####### done
2086 bytes per second
total TDI/TMS pairs sent
total TDO bits received
total ascii codes sent
total ascii codes recv
maximum continuous write
O/S serial writes
O/S serial reads (data)
O/S serial reads (sync)
XferFastData count
10mS delays (E/X/R)
elapsed programming time
78 Silicon Chip
=
=
=
=
=
=
=
=
=
=
=
3147141 pairs
459064 bits
987417
157828
452 chars
95940
14354
10
58902
9/0/0
2m 06s
The tracks on the underside of the
Veroboard must all be cut as shown
in this photograph.
set button while the reset pin is linked
to +5V, as doing so will short the USB
port’s +5V output to ground.
Software
The only software required is the
pic32prog.exe executable itself (versions 2.0.174 or later) that can be downloaded from the pic32prog source repository at GitHub:
https://github.com/sergev/pic32prog
https://github.com/sergev/pic32prog/
blob/master/pic32prog.exe?raw=true
Note: the second link goes direct to
the Win32 executable. Mac OS X and
Linux versions are also available.
Drivers for the Arduino’s USB to
serial bridge may also be required for
Windows XP and earlier. In the case of
Chinese clones of the Arduino Nano
currently available on eBay, it’s likely
that the bridge device used will be a
CH430G, with drivers available from
the manufacturer: www.wch.cn/download/CH341SER_ZIP.html
The Arduino IDE is not required for
loading the “ascii ICP” firmware onto
the Arduino Nano, as this function is
performed by pic32prog internally.
Once the USB drivers have been installed, simply open a command window at the folder where pic32prog is
located. With the Arduino attached to
a USB port but no target PIC32 connected, type:
pic32prog -d ascii:com5 -b3
where com5 is the serial port assigned
to the Arduino, ascii is the name for
the class of programmer (“ascii ICSP”),
and -b3 tells pic32prog to upload the
“ascii ICSP” firmware to the attached
Arduino. This should display a message saying that firmware is being uploaded to the Arduino and will complete in a few seconds:
siliconchip.com.au
C>pic32prog -d ascii:com5 -b3
Programmer for Microchip PIC32
microcontrollers, Version 2.0.147
Copyright: (C) 2011-2015 Serge
Vakulenko
(ascii ICSP coded by Robert
Rozee)
57600 baud ... synchronized
Signature = 1e950f Device =
ATmega328P
######################
Firmware uploaded to ‘ascii ICSP’
adapter OK
If you see a row of dots after the
“57600 baud” and a failure message,
try repeating the process but this time
press the Arduino’s reset button while
the dots are being written. If this also
fails, try using -b1, -b2 or -b4 to select
different Arduino bootloader baud
rates. The vast majority of Arduino
Nanos use a bootloader baud rate of
57,600 but there may be a few that use
something different.
Note that the above step to upload
the “ascii ICSP” firmware to the Arduino Nano need only be performed once.
You should then install a jumper between the reset and +5V pins. You are
then ready to upload the Micromite (or
other) firmware to your PIC32.
Using it
Place the firmware you want to upload in the same directory as pic32prog
and open a command window at that
location. Connect the target PIC32 to
the programming hardware (Arduino).
The target PIC32 needs to have all
its ground pins connected together,
all VDD pins connected, a suitable
capacitor between the Vcap pin and
ground, and -MCLR pulled up to VDD
via a 10kΩ resistor. It is essential that
(+3.3V <at> 26mA)
ICSP FROM
PROGRAMMER
100nF
10k
1
MCLR
2
Vcc
GND
PGD
26
25
24
PGC
23
IC1
PIC32MX170B
PIC3
2 MX170B
22
21
9
VCAP
10
(TxD)
11
(RxD)
12
GND
20
MM CONSOLE TX
18
MM CONSOLE RX
17
16
14
47 µF
6V
15
AVSS
27
VSS
19
VSS
8
Fig.4: before programming, the target PIC32 microcontroller needs to be
wired in a manner similar to this example PIC32MX170B (check the pin
connections for your particular PIC micro).
no ground or VDD pins be left disconnected.
An example schematic for the PIC32MX170B 28-pin DIP is shown in
Fig.4, including both ICSP and Micromite serial terminal connectors.
The capacitor at Vcap should be a
low-ESR type, such as chip ceramic
or tantalum. If ceramic, the capacitor
can be a 10µF part.
To upload the PIC32 firmware, type:
pic32prog -d ascii:com5 filename.hex
where filename.hex is the name of the
firmware file. The programming hardware handles supplying power to the
target PIC32, turning the 3.3V supply
on before commencing programming
and turning it off when completed.
Programming should take a few minutes, after which you can disconnect
Useful Batch Files: Open a Command
Window & List Serial Ports
When using command-line utilities like pic32prog under Windows, there are a
couple of batch files that are extremely useful to have sitting in the same folder.
The first is a “command.bat” file, which, when double-clicked in a folder, will open
a command window at that location. The “command.bat” file is as follows:
<at>prompt $n$g
<at>cmd
The other useful batch file to have around is “ports.bat” to list the available
serial ports on a Windows PC. While there are more complicated solutions, the
simplest means of obtaining this information is from the Windows registry using
the following lines within the “ports.bat” batch file:
<at>reg query HKLM\hardware\devicemap\serialcomm
<at>pause
siliconchip.com.au
PGD
7
TERMINAL
Vcc
MCLR
3
6
NC
VDD
4
5
PGC
13
28
AVDD
the programmer from your PIC32.
On very rare occasions, pic32prog
can throw an error during programming – this is usually due to spurious behaviour within the target PIC32
triggered by electrical noise from the
programming PC’s power supply (especially if it’s a laptop). If this happens,
just reprogram the device a second time
and the error will almost certainly not
re-occur. Laptop computers can produce more electrical noise when running on AC power, so running off the
battery may be prudent if issues arise.
Conclusion
The combination of pic32prog and
Arduino Nano provide a simple and
cheap method for programming a
PIC32 chip, albeit a bit more slowly
than the PICkit 2/3 and without the integrated support in the MPLAB X IDE.
Acknowledgements
(1) “ascii ICSP” protocol and hardware
designed by Robert Rozée.
(2) “bitbang.c” extension for pic32prog
written by Serge Vakulenko and
Robert Rozée.
(3) The Backshed Forums: http://www.
thebackshed.com/forum/forum_topics.asp?FID=16
(4) Full details of the “ascii ICSP” protocol are available in the file ICSP_
v1E.ino at: https://github.com/sergev/
pic32prog/tree/master/bitbang
(5) PIC32 programming details are
from: “PIC32 Flash Programming
SC
Specification” (60001145N.pdf)
November 2015 79
|