Talking Electronics PIC Theory

LIBRARY
OF
ROUTINES
Equates - Labels


EQUATES 
Before writing a program, the files can be listed from 0C to 1F. 
As you begin writing, you assign a particular operation to a file (such as to hold a key-value). If the file stays with the same key throughout the program, you assign the file to the key and this is called an "equate," such as Key3 is file 0C. 

Key3 equ 0C 

Each time you need to refer to Key3 in the program, you write INCF Key3,1 and the compiler will know that file 0C has to be incremented and the result stored in the file. 
In this way you refer to names through out the program and the compiler does all the sorting out. 
If you use a file for different things during the running of the program, don't use this arrangement as it will confuse the compiler. 
You do not have to use equates at all. If you can remember what is stored in every file, simply use the file numbers in the program. 
There are 25 files in the '508A micro. The files common to the PIC'84 and '508A are 0C, 0D, 0E, 0F, 10h, 11h, 12h, 13h, 14h, 15h, 16h, 17h, 18h, 19h, 1A, 1B, 1C, 1D, 1E, 1F. 
These are the files used in the programs. 
The Equates are as follows: 

0C - General purpose file 
0D - General purpose file
0E - General purpose file 
0F - General purpose file 
10h - General purpose file 
11h - General purpose file 
12h - General purpose file 
13h - General purpose file 
14h - Used by Library (temporary store) 
15h - Used by Library (temporary store) 
16h - Used by Library (temporary store) 
17h - Used by Library (temporary store) 
18h - Used by Library (temporary store) 
19h - Used by Library (temporary store) 
1A - Used by Library (Long-term store) 
1B - Used by Library (Long-term store) 
1C - Used by Library (Long-term store) 
1D - Used by Library (Long-term store) 
1E - Used by Library (Long-term store) 
1F - Used by Library (Long-term store) 

Examples of equates are as follows: 

Port equ 06h 
TxChr equ 0C 
TxLoop equ 0D 
DelayA equ 0E 
Temp equ 0F 


FAULTY START - FAULTY RESET 
If your program does not reset correctly EVERY TIME, the fault may lie in the 'in-built' delay at Start-Up. The delay may not be long enough. 
Two ways to solve the problem:
1. Execute a delay before the Start-Up routine.
2. Set the port(s) in/out features in the Main routine 


FILES 
There are 25 General Purpose Files in the PIC'508A. These are: 
07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 10h, 11h, 12h, 13h, 14h, 15h, 16h, 17h, 18h, 19h, 1A, 1B, 1C, 1D, 1E, 1F. 
The letter h stands for HEX to remind you that 12h is not a DOZEN! - it is actually 18! 
The files common to the PIC'84 and '508A are 0C, 0D, 0E, 0F, 10h, 11h, 12h, 13h, 14h, 15h, 16h, 17h, 18h, 19h, 1A, 1B, 1C, 1D, 1E, 1F. 
Using files: 
Files 0C to 13h should be used as GP Files - General Purpose Files. 
For instance, for use in a 3-file nested loop delay. 
Files 1E and 1F should be retained for long-time retention, such keypress flag. 

"508A Proto-1"
See '508A Proto-1 at end of chapter. A prototyping board containing input and output devices and a matrix of holes and pads for building your own circuit.

'508A Vs PIC'84 
The '508A should only be used in projects where cost and space-saving is of prime importance. 
The '508A is a One-Time-Programmable chip and if you think a program will need to be up-graded at a later date, the PIC'84 should be used. 
All the projects we have designed for the '508A will operate with a PIC'84. The files and port(s) in each program use only the features and capabilities that are common to BOTH chips. 
The Pseudo'508A is really a PIC'84 dressed up as a '508A so it can be re-programmed as many times as required during program development. See Pseudo'508A. 

FLAG 
A location in a file that is used to store a binary condition such as yes/no, on/off, true/false. The '508A has three flags in the Status register: Bit 0 = carry flag, Bit 1 = Digit Carry flag, Bit2 = Zero flag. 

You can produce your own flag file (we suggest 1E and 1F) for "keypress" bit etc. 

GO SUB 
PIC language does not have a Go Sub instruction. The equivalent is CALL. 

GOTO 
GOTO means to branch unconditionally. It is very important to know the difference between GOTO and CALL as the '508A has only a 2-HIGH stack and CALL uses the stack, while GOTO does not. See the article "PSEUDO'508A" for more details on CALL and GOTO. 
GOTO will access all memory locations whereas CALL only accesses the first 0FFh locations. See CALL for more details. 

HALT   - see also SLEEP 
The '508A does not have a HALT or STOP instruction. The micro is designed to constantly execute instructions at the rate of approx 1 million per second. 
A halt or stop should only be executed to test a program. 
The equivalent to HALT is an endless loop, where the micro enters the loop and continues to execute it until power is removed. An endless loop can be created as follows: 

                NOP 
Loop         NOP 
GOTO       Loop 

HEX NUMBERS 
Hex has sixteen values: 0 to F. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F). Hex values fit into a nibble. A nibble is half a byte. A byte contains a high nibble and a low nibble. Values such as 3C, AB, 10h, 42h, FF, are hex numbers. The highest hex value in a byte is FF = 256 
See "Hex Addresses for PICF84" for hex values 000 to 3FF. 
Hex values are used in delay routines and as literals for loading into W. 
A delay of FF produces 255 loops. (256 minus one as the instruction is DECrement, Skip if Zero. The micro jumps out of the loop when the file is zero). 
When counting in hex you have to think differently to counting with normal numbers. To keep your mind on the job and not convert to normal counting, the following procedure should be adopted. 
Counting up to nine is normal. Then say A, B, C, D, E, F, one-oh, one-one, one-two, one-three, one-four, one-five, . . . . . . one-nine, one-aye, one-bee, one-cee, one-dee, one-eee, one-f, two-oh, two-one, two-two two-three etc. 
This gets you out of the habit of saying twenty one etc and keeps you in hex mode. 
Refer to the full page showing Hex addresses for the PIC, from 000 to 3FF (1024) locations. 

HIGH 
Making a GP line HIGH. 
GP lines are General Purpose In/out lines and for the '508A, these lines are called: GP0, GP1, GP2, GP4 and GP5. GP3 is ONLY an INPUT. 
To make a GP line HIGH, you must first make its corresponding bit in the TRIS register a "0". A "0" stands for "Output." 
A "1" stands for "Input." 
In the examples below, we have made all the lines output, except GP3. It is best to start with this setting and change a "Port-line" to input as required. 
After the TRIS register has been loaded, you must SET the correct bit in file 06 to "1" as shown below: "1" corresponds to "HIGH." 

To make GP0 HIGH: 
                MOVLW 08         ;Load W with 0000 1000 
                TRIS 06              ;Load into TRIS register 
                BSF 06,0            ;Set bit 0 HIGH 

To make GP1 HIGH: 
               MOVLW 08          ;Load W with 0000 1000 
               TRIS 06               ;Load into TRIS register 
               BSF 06,1             ;Set bit 1 HIGH 

To make GP2 HIGH: 
                MOVLW 08         ;Load W with 0000 1000 
                TRIS 06               ;Load into TRIS register 
                BSF 06,2             ;Set bit 1 HIGH 

To make GP4 HIGH: 
                MOVLW 08          ;Load W with 0000 1000 
                TRIS 06                ;Load into TRIS register 
                BSF 06,4              ;Set bit 1 HIGH 

To make GP5 HIGH: 
                MOVLW 08           ;Load W with 0000 1000 
                TRIS 06                 ;Load into TRIS register 
                BSF 06,5               ;Set bit 1 HIGH 


INCLUSIVE OR - see IOR, See OR, see Syntax 

INCREMENT an address 
This instruction can mean two things: 
1. Increment the contents of the file at a given address: Use the instruction: INCF 1A,1 
2. Increment from say address 1A to address 1B. This means to increment from file 1A to file 1B. The purpose of these may be to look at the contents of the files etc. 
To perform this operation requires Indirect Addressing. 

                MOVLW 1A          ;Load W with start 
                MOVWF FSR       ;Load start into FSR 
                INCF FSR            ;FSR will point to 1B 

By looking at INDF (file 00) you will be looking at file 1B and you can load a literal into 1B: 

                MOVLW xx 
                MOVWF 00 

Or move the value out of file 1B: 

                MOVF 00,0 

INCREMENTING 
To increment a file, use the instruction INCF. E.g: 

                 INCF 0C,1              ;INC file 0C and put the result in the file 

INCREMENTING W 
To increment the W register use the instruction: 

                ADDLW 01             ;ADD 01 to W 
ADDLW is not available for the '508A. Use the following 3 instructions: 

                MOVWF 13h           ;Move W to any file 
                MOVLW 01             ;Put 01 into W 
                ADDWF 13h            ;ADD file 13h to W 

INDIRECT ADDRESSING 
Indirect Addressing is a very powerful (because it only requires a few instructions to produce a lot of information) method of addressing a number of files to gain information from them or write to them. 
To clear files 0C to 1F, the indirect addressing method is used. Indirect Addressing uses the INDF register and FSR (File Select Register). 
The FSR register is loaded with the first file (such as 0C) to be cleared. If we read the INDF register, we read the contents of the file looked at by FSR. In other words we read 0C! If we write into 
INDF, we write into file 0C. This is Indirect Addressing. 
FSR is called the "pointer" file, it looks down the list of files and points to a file. 
The advantage is we can increment FSR and thereby look down files 0C to 1F and either read them or write into them. 
You can also test a particular bit in any of the files or see if a bit is SET or CLEAR. 
You can also SET or CLEAR a bit in INDF and the same bit looked at by FSR will be CLEARed or SET. 
To clear RAM using Indirect Addressing. We can use the files as Random Access Memory as the '508A does not have RAM. 

                MOVLW 0C            ;Load W with start of RAM 
                MOVWF 04            ;Move start to FSR 
ClrRAM     CLRF 00                ;Clear INDF 
                INCF 04,1               ;Increment pointer 
                BTFSS 04,4           ;bit 4 will be HIGH when 20h is reached 
                GOTO ClrRAM 

INPUT 
Making a GP line INPUT. 
GP lines are General Purpose In/out lines and for the '508A, these lines are: GP0, GP1, GP2, GP4 and GP5. GP3 is ONLY an INPUT. 
To make a GP line INPUT, you must first make its corresponding bit in the TRIS register a "1". A "0" stands for "Output." 
Then you must then read file 06. 
Thus to make GP0 INPUT, the following instructions are needed: 
                MOVLW 01           ;Load W with 0000 0001 
                TRIS 06                 ;Load into TRIS register 
                MOVF 06,1           ;Move input value to W 

To make GP1 INPUT: 
                MOVLW 02           ;Load W with 0000 0010 
                TRIS 06                 ;Load into TRIS register 
                MOVF 06,1           ;Move input value to W 

To make GP2 INPUT: 
                MOVLW 04           ;Load W with 0000 0100 
                TRIS 06                 ;Load into TRIS register 
                MOVF 06,1             ;Move input value to W 

To make GP3 INPUT: 
                MOVLW 08            ;Load W with 0000 1000 
                TRIS 06                 ;Load into TRIS register 
                MOVF 06,1            ;Move input value to W 

To make GP4 INPUT: 
                MOVLW 10h         ;Load W with 0001 0000 
                TRIS 06                 ;Load into TRIS register 
                MOVF 06,1            ;Move input value to W 

To make GP5 INPUT: 
                MOVLW 20h         ;Load W with 0010 0000 
                TRIS 06                ;Load into TRIS register 
                MOVF 06,1           ;Move input value to W 

To read two or more inputs at the same time, the hex values are added together. E.g: GP2 and GP4 = 14h. 
A better way to test an input line is: 

For GP0: BTFSS 06,0 
For GP1: BTFSS 06,1 
For GP2: BTFSS 06,2 
For GP3: BTFSS 06,3 
For GP4: BTFSS 06,4 
For GP5: BTFSS 06,5 

INTEGER 
A whole number with no fractions. 

INTERRUPT 
The '508A does not have an interrupt feature. An interrupt is an input line that detects a HIGH and immediately goes to a special interrupt routine (at address 004h) and carries out the routine. 
Setting up an interrupt is very complex, to prevent program-crash, as the micro leaves what it is doing, carries out another operation, then returns. 
A much safer solution is to strobe an input line on a regular basis and allow the program to carry-out the decision-making. 
If the interrupt is only present for a very short period of time you will need to make sure the "look at input" will not miss the pulse, such as during the execution of a long delay. To overcome this, the 
delay routine should have another "look at input" included within the delay. 

IOR 
This is INCLUSIVE OR. See IOR instruction in syntax. See OR Instruction. 

IT DOESN'T WORK! 
If a program does not work see the chapter "Debugging '508A Programs." A number of hardware and software problems are discussed. One point to note: Make sure the WDT timer is OFF! 

JUMP 
PIC instructions do not have a Jump instruction. Use GOTO. 

JUMP DOWN A TABLE 
When a table is created, the first instruction has the effect of making the micro "jump down the table." 
To create a table, the first instruction adds a value to the Program Counter and effectively makes the micro "jump" to a value down the table: 

Table1         ADDWF 02,1        ;Add W to Pgm Counter 
                  RETLW 2C etc 

Note: W must be loaded with a value called the "jump value" before CALLing Table1. 

KISS 
Keep It Simple. The routines we have presented in the projects and in the Library are very simply laid out and very easy to follow. We have done this on purpose. We could have used much higher programming concepts and made every step an enigma. But what's the point. You will simply give up and nothing will be gained. 
Don't turn around and say the routines lack programming skills, because they do. They're intended for beginners and if a result is achieved with a few extra lines of code, and you understand what has been done, we have been successful. See the section on Equates. 

LABELS 
Labels are used to mark points in a program that will be destinations for CALL and GOTO instructions. 
eg: 
SetUp 
Main1 
Find_2 
Labels must in the first position of column1. They must start with an alpha character (NOT a number). Labels may contain alphanumeric characters, underbars "_" and the question mark "?" Keep them to a max of 8 characters to keep programs compact. Keep labels and "CALL Label" to the same format - eg: caps and lower case.