Talking Electronics PIC Theory

Carry - Endless Loop

The carry flag is located in the STATUS file (03) bit0. 
It is cleared by the instruction: 
                BCF 03,0 

It is set by the instruction: 
                BSF 03,0 
The direction of a port line can be changed at any time during the running of a program.
Suppose GP0 is an output and you want to make it an input (to receive a signal - such as HIGH) then make it an output again. 
For the '508A you cannot bit-set the TRIS register. You have to load a value into W and move the value into the TRIS register. 
E.g: To make GP0 input: 

The '508A has 6 lines and GP3 must always be input. 

                MOVLW 09               ;0000 1001 
                TRIS 06       ;Make GP0 and GP3 input 
                BTFSS 06,0              ;Look for HIGH on GP0 
                GOTO A                   ;GP0 not HIGH
                MOVLW 01               ;Make bit0 in file 1F 
                MOVWF 1F,1            ; "1" as "flag" 
                MOVLW 08 
                TRIS 06       ;Make GP0 output again 

Clear a file. (Same as Zero a file) 
The instruction to clear a file: CLRF where F is the file number (0C to 1F) 

                CLRF 0C             ;Clear file 0C 
                CLRW                 ;Clear W 

A file can be cleared by loading W with 00 and moving W into the file. 

                 MOVLW 00         ;Load W with 00 
                 MOVWF 0C        ;Move 00 into file 0C 

A file can be cleared by clearing the final remaining bit e.g: 
If file 0C contains 08, clearing bit 3 will clear the file: 

         0C = 0000 1000 
         BCF 0C,3 = 0000 0000 

A file can be cleared by ANDing with W and putting the result into the file. Put 00 in W and carry out the AND operation. 

                                        ;0C = 0101 1100 
                MOVLW 00       ;W = 0000 0000 
                ANDWF 0C,1    ;AND W with 0C 
                                        ;0C will have 000 0000 

Clear ALL files from 0C to 1F. 

ClrF         MOVLW 0C         ;Put start of files into W 
               MOVWF 04         ;Put 0C into FSR (pointer) 
Clear       CLRF 00              ;Clear INDF file 
               INCF 04,1            ;Increment pointer 
               BTFSS 04,5         ;Bit5 of FSR will set at 20h 
               GOTO Clear 
               GOTO Main etc 

To clear files from 0C to 10h: 

ClrF         MOVLW 0C            ;Put start of files into W 
               MOVWF 04            ;Put 0C into FSR (pointer) 
Clear       CLRF 00                 ;Clear INDF file 
               INCF 04,1               ;Increment pointer 
               MOVLW 10h          ;Put ending into W 
               XORWF 04,0          ;XOR end with FSR 
               BTFSS 03,2           ;Test Z flag. Z=set=match 
               GOTO Clear 
               GOTO Main etc


When you get your project up-and-running, you may want to protect your "intellectual property" (your program) from being pirated. This is quite understandable and the '508A has a CODE PROTECT feature whereby bit 3 in the CONFIGURATION WORD is made "0" to turn the Code Protection 
ON. This bit is made "0" during the burning of the chip and after burning, the program cannot be read by any external source. Any attempt to turn off the code protection bit will result the whole program 
being erased from memory. 
Before Code Protecting a chip, make sure you have a copy of the program in "hard" form, either on disc or in a non-protected chip. 
The PIC Designer '84 turns off the code protect bit during the burning operation so you can read the chip at a later date. 
Contact Talking Electronics if you want to burn chips with code protection. 

COMPARE - see also XOR and Comparison
To compare two numbers, they are XORed together and if they are the 
same, the Z flag will be set. Take two numbers: 

number  =  7A 0111 1010 
W         =  7A 0111 1010 

Starting at the right hand end, ask yourself the question, "Is one OR the other a 1?" The answer is no. The next column. "Is one number OR the other a 1?" No BOTH the numbers are 1! so that's why the answer is NO. In this way every column has the answer NO, when both numbers match. 
When all the answers are Zero, the flag rises! to say the result is ZERO. In other words it is SET. 
To find the zero flag look in the STATUS register, bit 2, i.e. File 03,2. 
e.g: To compare two files: 

                MOVF 1A,0           ;Move one file into W 
                XORWF 1B,0         ;XOR W and 1B 
                BTFSS 03,2           ;Test Z flag  If Z flag is SET (ie 1) the two files are the SAME! 

The same thing can be done by using the subtract operation:

                MOVF 1A,0           ;Move one file into W 
                SUBWF 1B,0         ;Subtract W from 1B 
                BTFSS 03,2           ;Test Z flag  If Z flag is SET (ie 1) the two files are the SAME! 

COMPARISON - see also XOR and Compare.
The contents of a file can be compared with the contents of the working register (W) to determine their relative magnitudes. This is done by subtracting the contents of W from the selected file. By testing 
the Carry and Zero flags, 4 results can be obtained: 

                MOVLW 22h          ;Put 22h in W 
                MOVWF 0CD         ;Move 15h to file 0C 
                MOVLW 15h          ;Put 15h in W 
                SUBWF 0C,1         ;Subtract 15h from 22h 
                BTFSS 03,2           ;Test Zero flag 


                BTFSS 03,0           ;Test Carry flag 

Zero flag is SET if W = File value = Match 
Zero flag is CLEAR if no Match 
Carry flag is SET if a borrow did not occur (W is less than or equal to file value) 
Carry flag is CLEAR if a borrow occurred (W is greater than file value) 

Suppose a file (file 1E) is incremented to 8 such as in the Logic Probe with Pulser. We need to know if the file is 1, 2 or 3. The first thing to do is eliminate the possibility of zero. 

TestA    MOVLW 00h          ;Eliminate file 1E if it is zero, 
             XORWF 1E,0         ;XOR file 1E with W
             BTFSC 03,2          ;Test the zero flag to see if file 1E is zero
             GOTO TestA1       ;File 1E is zero

The SUBWF operation below subtracts the W register (via a process called the 2's complement method) from file 1E and the carry flag in the Option register (file 03) will be SET if 1E is equal to W or more than W (i.e: 4 or more). 

             MOVLW 04            ;Put 04 into W for subtract operation
             SUBWF 1E,0          ;Carry will be set if 1E is = or more than 4
             BTFSS 03,0            ;Test the carry flag 
             GOTO Hi                ;Go to another sub-routine such as "Hi"

Here is the outcome for all the possibilities for file 1E:
     If 1E = 0   C = 0  (we have eliminated the possibility of zero via the first 4 lines above)
     If 1E = 1   C = 0  (carry is zero - this is not the CARRY BIT it is the SET (1) or clear (0) indicator)
     If 1E = 2   C = 0 
     If 1E = 3   C = 0 (carry is clear)
     If 1E = 4   C = 1 (carry is set)
     If 1E = 5   C = 1 
     If 1E = 6   C = 1 
     If 1E = 7   C = 1 
     If 1E = 8   C = 1 

The carry bit in the Option file is bit 0.  Therefore we test bit 0 of file 03:
       BTFSS 03,0
The result is 1E can only be 1, 2, or 3. 
To change bits to the opposite state. Such as 0's to 1's or 1's to 0's. The instruction COMF carries out this process. 

                 COMF 1C,0         ;result in W 
                 COMF 1C,1         ;result in file 1C 

A number that cannot change. All numbers in the PROGRAM SPACE of a '508A program are constants. The opposite is VARIABLE. A variable can be placed in a file by looking up a table, generating a random number or inputting data from an input such as GP3. 

CONVERSION TABLE - see also Table, Write 

A Table can also be called a CONVERSION TABLE. Routines can produce values but these values may not be large enough, linear or capable of being displayed. To make them useful we have to convert them. This is done via a table. The values may be 0, 1, 2, 3, etc (such as the press of a button). To show these on a 7-segment display we require the values: 06, 4B, 0F, 26h, etc. 
A table is created with the required values as follows: 

Table1       ADDWF 02,1         ;Add W to PC 
                RETLW 07h
                RETLW 4Bh
                RETLW 0Fh 
                RETLW 26h 

Before entering the table, the "Jump value" is loaded into W. 
Non-linear values can also be "corrected" via a table. By producing a table long enough to cover all the values obtained from the routine, the table can correct the high values. 
The values in the table will correspond to the "units" produced by the routine, at the low end but will "pull back" or "truncate" the high values. 
e.g: 01, 02, 03, 04, 05, 05, 06, 06, 06, 07, 07, 07, 07, 08, 08, 08, 08, 08, 09, 09, 09, 09, 09, 09, etc. 
A table can also "stretch" values but this introduces a lot of inaccuracy. 
A table can reverse a set of readings, although this can be done in a routine. 
A Table can generate values for ANOTHER TABLE. 

- see also PIC16F84 

Basically there is no point converting '508A programs to suit a PIC16F84. The '508A is much smaller in size, it has its own internal oscillator, and is cheaper. 
The programs however can be upgraded to PIC'84 (such as making use of the extra in/out lines or using the 64 bit EEPROM for data memory). 
All the instructions and mnemonics for the '508A are the same for the PIC'84 except the accessing of the in/out register (TRIS register). The '84 has additional files (0C to 2F), return from Interrupt instructions and 64 bit EEPROM for storing data. It also has two ports (PORTA, PORTB) with a total of 13 in/out lines. 
The '508A instructions for GP0 to GP5 are given as 06,0 to 06,5. This corresponds to PortB, bits 0 to 5 for the PIC'84. GP0 for the '508A will become RB0 for the PIC'84 etc. 

DATA - see TABLE - see WRITE 

Data and program take up the same place in the '508A. This place is called the PROGRAM SPACE. One byte of data takes up the same space (one address location) as a line of code in a program. 
Data must be in a table and located at 000 to 0FF in the Program Space. 
See table. 

The '508A micro does not have EEPROM Data storage space (commonly called RAM) and you have to use the files for temporary storage of data. Any files not required for a program can be used for storage. 
Clever programming can input data in nibbles or as a bit and thus more information can be stored. 

DEBOUNCE   - see Switch Debounce 

De-bugging a routine can be done by placing a "trace signal" at a particular location. 
If the micro reaches the location, a beep . . beep . . beep will emerge from a piezo. See Sound: Beep1 and Beep2. 

A debugging tone is available to help you debug a program to see how far the micro has advanced through the instructions. See Sound - 5. Beep repeat and 6.Beep...Beep...Beep... 

DECFSZ means to decrement (take one from the value in a file). If the file is zero, skip the next instruction in the program and execute the one below it. 
If the file is not zero, execute the next instruction in the program. 
A file that is being decremented with the instruction above comes out of a routine containing 00. 
A file loaded with 00 produces 256 decrements as the file is decremented first, then tested to see if it is zero. A file loaded with 00 is decremented (rolled over) to FF and a further 255 decrements are performed before it reaches 00. 

To DECrement a file, use the instruction: DECF. Eg: 

                 DECF 0C,1        ;DECrement file 0C and put the result in 0C 

To DECrement the W register, you have to put its contents in a temporary file, DEC the file and the result is placed in W. e.g: 

                MOVWF 0C,1      ;Move 0C to W 
                DECF 0C,0          ;DEC 0C and the result is placed in W 

The setting that applies when no instruction is given. 

DELAY    - See Pause
- See also Time Delay for 1/10th sec and 0.25 second delay 
A delay routine is very important in computer programming as it slows down the release of information on an external device so the human eye can view it.

Here is a 1 millisecond delay. It does not use a file, only W. This is a very complex delay routine to understand. It's an example of very complex thinking:
Delay     MOVLW 0F9h          ;put 249 into W          
Delay1   ADDLW 0FFh          ;This is effectively subtracting 1 from W
             BTFSS 03,2            ;Look at the zero bit of the Status register. Skip when Zero bit is SET.
             GOTO Delay1

The routine does 249 loops of 4 microseconds and the 250th loop consists of BTFSS (2 microseconds) and RETURN (2 microseconds). Total = 1,000 microseconds = 1 millisecond. 
Each loop takes 4 microseconds; ADDLW takes 1, BTFSS takes 1 and GOTO takes 2 microseconds. When the skip is taken it is also 4. ADDLW FFh adds FF to W and the result is the same as subtracting 1 from W. Don't use: SUBLW  01h  as this subtracts W from one, not the reverse. To subtract one from W you add the two's compliment of 1 which is FFh.  The next instruction tests the zero bit, (Z) in the STATUS register which will be set when the subtraction results in zero. The bit test takes 1 microsecond unless the skip occurs, in which case it takes 2 microseconds. 

Here is a simple 1 millisecond delay:

Delay      MOVLW 0F9h
              MOVWF 1A,1
Delay1    NOP
              DECFSZ 1A,1
              GOTO Delay1


                MOVLW 20h        ;Delay for 2 seconds 
                MOVWF 1C 
Delay1      DECFSZ 1A,1 
                GOTO Delay1 
                DECFSZ 1B,1 
                GOTO Delay1 
                DECFSZ 1C,1 
                GOTO Delay1 

Each 10h for file 1C = approx 1 sec 
This delay produces the longest delay-time on its second use as the files will be "00." 

The designator tells the micro where to place the result of a particular instruction. 
If the designator = 0 = put result in W 
If the designator = 1 = put the result in the file named in the instruction. 

For example: 
RLF 1A,0 The zero instructs the micro to put the result in W. 
RLF 1A,1 The 1 instructs the micro to put the result in file 1A. 

DIRECT ADDRESSING - See also "Look Down A Table." 
Direct addressing is straightforward. E.g: the address (of a file) is given after the instruction. INCF 0C,1 
Program Memory cannot be read directly or altered. In other words there is no instruction to "read the instruction or data at a particular address." 

DISPLAY TABLE   - See Conversion Table 

Simple division such as halving a number can be carried out by the RRF instruction. Shifting a value to the right halves the value but does not take into account any fractions (such as remainders). Successive RRF's divide by 4, 8, sixteen etc. 
To divide by other numbers, a successive subtraction is performed. 

               SUBWF subtracts W from literal - NOT literal from W. 
               SUBWF subtracts W from a file. 
To find when a file is zero (or below zero), the next instruction must see if a borrow occurred. 

Status file (03), bit 0 is the carry flag. 
C = 1 = SET = NO Borrow 
C = 0 = CLEAR = Borrow. 
Each time a subtraction takes place, another "count" file is incremented. 
E.g: File 1A contains a large, unknown value. Divide file 1A by 7. 

              MOVLW 07          ;put divider into W 
Div1        SUBWF 1A,0       ;SUBtract 7 from 1A 
              BTFSS 03,0         ;Test for borrow 
              RETURN              ;Borrow occurred 
              INCF 1B,1            ;INC "count" file 
              GOTO Div1 

File 1B will contain the "count" value. 
count x 7 + remainder = value in file 1A 

Shifting a value to the left doubles it. See RLF instruction in the Instruction Set. 

Electrically Erasable, Programmable, Read-Only Memory. Data stored in EEPROM is permanent and is not lost when power is removed. 
The '508A does NOT have an on-board EEPROM for storing values. You can store values in the files. We have suggested files 0C, 0D, 0E, 0F, 10h, 11h, 12h, 13h, 14h, 15h, 16h, 17h, 18h, 19h, can be used for your own programs, while files 1A to 1F are used in the library of routines for storing values or for particular routines. For instance, files 1E and 1F are used for storing "flag" values such as "0" or "1" to indicate if a button has been pressed for the first time, and files 1A, 1B, 1C and 1D are used in delay routines.  

This is called a pseudo instruction. It tells the assembler that the program being assembled, is over. The instruction END must be placed at the end of your program to inform the assembler that it has reached the end of your program. 

A loop with no exit. When fault-finding a program it is handy to know exactly how far the micro has advanced through the instructions. There is no point in putting a "Halt" instruction in the program (which the PIC does not have) as you will not know if the micro is in the loop or somewhere else. 
The answer is to put a GOTO Beep1 instruction in the program and the micro will beep when it gets to the "loop." 
See Beep1 and Beep2 under "SOUNDS." 

If you want to put a "loop" or "Halt" or "end" instruction in your program, the line(s) of code can be:

Loop      GOTO Loop
Loop       NOP
              GOTO Loop

If the Watchdog Timer has been activated, the micro will come out of the loop after a few hundred milliseconds.