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

 

                                               

 

 

 

 

 

 

 

 

......

 

SYMBOLIC CONSTANTS

In this lesson we will amend our first program to make it more readable and understandable Lets have a look at what we have:-

        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

As you can imagine trying to keep a track of all those numbers for the File Registers gets a bit tedious. The comments help, but when you are coding a new program, it would be hard to remember if 03h (thats 03 Hex) is the STATUS register or the PORTD register. Fortunately the Assembler gives us a way to replace these with the names of the registers. These are often called "Symbolic Constants" or "Symbols" for short. How does this work? Well first we use the "EQU" (EQUate) directive to define the symbol. e.g.

STATUS    EQU    03h    ; Symbol for the STATUS Register

Then whenever we want to refer to the STATUS register we can just use the word "STATUS" in the program. We can do the same for TRISD and PORTD. So our program now looks like this:-

;

; define some constants

;

STATUS  EQU     03H       ; Define a symbol for the status register

TRISD   EQU     08H       ; Define a symbol for the status register

PORTD   EQU     08H       ; Define a symbol for the status register

;

; Now the code starts here

;

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

 

The code generated by the assembler is exactly the same, but the program is now more readable.

Include Files

To make things even easier, MicroChip provide a file of EQU's for each chip type. We use the "#include"  directive to load a file of "EQU" statements into our program. So for the 44-pin board with the 16F877 chip we can do:-

;

; define some constants

;

#include "p16f677.inc"

;

; Now the code starts here

;

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

Notes:-

  1. The Assembler does not check the case of the instruction mnemonics, so we can write

    "bsf",

    "BSF"

or even

    "BsF".

  1. However the names defined in symbols, e.g. "TRISD" are by default case sensitive, so

    "PORTD"

and

    "PortD"

are different symbols.

  1. We don't need to put any code on a line. If we start it with a ";" then it will be ignored by the assembler. Many programmers use this to put a block of comments at the start start of the program as documentation. This is all ignored by the assembler, but is useful to humans when they come to look at the code.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;

; Daves Sample Program

; Written 01-April-2009

;

; Author:- Dave Wade G4UGM

;

; Version History:

;   1.0 Initial version for course

;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 

Quick Quiz

1. Which of these statements is FALSE?

SYMBOLS make the program more readable

You must use SYMBOLS to refer to File Registers

Symbol Names are case sensitive

2. Which of these statements is true

All programs must have a "#include" to define the registers symboles

All programs must have a "#include" to define the PIC Instructions

Microchip provides ".inc" files for each chip type to make codeing easier.

3. Which of these statements is true

Every line must contain an instruction or assembler directive

Every program must have a comment block at the start

You can use comments where needed

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"