Broccoli: Programming tools for PIC18F MCUs
To make this program work, first edit the Makefile if necessary. By default, everything will be compiled with "gcc -Wall -g" for Linux. That's probably what you want, so just go ahead and type "make" at the command prompt, look for errors, then skip the next paragraph.
If you are compiling for DOS, you will need the DJGPP DOS cross-compiler for Linux and Makefile.dos, or else you will have to edit the Makefile for whatever compiler you are using. You may also need to get csdpmi5b.zip by DJ Delorie from http://www.delorie.com/djgpp/ in order for the executables to work from MS-DOS.
You will get the following programs:
- readcfg
Dumps the configuration bits and device ID to stdout, in a human-readable format. I would try this program first to make sure that you are communicating with the PIC. You should not get all 00 unless the PIC has been configured strangely. You should NEVER get all ff. Here's an example output from readcfg:
Configuration bits 00 26 0d 0e 00 01 85 00 0f c0 0f e0 0f 40 Programmable ID bytes ff ff ff ff ff ff ff ff Device id 04 05
- readpic
readpic [-r] [address [length]] Takes one command line option, and up to two numeric arguments. The first numeric argument is the starting address to read from, and the second one is the number of bytes to read from the PIC. By default, the program outputs human-readable hexadecimal bytes. If you specify -r, the program dumps raw bytes to stdout. If you want to read the output, you could try something like "readpic -r 0 256 | od -t x1 -A x1".
- readdata
readdata [-r] [length] By default, readdata reads the first 256 bytes of the data EEPROM and dumps the raw data to stdout. If a length is specified readdata will output data contents starting at address 0. Once again, if you specify -r for raw output, you will want to use the "od" command. If readdata cannot identify the PIC it will output a warning message and assume that the chip has 256 bytes of EEPROM.
- writepic
writepic [filename.hex] Programs the PIC's code memory and configuration bits. Takes one command line argument, which must be the name of a file in Intel Hex format.
- erasepic
If you get a PIC from Microchip like I did that has part of the chip write protected, you will need to do a full chip erase before you can program the protected part of the chip. This program will perform that one-time chip erase for you. You need to power the chip at 4.5 V or more to run chiperase.
- portdiag
Interactive program to test the connections from the parallel port. Just follow the instructions on the screen and test the voltages in your circuit to make sure everything is connected properly.
I used bit 6 of the status port (ACK, pin 10) as the SDATA input from the PIC, and bits 0-3 of the data port for the outputs from the PC to the PIC as follows:
Table 1: PC-to-PIC connections
PIC pin name Bit Parallel port pin VPP/MCLR data3 5 (output) PGM/RB5 data2 4 (output) SCLK/RB6 data1 3 (output) SDATA/RB7 data0 2 (output through 220-Ohm resistor to PIC) SDATA/RB7 status6 10 (input) VSS or GND gnd 18 through 25 (choose one)
You can redefine these bits to other numbers in portcfg.h.
Note that all of the above pin placement assumes you want to use low-voltage programming mode. If you would prefer to use regular programming mode, leave data3 unconnected to VPP, and connect a 9-V battery between VPP and ground instead. (The + end goes to VPP). You do not need to connect PGM to the PC parallel port in high-voltage programming mode, but it won't hurt anything.
Once you are sure everything compiles fine on your system and you have decided whether to change my pin placement or stick with what I've given you, you will need to make the connections from the parallel port to your PIC18F. If you are going to program your PIC on a solderless breadboard like I did, you can use the following materials:
- 220-Ohm resistor
- Power decoupling cap for the PIC (I use 0.1-uF)
- Straight-through DB25 cable
- DB25 shell with solderable leads
- 3-V regulated power supply for the PIC
- Several wires
- Soldering iron and solder
- (optional) "Big-ass" capacitor for power supply
You should be able to get all of the above materials at Radio Shack or similar. Solder wires to the appropriate pins of the DB25 shell. Make sure you don't solder the side that plugs into the cable. Connect the wires to the PIC as in Table 1. Data0 (parallel port pin 2) should connect to the 220-Ohm resistor, which then connects to SDATA/RB7 of the PIC. This allows the PIC to pull up or pull down whatever level is on the output of Data0. Connect the decoupling capacitor between Vdd and Vss of the PIC. I also like to ground the oscillator input to the PIC on my programming board. This probably isn't necessary, but it can't hurt and it's only one more wire. Plug in one end of the cable to your PC's parallel port, and the other end to your DB25 shell. Connect whatever power supply you have rigged up to the PIC.
Note that my parallel port outputs 3.36 V when I write a logic "1" and 67 mV when I write a logic "0." If your parallel port outputs 5 V, you may want to use a 5 V power supply with your PIC. Remember Ohm's law and check if you need to replace the 220-Ohm resistor with the right size to avoid damaging your parallel port.
You should be ready to go! Run "readcfg" first to see if you can talk to the PIC. You may want to do a one-time full chip erase before you start programming the PIC, just in case you get a PIC with the configuration bits at non-default settings like I did. You can use "erasepic" with 9 Volts connected to VPP to clear the entire processor.
The circuit described above is a little too simple to work with all parallel ports. You may need to put some buffers between the PIC and the parallel port. See numerous examples of Tait-style parallel port PIC programmers on the web. Keep in mind that currently the software holds SDATA high when reading from the PIC, so design your circuit accordingly. Email any problems to davesullins@earthlink.net, but read PROBLEMS first.
Dave Sullins
