South Manchester Radio Club

010101 PIC Tutorial 1010101

     

  Index
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Back to Projects

 

                                               

 

 

 

 

 

 

 

 

......

 

In this lesson we will write our first program. Its a pretty simple program, it will cause the LED attached to PIN RD0 to light. We need to know how to change the state of this pin.  Whenever we want to interact with the:-

  • Digital I/O,
  • Analogue I/O,
  • Timers
  • RAM,

We do this by means of a "Register File". On the 16F877 on the 44-pin demo board the register file contains 4 banks (0 thru 3) by 64 registers. Each register contains eight bits, that is its a byte long. Think of them as a Patch Panel in a network cabinet, or a plug board on old fashioned telephone switchboard. The diagram below shows the first few entries in banks 0 and 1. Note that the file addresses are all in Hexadecimal (Hex). If you don't understand Hex don't worry, we will explain it as we proceed.

PIC16F887/7 Special Register Locations (Extract)

BANK 0 File Address BANK 1 File Address
Indirect addr. 00h Indirect addr. 80h
TMR0 01h OPTION_REG 81h
PCL 02h PCL 82h
STATUS 03h STATUS 83h
FSR 04h FSR 84h
PORTA 05h TRISA 85h
PORTB 06h TRISB 86h
PORTC 07h TRISC 87h
PORTD 08h TRISD 88h
PORTE 09h TRISE 89h
PCLATH 0Ah PCLATH 8Ah

The entries of interest to us are:-

  • STATUS (03 hex)

Bits 5 and 6 of this register control which bank of registers is selected. It is always accessible regardless of which bank is selected.

  • TRISD Bank 1,  08hex

The Digital I/O pins can either be

  • an output set high,
  • an output set low,
  • a high impedance input.

As it has three possible states, these are referred to as "Tri-State" pins. Setting Bit 0 in TRISD to a "1" will make Port D , Bit 0 an input. Clearing it makes the pin an output.

  • PORTD Bank 0, 08hex

The "PORT" registers are used to interact with the data lines. If the PORTs are configured as outputs then writing to these will set the corresponding out pin. If they are inputs, then reading will return the logic levels of the PIN.

Our First Instructions

Now its time to look at our first three instructions:-

  • BCF    <file>,<bit>

"Bit Clear File" clears a bit in a file register.

  • BSF    <file>,<bit>

"Bit Set File" sets a bit in a file register.

  • GOTO    <location>

Jumps to a different place in the program. As there is no way to stop a PIC in the program, all we can do is to use the GOTO to create a loop.

Assembler Instructions

Whilst the above three instructions are enough for the PIC, the assembler needs a couple of directives, to tell it how to assemble the program.

  • ORG <Location>

"The ORiGin" statement tells the assembler where to start generating code. For now we will always start at "0"

  • END

Tells the Assembler we are finished. I think its probably a hang over from punched cards where we needed to check nothing was missing.

Also note that the Assembler ignores any thing on a line after a ";"

At Last The Program!

 So here is the program.

        org     0         ; Start at location 0
        bsf     03h,5     ; Select Bank 1 (Set STATUS Bit 5)
        bcf     08h,0     ; Make PortD Bit 0 o/p - Clear TRISD, Bit 0
        bcf     03h,5     ; Back to Bank 0
        bsf     08h,0     ; Set RD0 High (Set PORTD, Bit 0 to 1)
        goto    $         ; Sit in an endless loop
        end

 I hope its pretty self explanatory. The "h" on the end of the numbers tells the assembler these are HEX. We have used the ";" to allow us to add comments to each line to explain what it does. This is a good idea as figuring out what code does weeks, months or even years after it was written can be tricky if haven't added any "Aid Memoirs"

If you have MPLAB want to download this code and run it you can do that from HERE.

Quick Quiz

1. What is "bsf" an abbreviation for ?

Bit State File

Bill Sells Fish

Bit Set File

2. END is used to:-

Tell the "PIC" to switch off

Tell the Assembler the program is complete

Tell the Assembler that comments follow

3. If  "bsf 08h,0" turns on RD0 which of these would turn it off

"bcf 08h,0"

"bsf 09h,0"

"bsf 08h,1"

South Manchester Radio & Computer Club PIC Tutorial

If you need support and help visit the SMRCC PIC Support Group on Yahoo

For more information about SMRCC visit the main web site or e-mail "chairman <at> smrcc.org.uk"