Talking Electronics PIC Theory

LIBRARY
OF
ROUTINES
A - Bug


Anyone who has done any programming in BASIC (Beginners All-purpose Symbolic Instruction Code) will say this chapter looks like BASIC. 
But it's not. It is a library of routines using some every-day names and can be used to assist with writing a program. 
We have done all the hard work for you. This way you don't have to create all the code for every step of your program. The routines allow you to turn on an output, send a clock pulse to an external device, 
or a beep to a piezo. You can use them as a sub-routine or incorporate them into you own program. 
If you are going to use a sub-routine in its entirety, it is placed before the Main routine and CALLed when required. 
Unlike higher-level languages such as BASIC, our routines do not slow down the speed of execution of a program and do not use any more memory than a program written by yourself. Using our Library approach means you can use the full memory (and the full capability) of the chip. 
A simple comparison can be made of our "library" approach and using a chip that "interprets" instructions using a language such as BASIC. 
Our programs run 4 to 50 times faster, depending on their size as the overhead in a BASIC routine is the time taken in fetching the instruction that tells the micro where to go for the next routine. 
This can take as many cycles as the instruction itself, or even more. Our routines cost nothing (you have them now) and the only cost is to purchase a blank '508A chip for approx $5.00. Chips containing BASIC routines cost more than $70.00! But the advantage of our system is the low on-going cost. Once you get a program up-and-running, the cost to you is about $5.00 in comparison with $70.00. I'm sure you will agree, there's NO comparison! 

Programs in the Library are almost identical to a normal source code routines except that the overall Main routine will be linear and not optimised - in other words it may take up slightly more address locations due to the CALL instructions. It all depends on how you incorporate the sub-routines into your own programs. 
As far as ease of programming is concerned, our "library" approach falls midway between "programming from Scratch" and using a high level language such as BASIC. 
Some projects can be designed using only Library routines, while others will need some input from you. If anything cannot be carried out with an available routine, it can be created within the Main routine - we will also show you how to do this. 
The end result of using the "library" method is exactly the same as if you created the program yourself using source code, however the think-time will be considerably reduced as you will be taking advantage of pre-made routines.
Before you CALL (or use) any of the routines, you must understand how they operate as some have to be set-up by loading a register (or more than one register) with a value so the routine can function correctly. You may have to set up the sub-routine in a routine called "SetUp." 

All the routines are specifically designed for the PIC12C508A and '509A Microcontroller. These micro's have only 5 input/output lines and 1 input only line. They also have a smaller number of General 
purpose files than other PIC's and there is no in-built EEPROM. The '508A has 511 address locations (000 to 1FE) while the '509 has 1024 locations (000 to 3FF). The 509 has some additional files but the 
main feature of the '509 is the extra address locations for a program. 
The previous chapter describes the features of the '508A and you should understand that we are using a PIC16F84 in a "carrier" (a PC board) to look like a '508A chip. The 508A is a one-time programmable device and rather than waste a lot of chips while developing a program or having to buy special EPROM versions of the '508A, plus an EPROM eraser lamp, we can use the re-programmable PIC'84 and it will cost nothing to develop a program! 

THE PSEUDO'508A 
Twenty files are common to both the PIC'84 and PIC'508A. These are the files we will be using. 
The files are: 0C, 0D, 0E, 0F, 10h, 11h, 12h, 13h, 14h, 15h, 16h, 17h, 18h, 19h, 1A, 1B, 1C, 1D, 1E, 1F. 
The files are separated into 3 groups: 
1. Files you can use for your own programs: 0C, 0D, 0E, 0F, 10h, 11h, 12h, and 13h. 
2. Files used by the routines in the library: 14h, 15h, 16h, 17h, 18h, 19h (these are used to store temporary information during the execution of a sub-routine and do not leave a value in any file). 
3. Files that are used to stores values for a long period of time: 1A, 1B, 1C, 1D, 1E, 1F. 

THE LIBRARY OF ROUTINES 
This is a list of routines and handy information for the '508A micro-controller and the Pseudo'508A (really a PIC'84 simulating a '508A). 
ADD 
To ADD two numbers together, the '508A has two instructions: 
1. ADD literal to W 
2. ADD W and a File 
The literal can be a decimal number when writing the program in a .ASM file as the assembler will convert it to a hex value. But if you want to understand hex numbers, you should think in hex. All numbers will be ADDed in binary via the micro but to make binary numbers easy to read they are written in hex. 
Thus, to add 3C to file 1A (file 1A already contains the value 2B), the instructions are: 

           MOVF 1A,0        ;Move 1A into W 
           ADDLW 3C         ;Add 3C to W 
           MOVWF 1A        ;Move W to 1A 

File 1A will now contain 3C + 2B = 67h 

To ADD File 1A to File 1B: 

           MOVF 1A,0         ;Move 1A to W 
           ADDWF 1B,1       ;Add W and F (result in 1B) 

To ADD 23 and 36: 

Firstly, 23 and 36 are decimal numbers. They must be converted to hex numbers (refer to "Hex addresses for PIC") to get the hex equivalents. 
23 = 17h and 36 = 24h. 

           MOVLW 17h         ;Put 17h into W 
           ADDLW 24h          ;ADD 24h to W 

Do not use an instruction such as "ADD 17h" to W in the first line unless you are sure W is zero at the start of the operation. 

ADDING HEX NUMBERS 
To add two hex numbers, the easiest way is to convert each number to binary and add. Then convert the binary answer to hex. 
E.g: 2C plus BA 
2C:     0010   1100 
BA:    1011   1010 
          1110   0110 
Answer: E6 

Some Hex numbers can be easily added: 

 

ADDRESS 
Some instruction-sets use the following: 
RLF address,f. The word address refers to the files (from 0C to 1F for the '508A) 

ADDRESSING 
To address a file such as 0C, the code is: 

            MOVF 0C,0              ;Move file 0C into W 

Only files can be addressed. This is called Direct Addressing - See Direct Addressing. You CANNOT address any of the Program without special instructions. See Tables 

To address a number of files, see Indirect Addressing
ADDRESS LOCATIONS 
The '508A has 511 address locations for data or program - see Program Space

ADDLW  ADD literal and W 

This instruction is not available for the '508A. If a program contains this instruction, the following three instructions can be substituted: 

         e.g:    ADDLW 80h 

Replace with: 
                 MOVWF 13h       ;Move W to any file 
                 MOVLW 80h       ;Put 80h into W 
                 ADDWF 13h        ;ADD file 13h to W 

AND

The logical AND operation can be carried out: 
1. With a number (called the literal) and W (the working register - also called the accumulator 
          in some micros) 
2. With W and a file. 
The logical AND operation will produce a "1" when both values are "1." 

For example: 
number =      0101    1100 
W         =     1011     0111 
                   0001     0100 

One of the important uses for the AND operation is to remove (mask) either the HIGH or LOW nibble so that other operations can be carried out. 

To remove the HIGH nibble from a number, it is ANDed with 0F: 
number =     1001    0111 
W        =      0000   1111 
                   0000    0111 

To remove the lower nibble: 
          MOVLW 3C       ;Load W with say 3C 
          MOVWF 0C       ;load 3C into file 0C 
          MOVLW F0        ;Load W with F0 
          ANDWF 0C,1     ;AND W with file 0C The result in file 0C will be 30 

Example: Use the AND operation to see when an input line is HIGH: 
(Don't forget the simple instruction: BTFSS 1C,3 etc)

To see when GP3 is HIGH, look at input port (file 06) and AND with 08. This is done by loading W with 08 and ANDing file 06 with W. 
When GP3 is HIGH, the result will be 0000 1000 and the Z flag will be clear. The Z flag is in the Status file (03), bit 2. 
              MOVF 06,0      ;Move input port to W 
              ANDLW 08       ;AND 08 with W 
              BTFSS 03,2     ;The Z flag will be SET when the input is LOW 
              GOTO H 
              GOTO L 

The micro will go to GOTO "H" if the input is HIGH or go to GOTO "L" if the input is LOW. 

ALGORITHM 
An algorithm is a program or routine that solves a problem in a finite number of steps. 
When writing an algorithm, ALL the possibilities must be solved with the routine. 
An algorithm is the simplest form of "artificial intelligence." The best algorithms are found in computer games - where the computer must make a decision. 

ARGUMENT 
A value provided to an instruction for processing. 

ASSEMBLER CODE 
Programs are written in Assembler Code (or Assembler Listing) using the 33 PIC mnemonic instructions on a Template in a Notebook Program on a PC. The program is saved with .ASM (dot ASM) extension. The program is then assembled with an assembler to produce the machine code values for the instructions and saved as a .LST (dot LST) file. 

ASSEMBLY LANGUAGE 
Machine Language able to be read by humans, with instructions such as MOV for move. 

BURNING '508A's 
Burning a '508A means to PROGRAM. 
The '508A is a one-time programmable chip and can only be programmed ONCE. However there are ways to re-program unused portions of the memory - see Re-burning '508A's 

BEEP 
See: SOUND for a single Beep, or double beep. 

BINARY SYSTEM 
Number system using 0's and 1's. 

BUG 
An error or fault in a program