The PIC microcontroller, by Microchip, has become widely used in many of today's projects. Its low cost, small size, ease of use and powerful RISC instruction set make it suitable for both small projects, and complex microcontroller applications. It has been featured in many of the articles and projects in several magazines. It is an especially exciting device to the hobbyist as it can drastically simplify most complex projects.
The PIC microcontroller that meets the needs of the casual experimenter and the beginner is the PIC16C84. The main feature of the 'C84 that lends itself to experimentation is its EEPROM memory that is electrically erasable and reprogrammable. This is beneficial when debugging code or developing an application. In order to learn about PICs and use them in your own applications, it is desirable to have a way to program them. The development software is widely available and distributed for free via Internet ftp and other sources. However most of the hardware that is available to program the PIC microcontrollers is too expensive to purchase for entry-level programming. There are several programmers available to build or buy that are less complex and less expensive than a full-blown development system, but none of them are truly optimized in terms of simplicity, minimum hardware, and minimum cost. This article outlines a system that is the bare minimum necessary to program the PIC16C84.
The 'C84 is programmed in a serial fashion by using two of its I/O pins for clocking in the data. For ease of assembly, a minimum hardware approach was decided on, using the parallel printer port of a PC for I/O. All of the resetting, clocking, programming and timing is then implemented in software. GWBASIC is a lowest common denominator language. It runs on everything from the oldest 8086 PC to the newest Pentium PC. It was chosen as the language in which to write the program presented here. Programs written in GWBASIC can be run in QBASIC (when saved in ASCII format).
The parallel printer port of the IBM PC is accessible via I/O address 378 (Hex). Pins 4,3 & 2 are the least significant bits, in that order. Pin 4 is used here to control the PIC's MCLR (reset). Pin 3 is used as the output for RB6 (clock) and pin 2 for RB7 (data). By using short cable lengths and stand-alone programming (not imbedded in the target application circuit) no buffering or other signal gating is necessary.
The MCLR line of the PIC (pin 4 on the 'C84) has to be grounded to reset the microcontroller prior to programming, then held at 13 (+/- 1) volts to write the program into the EEPROM. Pin 4 of the PC parallel printer port, when high, will bias on the 2N4401 (a 2N2222 may be substituted) and ground MCLR to reset the PIC. Then when pin 4 goes low, MCLR is pulled up to 14 volts through the 2.2K collector pull-up resistor.
Pin 3 of the parallel printer port is straight through to pin 12 of the PIC. Pin 12 is RB6 and is used to clock in the program. Printer port pin 2 is wired to pin 13 of the PIC. Pin 13, RB7, is used as the serial input for the programming commands and data. The serial input is clocked in, LSB first, on the falling edge of the clock pulse.
An onboard voltage regulator is implemented to supply the 5 volt VSS so that a single 14 volt supply is all that is necessary to power the programmer. An LM317 three terminal voltage regulator is used. The 270 and 820 ohm resistors set its regulation voltage.
The entire programmer is wired on a prototype board around an 18 pin ZIF socket. In order to minimize the potential for damage to the PIC pins, a ZIF is recommended, besides it is a lot easier on the fingers. For low use applications a regular stamped-pin socket would suffice and it is cheaper.
After assembly, a thorough pre-use check out is in order. Initially, verify continuity between the ground connection of the power supply input, and ground pin 18 of the parallel printer port connector. Prior to plugging the printer port connector to a computer, apply 13 volts to the power supply input. Using a voltmeter, check for 5 volts on pin 14 of the PIC socket, and 13 volts on pin 4. Be certain that no voltages exist on any of the parallel printer port connector pins. Connect the printer connector and run Microsoft's DEBUG (included with DOS and Windows95). Use the debug command `O' to output to the printer port 378. First, toggle the MCLR pin between 13 volts and ground by typing O378 04 and O378 00. Then toggle the clock (O378 02) and data (O378 01) lines, verifying each with a voltmeter. When the check out is complete, the programmer is ready to use. Type `Q' to exit DEBUG.
Initially the PIC socket is placed into the reset mode (MCLR grounded) and the IC is placed into the programming socket. The programming mode is entered then by holding pins RB6 and RB7 low while raising MCLR from ground to 13 volts.
Commands and data are clocked into the PIC on pin RB7 in the following fashion. The clock pin RB6 is pulled high. At the same time, the data bit is placed on output pin RB7 and allowed to stabilize. While holding the data bit on RB7, the clock bit is pulled low. The data bit is loaded into the PIC during the high-to- low transition of the clock bit. Six bit commands are clocked in, one bit at a time, least-significant-bit (LSB) first. Commands are usually followed by a 14 bit data word. The 14 bit data words are clocked in, LSB first, in a 16 bit packet by appending a leading and trailing zero as start and stop bits.
At the beginning of programming the target PIC, the state of the code protection, which enables write protection, is unknown. The procedure to disable code protection is specifically prescribed and it clears all of the program and data memory as well as removing write protection. The following procedure is from the Microchip Technology PIC16C84 EEPROM Memory Programming Specification (Literature Number DS30189D):
a) Execute load configuration (with a `1' in bit 4, code protect).
b) Increment to configuration word location (0x2007).
c) Execute command (000001).
d) Execute command (000111).
e) Execute `Begin Programming' (001000).
f) Wait 10ms.
g) Execute command (000001).
h) Execute command (000111).
After the PIC is cleared by following the procedure to disable code protect, the IC is reset to access the user program memory. Using the serial programming format, the following programming cycle is then repeated for all of the program being downloaded.
a) Execute the `load data to program memory' command.
b) Output the data word.
c) Execute the `begin programming' command.
d) Wait 10ms.
e) Execute the `increment address' command.
f) The cycle is repeated until the entire program has been downloaded.
If there is data to be downloaded to the data memory, then the above programming cycle is used by substituting the `load data to data memory' command in step a.
Finally the desired configuration word is loaded. The configuration word may contain code protection by clearing the code protect bit. The `load configuration' command is output followed by the configuration word. The increment address command is output seven times to step the program counter to address 0x2007. The `begin programming' command is output followed by a 10ms delay. The programming is then complete and the PIC is put into and held in the reset mode until removed from the programming socket.
The basic program listed, takes input from an ASCII hexadecimal object file in the following format: The first word is the configuration word, followed by the program data in comma or return delineated fields. If there is data memory data, follow the last program memory word with `data:' then the data. No end flag is necessary, even if there is no data, as the program continuously checks for End Of File (EOF).
A basic program is available which will take the listing file output from the Parallax Inc. PASMX assembler (which is available for free on the Internet by ftp) and parse it into an object code file that the down-loader program can use. Small programs can be hand assembled and the hexadecimal information typed directly into an object file using an ASCII editor (such as Microsoft's EDIT which comes with DOS or Notepad which comes with Windows95). Both the down-loader program and the parser program have been compiled and are available in executable format by Internet ftp.
Microchip Technology, Inc.
Building, and testing the down-loader takes less than an hour. By downloading the available software, you can be up and programming PIC processors in an afternoon with little out-of-pocket expense, using a programming system that has been proven in commercial application. If you are just starting out, you should consider beginning using the PIC16C84-04 and a 4 MHz ceramic resonator. This combination provides great stability, ease of wiring, low cost and enough horsepower to accomplish most hobby tasks. If you use a faster PIC then you will find yourself coding a bunch of delay loops into your programs for most applications. The `C84 is TTL compatible, sources exactly the right current to directly drive LEDs and is a blast to use in projects. Once you are more experienced with this programmer, you may want to upgrade, but this is an economical and practical way to get started in the fun world of microcontrollers.
Load the basic version of the PIC16C84 Downloader now.
Load the .EXE version of the PIC16C84 Downloader now.
Load the basic version of the .LST to .OBJ basic parser now.
Load the .EXE version of the .LST to .OBJ basic parser now.
Stephen M. Nolan (N5LFY) is a photolithograpy process engineer for Texas Instruments and a student of Southeastern Oklahoma State University's Master of Technology graduate program. This programmer was developed in fulfillment of a class requirement.
Copyright 1996, Stephen M. Nolan. All rights Reserved.