http://www.iki.fi/hyvatti/pic/picprog.html
2010-04-14
PIC16, PIC12 and PIC18 microcontroller programmer for Linux and Windows/Cygwin.
Translations of some version of this document: Dutch
PICmicro microcontrollers, or MCUs, are fine chips that are especially easy to program with a simple device attached to a parallel or serial port. Because of the EEPROM or Flash memory, they are also easy and fast to erase and reprogram without need for UV equipment. This makes them very popular among electronics hobbyists.
At the moment this is the second implementation of a PIC programmer for Linux that works with the very simple and cheap serial port programmers. The first one I know was made by Ralph Metzler in 1996. My programmer was originally designed for PIC16C84 and PIC16F84 chips back in 1997, and since then I have implemented other chips without access to most of them. I have tested PIC16F628, PIC16F676, PIC12F675, PIC16F88, PIC16F876A, PIC16F76, PIC18F1320 and PIC18F458. Others have used many other models. The dsPIC30 family has some code in the sources, but the support is not finished yet. Some 12 bit chips should be supported but I have not tested that support.
Maybe the best source for PIC information is the home page of PICLIST discussion group. Also historically a good collection of links and software for PIC was in David Tait's PIC links page and in GNUPIC pages. I have also documented here the software I took a look at back in 1997. I have focused into Linux support, so I have never used any DOS software mentioned.
The description below is based on old version of JDM hardware. As you can see above, there are better designs by Jens Madsen and others available, please use them instead. Note however that for example the latest Jens Madsen PCB does not support PIC16F628A that needs to have pin 10, RB4/PGM grounded. Please modify the PCB to ground this pin if your chip has PGM on pin 10.
I made a minor modification to the jdm84v23 schema and pcb mask, because I thought D4 was stressed on positive clock pulses - it short circuits the rs-232 RTS pin to GND. I added a 10k resistor between D4 and D3. But it also is not absolutely required, as the clock pulses are short and rs-232 is protected for short circuits anyway. Now I have noticed that this resistor makes the programmer less reliable, especially with later PIC chips, like PIC16F76 and PIC18 family. If you have built the hardware I previously suggested, and it does not work, please short circuit that resistor or build the new PCB from Jens Madsen site. Also you can try this patch by Matthijs Kooijman to test the levels of signals: picprog-slow-test.diff . This is for an older version of picprog, so it does not apply to current code, but you get the idea.
To support for example PIC16F628 and PIC16F88, which have a low voltage programming mode, the circuit was again modified to ground the RB3 pin 9 and the RB4 pin 10. This prevents the chip from entering the low voltage programming mode. To support for example PIC12F675, PIC16F630, and PIC16F676, pin 1 was connected to Vdd. Other jumpers need to be installed as described below.
To support for example PIC16F876A and 16F76, which have different pinouts for programming signals, you need an adapter that connects to the external connector of the above programmer. Alternatively you could redesign the pcb layout, but maybe it is easier to just solder the adaptor. Solder the pins like this:
pcb 1 -- clk -- 27 (RB6) ic connector 3 -- data -- 28 (RB7) socket 5 -- Vss -- 8,19,24 7 -- Vdd -- 20 9 -- Vpp -- 1
To support for example PIC12F675, PIC16F630 and PIC16F676, which have different pinouts for programming signals but do not conflict with PIC16C84 pins, you can solder jumper wires on the above pcb. You need to connect pin 16 on the socket to clock (same as pin 12), pin 17 to data (same as pins 11 and 13), and pin 18 on the socket to Vss (same as pins 5-10). Note that pin 1 is already connected to Vdd in the above PCB mask. If this connection is missing on your older PCB, connect pin 1 to pin 14 with a wire.
Always check the correct pinouts for your chip from the datasheets also!
For support for 3.3 volt in-circuit programming, see the modified circuit by Krüpl Zsolt, Hungary. The base of the transistor is moved from +5V to +3.45V with the added 22k resistor to PIC ground.
There are binary packages available that have been prepared by helpful users and operating system vendors. I know of the following, but cannot offer any support for them. They might be for older versions, but even if they are, check for the 10. Changes section below if you really need a later version. Some new versions of Picprog only fix small specific bug, which may not affect you at all.
To install from source, download the picprog-1.9.1.tar.gz package, if you do not already have it.
Check your system against the requirements mentioned above.
Untar the archive and change to the source file directory. You should only have to type:
make dep makeand the program should compile without errors or warnings. If it does not, please check that your compiler, c and c++ libraries and utilities like make are of a reasonably recent, bugfree and compatible version.
After compilation you can, as a root user, just type:
make installto install the program and manual page to /usr/local. Or just copy the files
picprog
and picprog.1
manually.
Make sure that you have access to the serial port device like /dev/ttyS0 or /dev/ttyS1 with the user that you are running the program as.
The Linux emulator for Windows, Cygwin, allows Picprog to be compiled on Windows plattforms.
To install Cygwin, go to www.cygwin.com and follow the instructions there. In short, download setup.exe and run it. You need to install at least the Developer packages Gcc C compiler, Gcc C++ compiler, Make, and Binutils in addition to the default install.
To install Picprog on Cygwin, follow the above instructions for Linux compilation from source.
Windows 2000 and XP installations that I have tried work fine. Windows 98 installations worked sometimes, and I suppose the timing routines of Picprog do not work well on Windows 98, they even lock the computer sometimes. I suggest using Windows 2000 or later.
If you are using USB serial adapters, they show up as /dev/ttyS13 or higher numbers. Try different numbers if that does not work.
--warranty
, --copyright
, --help
--quiet
, -q
--input-hexfile
and --output-hexfile
. If
the former is present, the program acts as a burner. If the
latter is specified, the program will read the contents of the PIC
device eeprom memories. Both may be specified on the same command
line, in which case the chip is first programmed and then read.
picprog --output saved-cal-chip1.hex --skip-ones --pic /dev/ttyS1
picprog --burn --input file.hex --pic /dev/ttyS1
picprog --erase --burn --input file.hex --pic /dev/ttyS1
--erase
--burn
--input-hexfile
path, -i
path
--cc-hexfile
path, -c
path
--force-calibration
--pic-serial-port
device, -p
device
/dev/ttyS0
.
Environment variable PIC_PORT
can be also used to specify
the serial port. Use /dev/ttyUSB0
in Linux or
/dev/ttyS13
in Windows/Cygwin for USB serial adapters.
--device
chipname, -d
chipname
PIC_DEVICE
can also be used to specify device. Currently supported by code are:
I do not know if all the chips work or if any other than pic16c84,
pic12f675, pic16f676, pic16f76, pic16f88, pic16f876a, pic16f628,
pic18f1320, and pic18f458 work, these I have tested myself. Default
is to autodetect the device by reading configuration memory location
0x2006. If no device id is present, the default is
pic16c84
. If reading location 0x2006 with 14 bit
programming algorithm fails, the PIC18 programming algorithm is used
to read configuration memory locations 0x3ffffe and 0x3fffff. To add
a new supported chip type, just edit the table in file
hexfile.cc
.
This example is for pic18f1320. These addresses are byte addresses. IHX32 is the only option for saving PIC18 family programs in hex files.
Before interfacing with the PIC chip, Picprog calibrates its delay loops by checking the clock speed of the CPU and whether the CPU supports the TSC feature. On Linux, /proc/cpuinfo is read. On Windows/Cygwin, /proc/cpuinfo is read and CPU clock frequency is estimated. Therefore the clock frequency displayed on Windows is not necessarily exactly the true clock frequency.
Long cables, different values on capacitors and resistors, and differences on sertial ports can cause very long signal rise and fall times. The JDM device is sensitive to that, and mostly is designed to be connected directly to computer or with very short cable. The sensitivity also depends on the PIC chip type. It may help to stretch the delays in Picprog to allow for longer signal settling times. Larger delays are needed also for the uJDM programmer device. A couple of options are available. They can be tried individually and together:
--rdtsc
--nordtsc
--slow
--reboot
--k8048
--jdm
Old patches to solve the timing problem were made available by Werner Almesberger: picprog-1.7-werner-almesberger.diff, and by Matthijs Kooijman: picprog-slow-test.diff.
picprog --output ofile.hex --pic /dev/ttyS1
--output-hexfile
path, -o
path
--ihx8m
or
--ihx32
options. For PIC18 family devices, file will
be written in IHX32 format.
--skip-ones
--ihx32
, --ihx16
, --ihx8m
--pic-serial-port
device, -p
device
/dev/ttyS0
.
This document has not changed much since it was first released with the 1.0 programmer. The changes include some information about new software and more accurate links to PIC information. New options to select type of device other than pic18c84 are also present.
2003-08-10 version 1.2
With help from Taneli Kalvas changed the schemantics diagram and pcb
mask to ground the RB4 pin, selecting high voltage programming on for
example pic16f628.
Implemented preservation of OSCCAL and other calibration data.
Added automatic detection of devices based on location 0x2006.
Merged Bart Goossens's changes to implement PIC16F73. I hope it works.
2003-08-21 version 1.3
Autodetect more chips. Fix programming of chips with OSCCAL. Fix
erasing some chips - erasing and resetting code protection is now
performed the hard way: all methods are tried regardless of chip type.
--erase
now works also without
--input-hexfile
flag.
2004-01-02 version 1.4
Add option --force-calibration
to program OSCCAL and BG
bits. Implement programming algorithms for 16f87/16f88 and 16f87Xa.
Revise some timings on programmer reset to avoid operating voltage to
dip. More verbose output on how many locations actually were burned.
2004-03-02 version 1.5
Fix PIC16F87xA configuration word burning. Remove the 10k resistor
added by me from the PCB and schema. Add support for PIC18 family.
Make the DTR be held low as long as possible. This may improve the
reliability and limit stress on RS-232 port.
2004-03-19 version 1.6
Now compiles on Windows with Cygwin DLL version 1.5.8 and later. More
accurate timings result in shorter programming times compared to
previous versions. These timings use the CPU RDTSC instruction on x86
and AMD64 plattforms. PIC16F87/88 second configuration word
programming implemented.
2004-04-28 version 1.7
Fixed 16c OTP and UV erased parts EPROM programming to use 100µs
programming/overprogramming pulses. Use real time priorities and
nanosleep() for delays if run under root priviledges. Relax timings
so that they work out of the box with longer cables. Fixed PIC12F629
/ PIC12F675 / PIC16F627a / PIC16F628a / PIC16F648a / PIC16F630 /
PIC16F676 programming with >1.3GHz i386 CPU.
2006-03-05 version 1.8
Experimental dsPIC support, never tested, just compiled. Disabled the
real time priorities, the nanosleep function does not work any more in
Linux 2.6 kernels as it used to. Exit value 76 added to indicate
unsupported chip id. Added more PIC18F models, thanks to Jan Wagemakers from Belgium.
2006-03-26 version 1.8.1
Fix a mistake with 18f[24]5[128]5 and 18f[24]6[128]0. Thanks to
Jan Wagemakers for the fix.
2006-09-28 version 1.8.3
Added --rdtsc, --nordtsc and --slow options. They may help with
laptops and new faster models of CPUs. Made --nordtsc the default.
This was originally release 1.8.2 that did not have that default.
2008-06-05 version 1.9.0
New chips defined, including 12 bit parts, thanks to Renato Caldas,
Kevin Buettner, Alexander Zangerl and Jan Wagemakers. Chip presence
is no more detected with start/stop bit checking, as some chips do not
clear them (see Debian bug #412778). Microcontroller is now powered
off before any operations with --reboot
option, thank you
for the idea to Mauro Giachero. Some code changes remove duplicate
port initialization. Timings have been changed completely, hopefully
this fixes the erratic behaviour with some chips. Thank you for
testing and ideas to Mauro Giachero and Jan Wagemakers. Added
environment variables PIC_PORT
and
PIC_DEVICE
, thanks to monttyle. Added --k8048 option for
Velleman K8048 with reversed signals, thanks to Bob Dunlop and Neil
Turton.
2010-04-14 version 1.9.1
Fixed a bug that prevented erasing chip with OSCCAL words. Thank you
for Eckhard Neber for finding this. Read also short hexfile data
lines, thanks to Jacques Klei, Salie Adams, Redzinalds Knipsis and
Jani Turkia for pointing this out. Charge capacitors a little longer,
suggested by Bernard Hatt. Added some NOP commands to PIC18F
programming sequence, suggested by Marco Mattila. Fixed warnings with
current compilers. Changed license to GPL3. Thanks to Mario Castelán
Castro for reminding about that.
I modified the circuit to include connections to pins that are needed for programming some PIC microcontrollers.
There is a new version of the PCB available on Jens Madsen site. It supports more chips without jumper wires: PIC-Programmer 2.
Picprog was first written and released in May 1997. Around that time I briefly experimented with microcontrollers. I found no Linux software for the cheap serial programmer hardware by Jens Madsen, and I wanted to use that one as it was so simple to build. Only later did I learn about serp-0.5. Anyway, that one directly programmed PC style serial port hardware while I wanted to use standard UNIX methods of accessing serial ports. Linux was missing an IOCTL to force BREAK condition (steady +12V) on the serial data transmit line. This kind of functionality existed for example in Solaris. No problem, I created a patch for Linux kernel versions 2.0.30 and 2.1.42, submitted it, and it was included in mainline kernel versions 2.0.32 and 2.1.45.
Since the time I first wrote the software I did not work with microcontrollers at all for years, though I maintained Picprog by fixing obvious compilation problems and updating documentation. Version 1.0.1 was put together in May 2001 and included mainly documentation fixes. In June 1997 I had worked on adding support for different memory sizes of different PIC chips, and these changes and again documentation updates were released as version 1.1 in February 2002. I also found the programmer hardware I thought I lost a few years back, and was able test that it still works.
Picprog-1.0 was ported to FreeBSD and included in the distribution around September 1999. MIT MASLab 6.186, a student-run robotics course, seems to have used it since January 2001. Recently this documentation page has attracted steadily over 1000 visits per month, so I guess someone is finding it useful.
Nowadays I mostly test Picprog with new chips, and sometimes I start a new project like KanSat satellite or 8-PIN PONG, and never finish them..
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/ .
The author may be contacted at:
Email: Jaakko.Hyvatti@iki.fi
URL: http://www.iki.fi/hyvatti/
Please send any suggestions, bug reports, success stories etc. to the Email address above. To avoid my spam filters, please put the word 'picprog' somewhere on the subject line.