Programming the PIC16F84 with LINUX

This document started it's life as http://www.iki.fi/hyvatti/pic/picprog.html  thanks to Jaakko Hyvatti.  It is intended to document how I am using Linux to program my PIC16F84 for usin in several other projects.  If you using other hardware, don't write me. But, if you have questions about what I'm doing... markg@okcofurm.org.

The PIC16F84 is a fine chip that is especially easy to program with a simple device attached to a parallel or serial port. Because of the EEPROM memory, it is also easy and fast to erase and reprogram without need for UV equipment. This makes it very popular among electronics hobbyists.
 

Where to Start

1. Background reading

You really should take a look at the Microchip www-pages and read the pic16c84 datasheet (30445b.pdf) and programming specification (30189d.pdf) there.

I also recomend the book "Easy PIC'n'" by Square 1. This book explains everything in simple language.

A good collection of links and software for PIC is in David Tait's PIC links page and in Rick Miller'sGNUPIC pages.

2. Requirements

Linux System kernel version 2.0.32 or 2.1.45 or later.
This programmer needs some functionality in Linux serial driver that as of kernel versions 2.0.32 and 2.1.45 is available in standard kernels. The programmer uses TIOCSBRK and TIOCCBRK ioctl to control the state of TxD serial port output accurately. These ioctl's are standard on BSD flavor unixes, like SunOS 4, but they still are unimplemented on many serial drivers in Linux kernel. Only the standard serial port is known to work by me.
Serial port PIC16F84 programming hardware
Most PIC programers use paralel port for their programing.  I have turned of my paralel port to gain another interupt.  Luckuly, I have a multi serial port card so I have lots of serial port to connect a programer to. See hardware below.
Linux PIC programing software
At the moment I am using  the picprog-1.0.tar.gz package. This is the second implementation of an PIC programmer for Linux that works with a serial port programmers. The first one was made by Ralph Metzler.
Pic Chips
I purchases my chip from Digi-Key. In quanity they have a good price.  I orded 25.
A compiler for PIC
Your assembler, or C compiler, or whatever, should produce either Intel IHX16 or IHX8M format hex files. For assembler I recommend picasm106 by Timo Rossi.
Pic Programs
If you are not going to write your own program or you have a project that just needs to be programed.  There are a couple of programs that come with picasm you might want to check out.

3. Hardware

Use a serial port programmer device with the following pinouts:
TxD - Programming voltage, pin /MCLR
RTS - Clock pulse, pin RB6
DTR - (output), CTS (input) Serial data, pin RB7
A very good programmer like this is for example jdm84v23 designed by Jens Madsen, Jens.Madsen@post3.tele.dk. This programmer doesn't require any power supply but it should be pluged directly into your serial port because it gets it voltage from the port. (Don't user a cable!)

The schemantics diagram:

jdm84new.gif

The 300 dpi pcb mask:

jdm84pcb3.gif

A picture of a this programmer point to point wired.

4. Installation

First, if you have not yet done so, download the picprog-1.0.tar.gz package.

Check your system against the requirements mentioned above. Compile and install the kernel with the serial driver ioctl patch, if the kernel version you use does not yet support the TIOCSBRK interface.

Untar the archive and change to the source file directory. You should only have to type:

make
and 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. I use glibc-2.0.2, make-3.75, linux-2.0.30 and gcc-2.7.2.1.

After compilation you can, as a root user, just type:

make install
to install the program and manual page to /usr/local. Or just copy the files manually.

5. PicProg Usage

To get information about the usage of the program, just type the program name. These options give information about the program:
--warranty, --copyright, --help
Display warranty or copyright information or the help text.
--quiet, -q
Do not display the copyright notice.
The actual operation of the program is controlled by the options --input-hexfile and --output-hexfile. If the former is present, the program acts as a burner. If the former 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.

6. Burning PICs

Simple instructions:
  1. Compile your program into a hex file.
  2. Insert the pic16c84 chip into the socket in the programmer, or connect the in-circuit programming cable to your device.
  3. Connect the programmer device to a serial port. If your programer is not self powered plug it directly into your comptuer.
  4. Burn the program with command:picprog --burn --input ????????.hex --pic /dev/ttyS??
  5. If the above command produces error output that suggests that the chip was in the code protection state, retry with the following command:picprog --erase --burn --input ????????.hex --pic /dev/ttyS??
The burning options are:
--erase
To be able to reprogram a PIC device that has previously been programmed into Code Protection state (Control Word fuse bit 0x10 cleared), it is necessary to bulk erase the chip. It is done by adding this option to the command line. The default is not to bulk erase the chip.
--burn
Actually program the device. Without this option only the syntax of input files and command line options is checked.
--input-hexfile path, -i path
Specifies the input hex file. The file can be either in IHX16 or IHX8M formats, the format is automatically recognized.
--cc-hexfile path, -c path
Only necessary for debugging. Outputs the same data as was read from the input hex file.
--pic-serial-port device, -p device
The device name of the serial port the programmer is connected to. Default is /dev/ttyS0.
The hex file addresses (in IHX16 format) used are the ones specified by Microchip:
0x0000-0x03FF
Program memory, 1024 words * 14 bits.
0x2000-0x2003
ID locations.
0x2007
Control word fuses
0x2100-0x213F
Data memory, 64 bytes * 8 bits.

7. Reading PICs

Simple instructions:
  1. Insert the pic16c84 chip into the socket in the programmer, or connect the in-circuit programming cable to your device.
  2. Connect the programmer device to a serial port.
  3. Read the device with command:picprog --output ????????.hex --pic /dev/ttyS??
The reading options are:
--output-hexfile path, -o path
Specifies the output hex file. The file will be written in IHX16 format, unless otherwise specified by the --ihx8m option.
--skip-ones
When reading the PIC device, do not consider the all-ones memory locations to be programmed, and skip them in the hex file output. This skips the program memory locations that have hex value 0x3FFF and data memory locations that have hex value 0xFF.
--ihx16, --ihx8m
Select the output hex file format to be either ihx16 or ihx8m, respectively. The default is ihx16.
--pic-serial-port device, -p device
The device name of the serial port the programmer is connected to. Default is /dev/ttyS0.

8. Internals

Source files and their contents:
picport.cc, .h
class picport: manipulates the serial port hardware. With this class you can execute programming commands like read a word, program a word, increment address etc. Look at picport.h for details.
hexfile.cc, .h
class hexfile: contains a PIC memory image. You can load and save the contents of this class to a file, and you can program and read it from the PIC chip. Programming uses class picport.
program.cc, h
class program: just some generic option handling.
stringc.cc, .h
class string: generic string class
main.cc
Just the main () to parse command line and call class hexfile to do its job.

NOTES:

This document has been revised after first released with the 1.0 programmer. The changes include some information about new software and more accurate links to PIC information. Also information about Linux kernels has been updated - the current kernels no more need a patch to implement the necessary ioctl functionality.

Other available programmers

serp-0.5 (serp-0.5.tgz)
A serial port programmer software for Linux, written in c++, author Ralph Metzler, rjkm@thp.uni-koeln.de. It directly handles the serial hardware, standard 16450/16550 compatible uarts, and needs root priviledges for that.
jdm84v23 (jdm84v23.zip, pgm84v23.zip)
A serial port programmer, schema (gif) and DOS software. The hardware manages with rs-232 interfaces with low voltage output, even as low as ±7V is fine. 300dpi layout and PCB masks available at the home page. Author Jens Madsen, Jens.Madsen@post3.tele.dk. This is the programmer I use with linux with my own software.
Jaakko Hyvatti did a minor modification to the jdm84v23 schema and pcb mask, because he thought D4 was stressed on positive clock pulses - it was short circuiting the rs-232 RTS pin to GND. He added a 10k resistor prevent this. I believe it does not do any harm. But it also is not absolutely required, as the clock pulses are short and rs-232 is protected for short circuits.
pip-02/com84 (pip-02.zip)
A serial port programmer, schema (gif) and DOS software. Needs +12V rs-232 positive voltage level.
prog84-0.9 (prog84-0.9.tar.gz)
A parallel port programmer, schema (postscript) and software for Linux, Written by Wim Lewis: wiml@netcom.com or wiml@hhhh.org. This is based to the dvtait84 package.
dvtait84, pic84faq (dvtait84.zip, pic84faq.zip)
A parallel port programmer, schema (ascii) and DOS software with basic and turbo-C sources included. Author David Tait, david.tait@man.ac.uk. He has a lot more stuff, and some new designs to program other PICs in his PIC links page.
mjcox84 (mjcox84.zip)
A parallel port programmer, no schema, written in assembler for DOS with 486/33 timings. Very limited. Author: Mark J Cox, m.j.cox@bradford.ac.uk.
ngoodw84 (ngoodw84.zip)
A parallel port programmer and disassembler, no schema, seems to use pins DATA1 = data and DATA2 = clock and needs external programming voltage. From Everyday Practical Electronics, February 1996, author Derren Crome. Disassembler by Nigel Goodwin nigelg@lpilsley.demon.co.uk.
Minimized PIC16C84 Programmer
Parallel port programmer, DOS software, needs external 13V power source. Author Stephen M. Nolan, nolan@isc-durant.com.

Available languages

Free compilers

picasm105 (picasm105.tar.gz)
Assembler in ANSI-C by Timo Rossi. Outputs both IHX16 and IHX8M. Looks pretty nice. You can also find disassemblers for 12 bit and 14 bit PIC's.
asm_c84 (asm_c84.zip)
Assembler in ANSI-C by James Cleverdon, jamesc@sequent.com. No INCLUDE, no IF, outputs IHX8M. The manual.
SIL
I have heard of free SIL language (something like Pascal/M2) compiler for PIC. GNU GPL version for Linux is under construction and should be available in Internet soon.

Commercial demos

HI-TECH Software C compiler
A demo is available of their currently beta version compiler.

Other software

PSIM
PIC simulator for 16Cxx