Talking Electronics PIC Theory

LIBRARY
OF
ROUTINES
LSB-to-output


LEAST SIGNIFICANT BIT 
The right-most bit of a group of bits in a byte or nibble. It provides the least contribution to the byte or nibble. 

LITERAL 
A Literal is a value (a number, a constant) A literal such as "0C" can be loaded into the W register. Don't get confused with "file 0C."
E.g: MOVLW 0C. 
The word literal has been used so that the letter "L" can be used in the instruction-set. If the word "value" was used, the instruction would be MOVVW! The word "literal" does not mean anything special. The most confusing instructions are: 

         MOVLW 0C          ;Put 0C into W 
         MOVWF 0C          ;Copy 0C into file 0C! 


LOGIC OPERATIONS 
The '508A has 3 logic operations in the instruction-set. 
These are: logical - AND (eg ANDWF) 
logical - OR (eg IORWF) 
logical - XOR (eg XORWF) 
For example, here is how a logical AND operation is carried out on the least significant bit (LSB) in two files: Ask yourself the question: "Is the bit in both files = 1?" If they are both 1 the answer is "1" 
Otherwise the answer is 0. 
The logical OR operation is carried out on 1's and 0's by the following statement: Does one of the bits = 1 or the other = 1? If either = 1 the answer = 1. The logical OR included the situation where both 
bits = 1. The only time the answer = 0 is when both bits = 0. 
The OR operation is also called the INCLUSIVE - OR operation. 
The logical XOR is more decisive. It covers the situation where only one of the bits = 1. If both = 1, the answer = 0. That's why it is called the EXCLUSIVE - OR operation. 
Logic operation do not involve adding or subtracting bits - they involve DECISION - MAKING and the value "1" takes preference over 0's. In other words, for an AND operation, when 1's are present, in both files the result is 1. - otherwise the result is 0. 

LOOK DOWN A TABLE 
- See also "Look-up" table for the opposite requirement. 
- See also Direct Addressing 
A table can be created in the '508A up to 255 bytes long. 
Do not use more than about 250 bytes as you will need some instructions such as "setUp" before the table and these use some of the "first page" locations. The table must be located at address 000 to 0FF and must not go over 0FF. Sub-routines are paced after the table and the main routine is placed at the end of your program - this will allow the table to be as large as possible. 

The layout should be as follows: 

Set_Up      MOVLW 08 
                 TRIS 06          ;Make GP3 input 
                 GOTO Main 

Table1       ADDWF 02,1 
                 RETLW xx 
                 RETLW xx 

Main          MOVLW 02 
                 CALL Table1 
                 xxxxx 
                 xxxxx 

You can have one long table and access any part of it. In the sub-routine you must load W with the "offset" for the table, then CALL Table1. The micro will go to Table1 where it will find the instruction "ADD a value (a jump value or "offset" value) to the Program Counter." The micro will jump down the table and pick up the required value. The instruction RETLW means to RETURN with the literal value in W. You can then use the value in your sub-routine. 
The following routine compares a byte in a file with Table1. If a MATCH is found, a flag in file 1F is "SET." File 0C contains the value to be compared.   Flag file = 1F (bit 7) 

Table1       BCF 1F,7            ;Clear the flag bit 
                CLRF 0E             ;Zero "Table jump" 
                MOVLW 30h        ;30h byte table 
                MOVWF 0D         ;Table byte counter 
AAA         MOVF 0E            ;Move 0E into W 
                CALL Table1 
                XORWF 0C,0       ;DONT alter 0C 
                BTFSS 03,2         ;Test zero flag 
                GOTO BBB 
                BSF 1F,7             ;Set the flag 
                RETURN 

BBB         INCF 0E,1           ;INC the "table jump" 
               DECFSZ 0D,1 
               GOTO AAA 
               RETURN 

"LOOK-UP" TABLE 
- see "Look Down a Table" for a similar operation but in "reverse." 

A LOOK UP table is simply a "table" as presented in our Library of Routines. It contains a set of values combined with the instruction RETLW. The micro goes to the label "Table1" and adds a "jump value" to the low bits of the Program Counter. The PC is normally incremented by the micro to look at the next instruction in the program. 
At table1 the PC is incremented so that it "jumps" down the table. It finds a "RETURN with the data value in W" instruction. The micro returns to the routine with the value in W. 
The program counter is file 02 and the value "1" indicates the result is to be placed in the file itself. 

Table1       ADDWF 02,1        ;ADD the "table jump  value" to the Program Counter. Result in PC 
                RETLW 01 
                RETLW 02 
                RETLW 03 
                etc 

LOOP 
A set of program instructions that are executed repeatedly. 

LOW 
Making a GP line LOW. 
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 LOW, you must first make its corresponding bit in the TRIS register a "0". A "0" stands for "Output."    "1" stands for "Input." 
Only an OUTPUT line can be made HIGH or LOW. (If you make an INPUT line HIGH or LOW it does not have any effect when the input line is read. The micro reads what is actually on the input at the time and not its setting.) 
In the examples below, we have made all the lines output, except GP3. It is best to start off with this setting and change a line to input as required. 
Next you must make the correct bit in file 06 a "0." "0" corresponds to "LOW." 
This is done by clearing the appropriate bit: e.g: BCF 06,1 
Thus to make GP0 LOW, the following instructions are needed: 

        MOVLW 08           ;Load W with 0000 1000 
        TRIS 06                 ;Load into TRIS register 
        BCF 06,0              ;Make bit 0 LOW 


To make GP1 LOW: 

        MOVLW 08          ;Load W with 0000 1000 
        TRIS 06               ;Load into TRIS register 
        BCF 06,1             ;Make bit 1 LOW 

To make GP2 LOW: 

        MOVLW 08           ;Load W with 0000 1000 
        TRIS 06                 ;Load into TRIS register 
        BCF 06,2               ;Make bit 2 LOW 

To make GP4 LOW: 

         MOVLW 08          ;Load W with 0000 1000 
         TRIS 06               ;Load into TRIS register 
         BCF 06,4             ;Make bit 4 LOW 

To make GP5 LOW: 

        MOVLW 08          ;Load W with 0000 1000 
        TRIS 06               ;Load into TRIS register 
        BCF 06,5             ;Make bit 5 LOW 


MACHINE LANGUAGE 
Instructions that a microcontroller is able to execute. Since machine language is made up of binary values (0's and 1's), for the instructions and data, it is difficult to read the values. To enable programmers to read the instructions, they work in assembly language which uses readable tags called mnemonics. 

MACHINE CYCLES 
The 4MHz oscillator frequency is divided by four to produce "Machine Cycles" for the micro of 1MHz. This is the speed of processing the instructions. Each instruction takes one machine cycle except when the instruction involves altering the value of the Program Counter. 

MASK 
To Mask (remove) bits from a file it is ANDed with "0's" See AND instruction. 
To mask a file to get only 0 to 9 cannot be done. Increment the file from zero and test for ten (0A). 

MICROSECONDS 
- See Timing 

MILLISECONDS 
- See Timing 

MOST SIGNIFICANT BIT 
The left-most bit of a group of bits in a byte or nibble. 

MOVE 
The instruction MOVE (MOV) always means "copy." In other words the originating file remains unchanged. MOVWF means to move the value in W to a specified file. 
MOVF means to move data in and out of a file but do not place it in any other file. This instruction sets the zero flag if the file is zero. 

MOVFW - Make sure you don't type an instruction like this. (In our book this is an incorrect instruction as we give the value: W = 0 and file = 1). The assembler will recognise this as MOVF 
W! The programmer possibly meant: MOVWF (copy the value in W to F). 

MULTIPLICATION 
Simple multiplication such as multiply by 2 can be performed by the RLF instruction. Successive RLF's will multiply by 4, 8, sixteen etc. 
To multiply any two numbers together requires a program. Since the '508A does not have any multiply function, it is carried out by successive ADDITIONS. 
The simplest multiplication is done with binary numbers (the numbers appear as Hex values in the program). A number from 01 to 255 can be multiplied by 01 to 255. 
To multiply 4Bh(75) by 7A(122), 122 is added to a file 75 times. It needs two files to hold the answer. 

        CLRF 1B 
        CLRF 1C 
        MOVLW 7Ah                 ;The number: 122 
        MOVWF 1A,1                ;1A holds 122 
        MOVLW 4B                   ;The number: 75 
M1    ADDWF 1B,1                 ;ADD 75 to file 1B 
        BTFSS 03,0                   ;Test Carry bit in status 
        GOTO M2                      ;file. CLEAR = no carry 
        INCF 1C,1                      ; SET = carry 
M2   DECFSZ 1A,1 
        GOTO M1 
        RETURN 

The result is a 16 bit binary number of the form: File 1C, file 1B = 0000 0000 0000 0000 

NESTED LOOP 
A nested delay or nested loop requires the use of two or more files, an inner file and an outer file. A value (a literal) is placed in each file for decrementing. If no value is assigned to a file, any value in the file will be decremented to zero and on the second pass, it will decrement 256 times. 
The outer file multiplies the time-delay of the inner file. Loading the outer file with 30h multiplies the time by 48! Refer to HEX ADDRESS FOR PIC'84. This full page gives the decimal equivalent for all Hex values from 00 to FF for loading into the files for decrementing. In a nested loop the inner file is decremented to zero. The outer file is decremented once and the inner loop is decremented once again to zero. The outer loop is reduced by one and the cycle repeats. When the outer loop is zero, the micro comes out of the delay. 

NIBBLE 
A group of four bits capable of holding the value 0 to 15 decimal. 

NON-VOLATILE 
Memory that retains its contents when power is removed - such as EPROM. EEPROM is special in that it holds memory after power is removed and can also be easily erased by electrical means. 

NOP 
No-Operation 
This instruction can be used to "pad-out" a delay routine to produce accurate time delays. Each NOP takes 1uS (micro second). 

OBJECT CODE 
Machine-readable version of a program. 

OPERAND 
A value that a maths or logic operator works on, such as "numbers." 

OPERATOR 
A symbol such as multiply, add, subtract. 

OPTION FILE 
The Option file (register) is a write-only register (the micro reads the values). 
It contains prescaler bits for the WatchDog Timer and Timer0, edge-select bit for timer, source select bit for timer0, enable bit for weak pulls-ups on GP0, GP1, GP3 and enable wake-up on pin change for GP0, GP1, GP3. 
It also determines the condition of GP2. If bit 5 is SET (1), GP2 will be an INPUT. If bit 5 is CLEAR (0), GP2 will be an OUTPUT. 

    To make GP2 input: 
             OPTION 0FFh 

    To make GP2 output: 
             OPTION 0DFh           ;1101 1111 


ORG 000 
This is called a pseudo instruction. It is read by the assembler and tells it the next instruction is to be placed at address 000. 

OR OPERATION 
- see Exclusive OR Operation: XOR 
The OR operation is ambiguous. It can be looked at in two different ways. 
If we say "this switch OR this switch will turn on the light." 
1. It could mean: either of the switches will turn on the light and if both switches are pressed, the light will turn on. 
2. It could mean: either switch will turn on the light BUT if both switches are pressed, the light will not turn on. 
The first statement is INCLUSIVE OR, it includes the possibility of both switches being pressed. See IOR. 
Statement 2 is EXCLUSIVE OR. In other words it is exclusively an OR command, not an AND statement, as well. See XOR. 

OUTPUT 
Making a GP line OUTPUT. 
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 OUTPUT, you must first make its corresponding bit in the TRIS register a "0". "1" stands for "Input." 
Thus to make GP0 OUTPUT, the following instructions are needed: 

           MOVLW 08           ;Load W with 0000 1000 
           TRIS 06                 ;Load into TRIS register 

The output line must then be made HIGH or LOW, to have any functional purpose. 

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

To make GP0 (output) LOW: 
       MOVLW 08               ;Load W with 0000 1000 
       TRIS 06                     ;Load into TRIS register 
       BCF 06,0                   ;Make bit 0 LOW 

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

To make GP1 (output) LOW: 
       MOVLW 08           ;Load W with 0000 1000 
       TRIS 06                 ;Load into TRIS register 
       BCF 06,1               ;Make bit 1 LOW 

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

To make GP2 (output) LOW: 
          MOVLW 08            ;Load W with 0000 1000 
          TRIS 06                  ;Load into TRIS register 
          BCF 06,2               ;Make bit 2 LOW 

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

To make GP4 (output) LOW: 
         MOVLW 08             ;Load W with 0000 1000 
         TRIS 06                  ;Load into TRIS register 
         BCF 06,4                ;Make bit 4 LOW 

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

To make GP5 (output) LOW: 
          MOVLW 08             ;Load W with 0000 1000 
          TRIS 06                  ;Load into TRIS register 
          BCF 06,5                ;Make bit 5 LOW