First PIC Projects ================== David Tait david.tait@man.ac.uk Here is a really simple circuit you can use as the basis of your first PIC16C84 projects: / +-O O---+---------------+--------------------+ | | | | | | +----O----+ | | + | | 14 | PIC16C84 | ------- | ____ | | ____ | --- +--[____]--O 4 16 O----+--[____]--+ ------- 1K | | | 4.7K | 4.5V --- ____ | | | | battery ------- +--[____]--O 10 | _|_ _|_ --- _|_ 220 | 5 | ___ 22pF ___ 0.1uF | - \ /^ +----O----+ | | | --- LED | | | | | | | | +-------+---------------+---------+----------+ Charles Manning (Electronics Australia, April 1996) wrote an amazingly short (6 word) LED flasher program you can use to test this circuit LIST P=16C84 MOVLW 0 TRIS 6 OPTION LOOP SLEEP INCF 6,F GOTO LOOP END Assemble the program using MPASM and ignore the warnings about TRIS and OPTION being "not recommended". Make sure you program the PIC with the watchdog enabled and the RC oscillator selected. If don't have MPASM yet here is a hex representation of the program I prepared earlier: :0C0000000030660062006300860A0328DE :00000001FF If you want to use PP.EXE to program your PIC just save these two hex records to a file (LIGHTS.HEX for example) and give this command: PP -RW8 LIGHTS.HEX The program uses the watchdog timeout as a timing source to decide when to turn the LED on or off; in fact the LED will flash at different rates depending on which pin (6-13) you connect it to. A consequence of using the watchdog timeout for timing is that the program will still work correctly no matter what PIC oscillator configuration is actually used (provided the oscillator frequency is at least a few kHz). This feature makes the program very useful for initial testing of almost any PIC protoboard. The circuit can be modified to give a slightly more entertaining effect by adding more LEDs. Connect the first LED to pin 6 (RB0), a second to pin 7 (RB1), a third to pin 8 (RB2) and so on; it's best to use at least four LEDs and you can use up to eight (the last one connected to pin 13). Each LED should be connected in series with a 470 ohm resistor and wired between the PIC pin and the -ve battery connection (ground) just like the one in the schematic above. The following program will illuminate each LED in turn obeying a to-and-fro pattern (remember the display on the car featured in the old "Knight Rider" TV series?): LIST P=16C84 ; PORTB EQU 6 TRISB EQU 86H OPTREG EQU 81H STATUS EQU 3 CARRY EQU 0 RP0 EQU 5 MSB EQU 3 ;BIT POSITION OF LEFTMOST LED ; CLRF PORTB ;ALL LEDS OFF BSF STATUS,RP0 ;SELECT REGISTER BANK 1 CLRF TRISB^80H ;SET PORTB TO ALL OUTPUTS MOVLW 0AH MOVWF OPTREG^80H ;ASSIGN PRESCALER (1:4) TO WDT BCF STATUS,RP0 ;SELECT REGISTER BANK 0 INCF PORTB,F ;TURN ON RIGHTMOST LED BCF STATUS,CARRY ;CLEAR CARRY LEFT SLEEP ;WAIT FOR WDT TIMEOUT RLF PORTB,F ;TURN ON LED TO LEFT BTFSS PORTB,MSB ;REACHED LEFTMOST? GOTO LEFT ;LOOP IF NOT RIGHT SLEEP ;WAIT FOR WDT TIMEOUT RRF PORTB,F ;TURN ON LED TO RIGHT BTFSS PORTB,0 ;REACHED RIGHTMOST? GOTO RIGHT ;LOOP IF NOT GOTO LEFT ;START NEW CYCLE END MPASM should assemble the program to give this hex representation: :100000008601831686010A3081008312860A031056 :100010006300860D861D08286300860C061C0C28CC :020020000828AE :00000001FF If you use PP.EXE, save the four hex records to a file (WALKLEDS.HEX say) and run the command "PP -RW8 WALKLEDS.HEX" to program your PIC. As it stands the "LED walking" program is suitable for four LEDs but you can change MSB if you want to use more - MSB should be 4, 5, 6 or 7 for 5, 6, 7 or 8 LEDs. Notice the program doesn't use the deprecated TRIS and OPTION instructions and therefore, unlike the previous program, doesn't generate any warnings when assembled. Inverting the most significant bit of a bank 1 register address (e.g. using TRISB^80H rather than simply TRISB) is just a trick to prevent MPASM generating annoying messages about the correct use of bank selection bits; there are other ways to silence MPASM but this method has some advantages. These projects may not seem very exciting but if you have just built (or bought for that matter) a PIC programmer and hurriedly put together the simple test circuit then seeing that LED flash on and off is exceedingly gratifying. I hope you find that out for yourself. Good luck.