ALINK="#FF0000">
LINUX GAZETTE
[ Prev ][ Table of Contents ][ Front Page ][ Talkback ][ FAQ ][ Next ]

"Linux Gazette...making Linux just a little more fun!"


Getting started with PIC 16F84 on GNU/Linux

By Jerry Sebastian


There was a time when you had to boot up a Microsoft operating system whenever you wanted to do a bit of hardware hacking. But that's not the case nowadays - there are free tools available to do development on all the major industrial microcontrollers - be it the Microchip PIC, Intel 8051, Motorola 68HC05, Atmel AVR, or whatever. In this document, I will describe how to get started with the PIC 16F84 on Linux.

2. Brief Description of PIC16F84

Peripheral Interface Controller 16F84 is the best suited middle range microcontroller available to a novice. PIC Microcontrollers are cheap and versatile. They support a lot of functionality on a single chip - 1024 bytes (14 bits long) of program memory, 68 bytes of RAM and 64 bytes of EEPROM memory (feel insufficient ? Well , it is more than enough). The 16F84 comes in 18-pin package and has 13 digital IO port pins. Internal to the chip, there is a timer and an interrupt controller. Apart from this, it supports In Circuit Serial Programming. Further documentation can be found at the official MicroChip site.

3. Hardware requirements

For writing the machine code onto the PIC, you need a burner hardware. A wide variety of programming circuits are available on the net, both simple and complex. Out of these the most common is the PIC programmer by David Tait.The circuit is simple and can be easily set up. For proper working of the circuit, you will need a d.c voltage supply of at least 15v.

4. Software requirements

4.1 Programmer

A GNU burner software for PIC, pp (Pic Programmer), is available which works with the above circuit. It is also written by David Tait. It can be downloaded from the PIC archive site . pp supports a wide variety of hardware other than the above circuit. The program memory of PIC supports only 1000 write cycles. pp is highly efficient and fast in the sense that it overwrites only the locations having codes that differ from those to be written. If the code is same, it doesn't rewrite the code into the memory.

Using the Pic Programmer

After making the binary of pp you need to do the following steps :

  1. Connect the hardware to the parallel port.
  2. Switch on the supply of the circuit.
  3. login in as root
  4. type export PPSETUP=3 . This is for telling pp that we are using the Classic Pic Burner circuit.

Now type pp. If everything is Ok and the hardware works,then pp displays a message Hardware detected OK. It also displays the options that can be given to pp during burning. Otherwise it enters into debugging mode.This is useful for debugging the hardware.

If everything is fine, you can now actually burn the machine code into the PIC.

Using the GNU PIC Assembler (gpasm)

The GNU PIC Assembler generates the machine code for the input assembly instructions. After assembling, it generates the machine code in the intel HEX format. This output code can be given as a direct input to pp.

gpasm can be downloaded from gputils which contains a lot of software for PIC family

5. Hello world !!!!

We will write our first program for blinking a group of LEDs (say 8). The LEDs are connected to the 8 pins of PORTB. So we need to configure them as output pins. Once we have configured PORTB as output port, then we can send 1's and 0's into the 8 bits of PORTB (which corresponds to the 8 LEDs) with a delay in between.

Use your favorite text editor (like vi or Emacs) for writing the assembly code. The first line of the assembly code should specify the micro-controller used (if you are using GPASM). Then we can start writing our code. The code for blinking the LEDs is as shown : [text version]

        list p=16f84            ;specify the micro controller used.
 f      equ     1
 porta          equ     0x5             ;specify the address of the ports
 portb          equ     0x6             ;
 Delay_i        equ     0xc             ;The first byte of RAM available.
 Long_Delay_i   equ 0xd
 tmp            equ     0xe

        goto Main
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;
 ; The following subroutine makes a 1 ms delay for a clock of 4Mhz for the PIC.
 ;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 Delay
        movlw 0xff
        movwf Delay_i
 L1     nop
        decfsz Delay_i, f
        goto L1
        return
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;
 ;      This subroutine produces a 1 sec delay( approx.).
 ;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 Long_Delay
        movwf tmp
        movlw 0xff
        movwf Long_Delay_i
 L2     call Delay
        decfsz Long_Delay_i, f
        goto L2
        movf tmp,w
        return
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;
 ;The following code configures all ports as output ports.
 ;If you want to configure a port as input port, change the
 ;corresponding bit to 1 in TRIS register.
 ;For eg.if you want to use port portb0-3 as input and all other as output
 ;pins, use instructions
 ;
 ;      movlw 0x0
 ;      tris porta
 ;      movlw 0x0F
 ;      tris portb
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 Main
        movlw 0x0
        tris porta
        tris portb

        movlw 0xff
        movwf portb
 L3
        call Long_Delay         ;make a 1 sec delay between blinking.
        comf portb,f            ;complement portb
        goto L3

        end
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Save the file with .s extension. The code can be assembled by the typing the command gpasm filename.s . If no errors are reported, machine code will be available in the file filename.hex. Now type the following commands for burning the code.

   
$ export PPSETUP=3
$ pp -xp filename.hex

The option -x specifies we are using crystal oscillator in the circuit, and -p specifies that we want to enable the power up timer of PIC. Other commonly used options are :


       -e --- for erasing the program memory and eeprom memory
       -w --- for enabling the watchdog timer
       -v --- for verifying the code.
The following figure shows the hardware needed to run the program. [circuit.jpg]

So what are you looking for ? Start hacking with PIC;-)

Jerry Sebastian

I am a final year B.tech student doing my course in Electronics and Communication at Govt. Engg. College, Thrissur, Kerala, India.


Copyright © 2002, Jerry Sebastian.
Copying license http://www.linuxgazette.net/copying.html
Published in Issue 79 of Linux Gazette, June 2002

[ Prev ][ Table of Contents ][ Front Page ][ Talkback ][ FAQ ][ Next ]