b pic m with a “no p art s pic programmer w · burn pic microcontrollers with a “no p art s ”...

19
BURN PIC MICROCONTROLLERS W ITH A “No PARTSPIC PROGRAMMER W hen creat- ing and de- signing a new electronics project, the current trend is to try to keep the descrip- tive terms “low-cost,” “full-featured,” and “ultra-compact” in the top slots in a list of fea- tures. That used to be hard to do. Fortunately, over the last several years, designing a pro- ject with those attribut- es has become much easier thanks to the de- velopment of program- mable devices such as the PIC microcontroller family from Microchip Technologies. Looking over the last year’s worth of construction articles that have been pub- lished in Electronics Now shows just how com- monplace those mic- rocontrollers have become. Unfortunately, some sort of pro- gramming device is needed to “burn” the software into those pro- grammable chips. The cost of such a device could easily wipe out any savings from using a microcontroller in a project-until now. Obviously, the PIC programmer presented here needs some parts in order to build it. The nicest fea- ture of the unit is that it needs no specialized parts, and it’s a very simple circuit. The unit can be built in about an hour with parts from either your junk box or the nearest Radio Shack store. Connect it to the printer port of any handy PC (the port need not be bi-directional), run the free software, and you can get started programming PIC16F84, 16F83. and 16C84 microcontrollers Now you can magically create your own custom PICs with this simple programmer: MICHAEL A. COVINGTON immediately. With an adjustable power supply, you can even do production-grade work. What's a PIC? A PIC, like most micro- controllers, is a tiny computer with CPU, ROM, RAM, and I/O circuits all on one chip. Microcontrollers are the fastestgrowing segment of the elec- tronics industry; the average home now contains about 100 of them in everything from micro- wave ovens to wrist- watches. Many microcontroller applications don’t in- volve computing, at least not as you’d nor- mally think of it. A micro- controller is best thought of as an IC that you can customize by writing a program in assembly language. You then download the program into the ROM area of the microcontroller, and the microcontroller be- comes a custom IC. Sometimes the program is designed to be little more than a logic gate or an oscillator, but what’s important is that it does exactly what you tell it to do. Among the low-end microcontrollers, the PIC family from Microchip, inc. is especially popular because of their simplicity and low cost. Add- itionally, much of the software used to create the program code needed for programming PlCs is available free for downloading from Microchip’s Web site (www. microchip.com). Of the various types of PlCs that are available, the 16F83, 16F84 and 16C84 are probably the easiest to work with. Those particular models are the ones that our programmer supports. They cost less than $6 each, and their ROM is electrically erasable so that you don’t need an ultraviolet light to erase and repro- gram the devices. The 16F84 is the most popular of the threti has 68 bytes of RAM and 1024 words of program memo- ry. The program memory is a “flash”

Upload: others

Post on 12-Jun-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

BURN PIC MICROCONTROLLERSWITH A “No PARTS”

PIC PROGRAMMER

W hen creat-ing and de-signing a

new electronics project,the current trend is totry to keep the descrip-tive terms “low-cost,”“full-featured,” and“ultra-compact” in thetop slots in a list of fea-tures. That used to behard to do. Fortunately,over the last severalyears, designing a pro-ject with those attribut-es has become mucheasier thanks to the de-velopment of program-mable devices such asthe PIC microcontrollerfamily from MicrochipTechnologies. Lookingover the last year’s worthof construction articlesthat have been pub-lished in Electronics Nowshows just how com-monplace those mic-rocontrollers have become.

Unfortunately, some sort of pro-gramming device is needed to“burn” the software into those pro-grammable chips. The cost of sucha device could easily wipe out anysavings from using a microcontrollerin a project-until now.

Obviously, the PIC programmerpresented here needs some partsin order to build it. The nicest fea-ture of the unit is that it needs nospecialized parts, and it’s a verysimple circuit. The unit can be builtin about an hour with parts fromeither your junk box or the nearestRadio Shack store. Connect it to theprinter port of any handy PC (theport need not be bi-directional),run the free software, and you canget started programming PIC16F84,16F83. and 16C84 microcontrollers

Now you can magically ”create your own customPICs with this simple

programmer:

MICHAEL A. COVINGTON

immediately. With an adjustablepower supply, you can even doproduction-grade work.

What's a PIC? A PIC, like most micro-controllers, is a tiny computer withCPU, ROM, RAM, and I/O circuits allon one chip. Microcontrollers are thefastestgrowing segment of the elec-tronics industry; the average homenow contains about 100 of them in

everything from micro-wave ovens to wrist-watches.

Many microcontrollerapplications don’t in-volve computing, atleast not as you’d nor-mally think of it. A micro-controller is best thoughtof as an IC that you cancustomize by writing aprogram in assemblylanguage. You thendownload the programinto the ROM area ofthe microcontroller, andthe microcontroller be-comes a custom IC.Sometimes the programis designed to be littlemore than a logic gateor an oscillator, butwhat’s important is thatit does exactly what youtell it to do.

Among the low-endmicrocontrollers, the PICfamily from Microchip,

inc. is especially popular because oftheir simplicity and low cost. Add-itionally, much of the software usedto create the program codeneeded for programming PlCs isavailable free for downloadingfrom Microchip’s Web site (www.microchip.com).

Of the various types of PlCs thatare available, the 16F83, 16F84 and16C84 are probably the easiest towork with. Those particular modelsare the ones that our programmersupports. They cost less than $6each, and their ROM is electricallyerasable so that you don’t need anultraviolet light to erase and repro-gram the devices.

The 16F84 is the most popular ofthe threti has 68 bytes of RAMand 1024 words of program memo-ry. The program memory is a “flash”

EEPROM that can be rewritten overa million times. Any stored informa-tion will be preserved for over 40years without any power. The 16C84is very similar but uses an older typeof EEPROM. The 16F83 can be con-sidered a “little brother” in that it hasonly half as much memory; on theplus side, it is somewhat lower in cost.

Unlike most microcontrollers,ThosePlCs don’t require quartz crystals orresonators for their clock; you canuse a resistor and capacitor as theoscillating elements. They also donot need a tightly-regulated powersupply. The supply voltage for the16F84 can be anywhere from 4 to 6volts. An extended-voltage-rangeversion can work with the supplybeing as low as 2 volts. There are 13input/output pins, each of whichcan be set individually to be eitheran output or an input-with or with-out a built-in pull-up resistor.

The “No Parts” Programmer. Pro-gramming the above-mentionedPIC chips is unusually simple. Apply 5volts to pin 14 (with pin 5 connectedto ground) and raise the voltage onpin 4 to between 12 and 14 volts.The data is then clocked in one bitat a time through pins 13 and 12. Thedata itself is sent to pin 13. Once thebit is ready, the voltage on pin 12 israised to 5 volts for at least 0.1microseconds before being low-ered back to ground. The datastream going into the chip containsboth commands that specify thevarious steps in the programmingprocess and the data that will bestored in the chip’s ROM. To verifythat the PIC was programmed cor-rectly, the PIC can also send Its con-tents back out through pin 13. Forthose who are interested In the fulltechnical details on programming

PIC chips, specifications for pro- gramming and verification can be found on Microchip’s Web site (www.microchip.com).

The schematic diagram for the“No Parts” PIC Programmer, as

shown in Fig. 1, is as simple as the programming process that it sup-

= ports, The circuit is designed to plug into the printer port of any PC. The programming data and clock sig-

nals are applied to IC1-the PICbeing programmed-through R2

6 and R3. Diodes Dl , D2, and resistor

R1 let pin 13 of IC 1 be used as bothan input for programming and asan output for verification. When pin17 on the printer port is high, the PCcan read data from pin 13 of IC1through pin 11 of the printer port. Inthat mode, R1 and D2 provide pull-up for the data signal. When pin 11of the printer port is low, D1 con-ducts. The anode of D2 is grounded,blocking current flow. The PIC chip isthen free to receive data from pin14 of the printer port. The connec-tion that D1 creates between pins11 and 17 on the printer port lets theprogramming software detect if theprogrammer is connected to theprinter port.

Capacitors Cl and C2 eliminatenoise in the DC power lines. Theprogramming voltage is switchedby Q1.

Two power supplies are neededto run the ‘No Parts” PIC Pro-grammer: 5 volts and 12-14 volts. Ifyou’re doing any experimentationwith digital circuits, you probablyalready have a power supply avail-able that provides those voltage lev-els. Obviously, that supply can alsobe used for the programmer.Another alternative is to “borrow” thevoltages from a diskette-drive power

connector inside the PC. Make surethat the 12-volt line is not less than12.0 volts. If you use that powersource, you should be comfortableopening up your PC and workingwith the internal components, If youaren’t and don’t know someonequalified that can help you, you canuse the power-supply schematicshown in Fig. 2. That schematic istaken from the data sheet for theLM317 adjustable-voltage regulator,and is provided here only for experi-mentation purposes, The completedata sheet for the LM317 is available

from National Semiconductor’s Website (www.nationalcom).

The programmer’s circuit is sosimple, it can be easily built on apiece of perfboard using standardconstruction techniques, A zero-insertion-force (ZIF) socket can beused at IC1 as a professional touchbut is not required. With a suitableadapter, surface-mount PlCs canalso be programmed. The packag-ing of the unit can be as simple oras fancy as you want. The author’sprototype, shown in Fig. 3, wasmounted on a hardwood base thatwas stained and varnished for a“home-built-with-old-world-pride”look. Mounting the cable connec-tor separately as shown makes the‘No Parts” PIC Programmer easy touse-simply connect it to the PC’sprinter port with a suitably-long 25-conductor cable.

PIC Programming. The easiest wayto learn to use the ‘No Parts” PICProgrammer is to actually write asimple program and burn a chip.Learning about the programmerthat way is also a good way to testout the hardware and software.

For example, let’s look at the sim-ple problem of building an LED ‘lightchaser,” a circuit that will light upone LED after another, in sequence,over and over. You could do it withthree ICs: an oscillator, a binarycounter, and a decoder/demulti-plexer. A PIC version of the circuit is

shown in Fig. 4.Although that circuit might cost a

bit more than a traditional digital cir-cuit, it is certainly smaller in terms ofthe physical size of the board need-ed to hold all of the components.However, there is another feature tousing a PIC for such a mundanetask-intelligence. Different patternscan be generated or multiple pat-terns can be selected with the addi-tion of a simple switch arrangement,With discrete components, the cir-cuit would have to be scrappedand re-designed to accommodatechanges like those: With a PIC, it is asimple matter of modifying the pro-gram, erasing the PIC, and re-pro-gramming it.

Let’s get back to our example.The program itself, written in PICassembly language, is shown inListing 1. The most important part ofany program is the documentation.If you leave notes in the program,you’ll remember what you were try-ing to do if you have to review theprogram at some time in the future.In the program listing, those notesare any lines that start with a semi-colon, Those lines will be ignored bythe software, called an assembler,that will turn the text in the programinto the binary numbers that thePIC recognizes as instructions,

The first three instructions, proces-sor, include, and _config, are in-structions to the assembler. The firstinstruction tells the assembler to use

P A R T S LIST FOR THE "NOPARTS" PIC PROGRAMMERSEMICONDUCTORSIC1-PIC16C84, PIC16F84, or PIC16F83

microcontroller, unprogrammed D1,D2--1N914 silicon diodeQl~2N;sW NPN sixicon h-an&&;

misrcms ( A l l resistors are Ijq-w&t& 5% u&Q&_.&~00_ohm R2-R4-_1ooa_ohm w--22m-dun,

AlBDWIQrUA~~ARTS: AND,MAwa&$$ Cl, cW:l-p &xs+&$~~ta;,.&-DB25malecxGsct~. Socket for IC1, 25conductor cab le ,

hardware, etc. _,

16F84 instructions. The second in- struction says to include a set of predefined constants in a file calledP16F84.INC. Finally, the third instruc- tion sets various configuration bits inthe PIC to turn some hardware fea-tures on or off, In this example, thechip will be using its RC oscillator, its“watchdog” timer will be turned off,and the automatic power-up resettimer will be turned on. That way, the 37

PIC will reboot whenever power is ing up” or for a special timingapplied, but it won’t automatically arrangement in a control applica-reboot several times a second. That tion.feature may be used if there is the It is important to use the _configpossibility of the PIC program "lock- instruction in any programs used

with the “No Parts” PIC Programmer.The assembler program will not bedoing the actual programming-itwill only be creating a file with thenumbers that will be transferred tothe PIC chip as a second step.

The two equ Instructions reservememory space In the PIC’s RAM fortwo variables, which we’ll be calling"J " and “K.” It is just like declaringvariables In BASIC, only we need tosay which physical RAM locationswill be used. In this case, those loca-tions are (in hexadecimal number-ing) 1E and 1 F. Those locations willbe used to store counters to keeptrack of how many times a loop hasbeen repeated.

The org instruction tells the assem-bler that the program starts at loca-tion 0 in program memory and thatthe actual program is next.

The first real PIC instruction is amovlw instruction that clears theworking register (called W). Thatnumber is then copied into the TRIScontrol register for port B, setting pins6-13 to output pins instead of inputpins. Next, the program puts binary00000001 into the W register andcopies it to Port B. That lights the LEDconnected to pin 6. But before youhave time to actually see the LEDcome on, the program executes anr/f Instruction that rotates the con-tents of Port B to the left, changingthe data to 00000010. That will lightthe second LED attached to pin 7instead. Repeating that instructionwill give 00000100, then 0 0 0 0 1 0 0 0 ,and so on,making the LEDs flash in amarching pattern,

In between rotations, the pro-gram needs to wait about ½ sec-ond so that the action isn’t too fastto see. That’s why we have a delayloop In the program. It stores thedecimal number 50 in memorylocations “J” and “K,” using thede&z instruction to count downfrom 50 to 0.

Conditional instructions on thePIC are somewhat unusual, anddecfsz is no exception. It stands for“Decrement and skip next instruc-tion if zero.” Translating the programinto English, the instructions

kloop: decfsz K,fgot0 kloop

mean “Subtract 1 from variable K,

and if the result is zero, skip the gotoinstruction,” Normally, the result is notzero and the goto part of the in-struction is not skipped. Instead, theloop executes repeatedly until K

reaches zero. As you can see, if youlike double negatives-or rather,

don’t not like double negatives,you’ll love programming PICs. Theactual program uses two loops, onenested within the other.

Finally, goto mloop sends execu-tion back to the beginning of theprogram. The end Instruction is not

! a CPU instruction; instead, it tells the

assembler that the program Is over.The 16F84 has 35 different CPU

instructions. As you can see from thesimple program we just created, youdon’t have to master all of them inorder to write useful programs.

Compiling Programs. At the Micro-chip Web site, you can get datasheets, application notes, and bestof all, MPLAB, which is a full-featureddevelopment program for compil-ing and testing PIC programs. It isdesigned to run under MicrosoftWindows. A sample screen shot

seen in Fig. 5 shows our demo pro-gram being edited. The MPLAB soft-ware lets you edit assembly-lan-guage programs (also called sourcecode), assemble them into objectcode, and then step through theresulting binary code to see what itwill actually make the microcon-troller do. That way, you can spotany logical errors in your programming before you actually commitany code to hardware.

The Microchip software is welldocumented on its use. We are go-ing to be using MPLAB to create anobject-code file from the source-code text we typed in from Listing 1.Be ready for an error message whencompiling the program. MPASM willcomplain mildly that you’re not supposed to use the tris Instruction.Microchip has dropped support forthat Instruction and some future PICprocessors might not support it. Anice feature of the PIC from a soft-ware point of view is what is calledsource-code compatibility. If yourdesign outgrows the resources ofthe chip that you started with, youcan use another chip with moreresources without having to rewritethe program from scratch. For ourpurposes, using the tris instruction onthe 16C34, 16F84, and 16F83 worksfine. Besides, the alternative way ofsetting up Fort B for output is muchmore complicated.

"Burning" a PIC. A second piece ofsoftware is needed for actually usingthe “No Parts” PIC Programmer. Thatsoftware is available at the Gerns-back Web site (ftp://ftp.gernsback.com/pub/EN/noppp.zip). That MS-DOS program runs well under Win-dows 3x or Wlndows 95. If, however,you are running it under Windows3.1, it will work best If you run theapplication “full screen” Instead of ina window. Timing is critical for theprogramming pulses, and full-screenDOS applications get full control ofthe computer. If for some reason youhave difficulty running the NOPPPprogram under Windows 3.1, try exit-ing to a DOS prompt and run it fromthere. You can even run the programunder OS/2; if you do, be sure to setthe HW_TIMER to “on” In the DOS set-tlngs for the program.

The first step is to connect the “NoParts” PIC Programmer to the PC’s

printer port and start the NOPPPprogram without any power con-nected to the programmer. If the 5volt line is grounded, the softwarewill not be able to detect D1, andwill assume that the programmer isnot attached to the printer port.

if all is well, you should see ascreen similar to the one shown inFig. 6. The menu of choices is self-explanatory, In general, you wouldwant to load an object-code file(with a .HEX extension in the file-name) into memory, select the type of PIC that you will be programming,program the part, and then verifythat the code was programmedinto the chip correctly, You can alsoerase a PIC that has already beenprogrammed for re-use or updatingof the programming. One note ofcaution: you should never insert orremove a PIC from the programmerwhile the power to the programmeris on. When programming a PIC, thesoftware will tell you what to do andwhen to do it.

Since the programmer softwarerequires some tricky timing, it waswritten to run as a DOS program.Recall that the clock pulses for pro-gramming the PIC have to last atleast 0.1 microseconds. In practice,they are somewhat longer in orderto overcome any signal "bounce” inthe cables. However, they shouldn’tbe too long or the programmingprocess will go too slowly. It is alsoimportant that the pulse timingnot depend on the speed of thecomputer’s CPU. Because thesoftware was written with that inmind, it will run on any IBM-com-patible from a 4.77-MHz XT up tothe latest Pentiums.

To achieve that, the programmersoftware uses one of the timers builtinto the PC motherboard. One of thePC’s built-in timers produces an inter-rupt 18.2 times per second (65,536times in a 24-hour day). That timer isused to update DOS’s time-of-dayclock, and some software uses it tomanage any concurrent processes.However, 18.2 times per second is fartoo slow to be useful for PIC pro-gramming. The software instead usesthe other timer that is normally usedto control the tones in the internalspeaker. The time delay availablefrom that timer can be set to 25microseconds, so that even on the

fastest Pentiums, the programmingpulses are not too short. There will besome unpredictable software over-head, so the pulses wi l l come out abii too long on the slowest PCs, butnot long enough to do any harm.

Production-Grade Programming.As cheap and as simple as it is, the‘No Parts” PIC Programmer canqualify as a productiongrade pro-grammer for confirming the rellabili-ty of your programmed PICs. How?By varying the 5-volt supply over theentire specified range while verify-ing the PIC. To do that, you’ll ofcourse need an adjustable 5-voltsupply First program and verify thePIC with the 5volt supply set to 5.0volts. Next, set the 5volt supply to 6.0volts and verify again. Finally, verifythe PIC a third time with the 5-voltsupply set to 4.0 volts.

Why does that guarantee reliabil-ity? Because the main source ofunreliability in EPROMs of any type isthat some of the cells might not becompletely programmed or com-pletely erased. If a particular loca-tion is only “half on” or *half-off”, itmight read correctly for a while butthen shift to the wrong value withage or changes in the supply volt-age. The result is that the ROM con-tents change unpredictably andthe microcontroller fails during use.By shifting the 5-volt supply voltageup and down, you change thethreshold voltages that define 0 and1 so that you can detect marginallyprogrammed bits. The cheapestcommercial programmers don’thave that feature at all. Higher-qual-ity units do it partly in that they raisethe supply voltage but not lower it.The ‘No Parts” PIC Programmer givesyou full control over the supply volt-age, making it easy to test any pro-grammed part over the chip’s fullvoltage range.

The author would like to acknowl-edge David Tait in England for hiswork on TOPIC, a PIC programmingpackage of which the “No Parts” PICProgrammer is a direct descendant.Mr. Tait’s work has been distributedon the Internet, and he has given hispermission for this adaptation. TheTOPIC software works with “No Parts”PIC Programmer hardware and viceversa. Because of that compatibility,the TOPIC package has been in-

cluded with the NOPPP softwareon the Gernsback Web site (www.gernsback.com).