Go Back Programming  Go to Contents

Quick PIC Tutorial

  Microchip's range of PIC microcontrollers are unbelievably powerful for their size. They are also an amazingly flexible solution to hardware problems which only a few years ago would have taken hundreds if not thousands of discrete components to make.

  This page does not include detailed explanations as to what PIC micros are, or how they used (for more starter information look here). Instead it describes a simple 'first project' which demonstrates the simplicity of both the hardware and software requirements of the chips. I hope you learn something from the project, and maybe try it out for yourself.


Traffic light project

  When I started to learn about PIC micros I wanted my first project to be as simple as possible. The PIC hardware version of the 'Hello World' program (which represents most people's first programming attempt), is the 'flashing LED program'. An LED is set to switch on and off every second (forever). While this is about as simple as it gets, I wanted something slightly more interesting, hence the following traffic light project.

  
Parts List

PIC:PIC16F84  While not the cheapest or simplest, this PIC was chosen for the first project since it can be reprogrammed again and again.
   
Programmer:EPIC  The EPIC+ pocket programmer was chosen as a cost effective alternative to the PICSTART+ from microchip.
   
Project board.    Euro Breadboard  The project was not intended to be permanent so a prototyping board was chosen.
   
Components:3 coloured LEDs/Resistors/Capacitor  These components are cheap and simple to find.



The circuit



  The circuit was designed to be about as simple as it could possibly be. Each LED is wired in series with a 470 ohm resistor to limit the current flowing through the output pins of the PIC to <20mA per LED. The 4.7K Ohm resistor and 22pF capacitor pair produce a simple R/C timing oscillator for the PIC. The diagram shows the PIC's pins in their correct relative positions to make the transfer to the prototyping board more simple.


The Program


      
;=============================================================================
;Traffic lights PIC demo.  JDC1999.
;=============================================================================
;
; Winks 3 coloured LEDs in Traffic light sequence.
;
;
;=============================================================================
        ;load in predefined register/data equates
        MACLIB "P16F8X.INC"     
;=============================================================================
        ;setup the PIC
        DEVICE PIC16F84
        DEVICE RC_OSC
        DEVICE WDT_OFF
        DEVICE PWRT_ON
        DEVICE PROTECT_OFF
;=============================================================================
        ;set to start of ram
        ORG 0CH
;pause vars
C1 DS 1       
C2 DS 1
C3 DS 1
;=============================================================================
        ;setup reset vector 
        ORG 0                   
        JMP START     
        ORG 5   
        RETW 'TRAFFIC'
;=============================================================================
START
        MOV PORTB,#0            ;start with all leds off 
        SETB RP0                ;set register page 1
        MOV TRISB,#0            ;set portb to all outputs
        CLRB RP0                ;set register page 0

MAINLOOP
        MOV PORTB,#01B          ;RED=Stop!
        CALL PAUSE
        CALL PAUSE
        CALL PAUSE
        CALL PAUSE                                
        MOV PORTB,#11B          ;Amber 
        CALL PAUSE
        MOV PORTB,#100B         ;Green=Go!
        CALL PAUSE
        CALL PAUSE
        CALL PAUSE
        CALL PAUSE
        MOV PORTB,#10B          ;Amber 
        CALL PAUSE
        JMP MAINLOOP            ;do it forever
;=============================================================================
        ;pause for about a second
PAUSE
        MOV C3,#4
        CLR C2
        CLR C1
PAUSELOOP
        NOP
        DJNZ C1,PAUSELOOP
        DJNZ C2,PAUSELOOP
        RET
;=============================================================================
;THEND
;=============================================================================




  The program is again designed to be simple rather than efficient or accurate. The program uses two bytes of ram to produce a crude pause routine, which depends on the clock speed of the processor, and therefore the values of the R/C pair in the circuit.

  The source was written in Parallax Inc.'s 8051-like instruction set rather than the Microchip instruction syntax merely because I am more familiar with the former.

  You can download the assembled HEX file here.


The finished project






A battery of cells made from stuff we found around the house
  (And here, for no particular reason is the same PIC microprocessor project being powered by an improvised battery of cells made up from: salt water, zinc plated screws, and 8 pennies! See here for more information).




[Go back for other PIC projects and info]


  A page from James David Chapman's website.
  Located at: http://www.users.globalnet.co.uk/~jchap/
  
Site mirrored here at: http://www.j.chap.btinternet.co.uk
Go back to the last page you viewed. Go to previous page on this website. Go on to the next page in this sub site. Go to the main contents list. Go to the help page. Please send me *your* home page address!. Go to the web form to simply and quickly send me your comments.
  This page last updated:
  
My rating for the page: How happy I am with this page...