Talking Electronics PIC Theory

Explaining the Instruction Set for the
PIC12
c508A


Note: 00 to 1F represents the files.  (1F is equal to thirty-two files). These files are separated into two parts. The 7 Special Purpose Registers and 25 General Purpose Files. 

Files 00 to 06 are Special Purpose Registers - there are 7 of these (see below for definition of files and registers - we have given them different names to make them easy to identify.  Registers 00 to 06 are mainly used by the micro. Files 07 to 1F are used by you, the programmer.)

Files 07 to 1F are General Purpose files - there are 25 of these

Files 0C to 1F match up with PIC16F84 and these are the files we recommend you use when programming. The only files you lose are: 07, 08, 09, 0A & 0B.


The following is a detailed description of each instruction for the PIC12c508A, in alphabetical order:

ADDLW 
This means: Add Literal to W. The word Literal means "NUMBER" such as the number 9 or 16h. The number 16h is a hex number and in normal values it represents the number: "twenty-two."
ADDLW 00 to FF
    A fixed number (called the literal) is added to the W register
(the Working register). The literal (number) can be 00 to FF.  Three flags in the STATUS register are affected by the ADD command.    See below.
This instruction is not available for the '508A. If you want this instruction or a program
requires or contains it, use the following 3 instructions:

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

ADDWF This means: Add W and a file 
ADDWF 00 to 1F,0
the result is stored in the W register.
ADDWF 00 to 1F,1
the result is stored back in the file.
ADD the contents of the W register with a file. The result can be held in the W register
(the designator=0) or put back into the named file (the designator=1).  Three flags in the STATUS register are affected by the ADD command:    Carry, Zero and Digit Carry.
If the result of an ADD instruction is greater than 255, the Carry
flag is set (1), otherwise it is 0.
If the result of an ADD instruction is zero, the zero flag is set
(1), otherwise it is 0.
The PIC monitors the lowest nibble in an addition and if the result is higher than "F," the Digit Carry flag is set (1), otherwise it is 0. For instance, if 2
1 is added to 3C, the result is 5D and this
does not affect the carry flag. But if 2D is added to 3E, the result
is 6B and the Digit Carry flag is set (1).

ANDLW This means: AND Literal with W   
ANDLW 00 to FF
The purpose of the AND operation is to detect when the same bits in
two binary numbers are set (1). If either or both bits are zero, the result is zero and the XOR instruction is used in this case.
In this instruction, the contents of the W register is ANDed with a fixed number (called the literal) and the result is placed in the W register. The literal can be 00 to FF.
The AND operation can be used to mask (remove) any bits as needed.
For example: 0F will mask the high nibble and F0 will remove the low
nibble. E.g:

using 0F:
First number:              
  1001 0011
Second number (0F):     0000 1111
ANDed answer:             0000 0011

using F0:
First number:               
  1001 0011
Second number (F0):      1111 0000
ANDed answer:              1001 0000

ANDWF  This means: AND W with f  
ANDWF 00 to 1F,0
the result is stored in the  W register. 
ANDWF 00 to 1F,1
  the result is stored back in the  file. 
The W register is ANDed with a file.  Two files cannot be ANDed together. One file must be moved into W and ANDed with the second file.
See above for masking operations for the AND operation.
With the three AND instructions, only the Zero flag is affected. If
the answer is zero, the Zero flag in the STATUS register is set (1). If the answer is greater than zero, the flag is 0.

BCF This means: Bit Clear in the file named
BCF 00 to 1F,bit
There are nearly 300 instructions (hidden in the micro's internal workings) for this command.
There are 25 files in the PIC12C508A. The first 06h files are called
Special Function Registers (SFR's) and the next nineteen files are called General Purpose Files (GPR's). These GPR's are called 07
to 1F to avoid confusion. Each file has 8 bits.
BCF means to clear (make 0) the identified bit in a named register.
E.g: BCF 6,5 means that bit 5 of file 6 is to be cleared. (File 6
contains the in/out lines - commonly called the in/out port).

BSF This means: Bit Set in the file named
BSF 00 to 1F,bit
There are nearly 300 instructions for this command.
There are
32
files in the PIC12C508A. The first seven files are called Special Function Registers (SFR's) and the next twenty five files are called General Purpose Files (GPR's). These GPR's are  07 to 1F. Each file has 8 bits.
BSF means to set (make logic 1) the identified bit in a named file.
E.g: BSF 6,5 means that bit 5 of file 6 is to be set. (File 6
contains
the in/out lines - commonly called the in/out port).

BTFSC This means: Bit Test, Skip if Clear 
BTFSC 00 to 1F,bit
There are nearly 300 instructions for this command to cover the 25 files, each with 8 bits.
BTFSC means to test the identified bit in the named register and skip
the next instruction if it is 0.
BTFSC 6,4 means to test bit 4 in file 6 and skip the next instruction
if bit 4 is zero.

BTFSS This means: Bit Test, Skip if Set
BTFSS 00 to 1F,bit
There are nearly 300 instructions for this command to cover the 25 files, each with 8 bits.
BTFSS means to test the identified bit in the named register and skip
the next instruction if it is 1. BTFSS 3,2 means to test bit 2 in file 3 and skip the next instruction if bit 2 is 1.

CALL    This means: Call Subroutine. In a program it is written as: CALL Spike1 or CALL Tone1 etc, where Spike1 and Tone1 are called Labels. A RETURN should be placed at the end of the sub-routine being CALLed so that the micro comes back to the next instruction after the CALL instruction.  

CALL Label
For the PIC12C508A, the CALL instruction can only access addresses: 000 to 0FF. This is effectively only half the memory and if your program is more than approx 256 bytes long,
(0FFh)  you will have to consider how you will structure the program. See Structuring the program.
Programs should be written so that the first few instructions take the micro to the MAIN PROGRAM. The "Main Program" is located at the end of your program and this may be at the end of memory or half-way through, if your program is about 250 bytes long. 
After the first few instructions that tell the micro to GOTO Main, you place all the sub-routines required for the program. The sub-routines are called by a CALL command and the end of each sub-routine must have a RETLW 00 instruction as the '508A does not have a RETURN instruction.
A further call can be made from the sub-routine to another sub-routine but this second sub-routine cannot call another sub-routine. Each time a CALL instruction is executed, an address value is placed (PUSHED) into the stack so the micro knows where to come back to after the sub-routine is executed and the stack can only be 2 high. So care is needed not to run out of stack.
See also GOTO, RETURN and RETLW.

CLRF  This means: Clear f 
CLRF 00 to 1F
The contents of a file is cleared and the Z bit is set. That is: all 8 bits become 0.
The first 6 files are Special Function Registers (SFR's) and the next nineteen files are called General Purpose files (GPR's). These GPR's are called 07h to 1F (they can also be called registers 7 to thirty-two).

CLRW  This means: Clear W (the working register -called the accumulator in other microprocessors)
CLRW
The working register (W) is cleared. All bits are made (0's). When a bit is zeroed, the zero flag (Z flag ) is set (1). In other words the Z flag is 1. 

CLRWDT  This means: Clear Watchdog Timer
CLRWDT
CLRWDT instruction resets the Watchdog Timer. It also resets the prescaler of the WDT.

COMF  This means: Complement the file
COMF00 to 1F,0
(the result is in W)
COMF00 to 1F,1
(the result is in f)
The contents of file `f' are complemented (0's are changed to 1's and 1's to 0's).

DECF  This means: Decrement the file
DECF 00 to 1F,0
(the result is in W)
DECF 00 to 1F,1
(the result is in f)
The contents of file `f' are decremented. This simply means 1 is deducted (taken) from the file. If the file is 0000 0000 it rolls over to 1111 1111 (255). When the file is 0000 0001 and a DECF instruction is executed, the file becomes 0000 0000 and the Zero flag is set (1), otherwise it is (0).

DECFSZ  This means: Decrement the file, Skip if 0
DECFSZ 00 to 1F,0
(the result is in W)
DECFSZ 00 to 1F,1
(the result is in f)
The DECFSZ instruction has many uses. One important use is in a Delay sub-routine. In this routine the DECFSZ instruction creates a loop in which the micro is sent to an instruction up-the-program so that a set of instructions are executed over and over. This is a time-wasting tactic to create a Delay.   Each time the micro comes to DECFSZ, the contents of the file are decremented and if the file is not zero, the next instruction in the program is executed. The next instruction is normally GOTO and this sends the micro up-the-program. Eventually the micro will come to DECFSZ and the file, after decrementing, will be zero. The GOTO instruction will be passed over and the next instruction will be executed.

GOTO  This means: Unconditional Branch
GOTO Label
GOTO is an unconditional branch. The micro is sent to the Label specified.

INCF  This means: Increment the file 
INCF 00 to 1F,0
(the result is in W)
INCF 00 to 1F,1
(the result is in f)
The contents of file `f' are incremented. This simply means 1 is added to the file. If the file is 1111 1111 (255), it rolls over to 0000 0000. When the file is 1111 1111 and an INCF instruction is executed,
the file becomes 0000 0000 and the Zero flag is set (1), otherwise it is (0).

INCFSZ   This means: Increment the file, Skip if 0 
INCFSZ 00 to 1F,0
(the result is in W)
INCFSZ 00 to 1F,1
(the result is in f)
Normally, the decrement function DECFSZ is used to create a delay, but an INCFSZ can be used.
It works like this: The contents of a file are incremented and the result will not be zero, so the next instruction is executed, such as GOTO an address above and execute another INCFSZ. Eventually the
file will be 1111 1111 and the next increment will roll it over to 0000 0000. The result will be zero and the GOTO instruction will be passed over. The next instruction will be executed.

IORLW This means: Inclusive OR Literal with W
IORLW 00 to FF
The contents of the W register is OR'ed with a number. The result is placed in the W register. The literal number can be 00 to FF.
This is simply an `OR' operation and the purpose of performing it is to change two or more bits to `1."
If a bit is ORed with 0, the answer is unchanged.
If a bit is ORed with 1, the result is 1.
If the working register (W) is loaded with 1111 0000 and a number such as 0100 1110 is ORed with W, the result is 1111 1110.

IORWF This means: Inclusive OR W with the file 
IORWF 00 to 1F,0
(the result is in W)
IORWF 00 to 1F,1
(the result is in f)
The contents of the W register is OR'ed with file f. This is simply an `OR' operation and the purpose of performing it is to change two or more bits to `1."
If a bit is ORed with 0, the answer is unchanged.
If a bit is ORed with 1, the result is 1.
If the working register (W) is loaded with 1111 0000 and a file with a number such as 0100 1110 is ORed with W, the result is 1111 1110.

MOVLW  This means: Move Literal to W 
MOVLW 00 to FF
A fixed number (called the Literal) is loaded into W register. The literal can be  00 to FF.

MOVF This means: Move the contents of file 00 to 1F in and out of the file or to W
MOVF 00 to 1F,0
(the result is in W). The contents of a file is moved to W.
MOVF 00 to 1F,1
(the result is in f)
For the instruction MOVF 00 to 1F,1 the contents is moved out of the file and back  into it again.  It does not go into W. This is a useful test since the zero flag (Z) is affected. If the contents are zero, the Z flag is set (1). If the contents are not zero,  the Z flag is (0).

MOVWF  This means: Copy W to the named file 
MOVWF 00 to 1F
This instruction copies data from W register to file F.

NOP      This means: No Operation
The micro performs a No Operation.

OPTION      This means: Load OPTION register
The contents of the W register is loaded into the OPTION register.

RETLW  This means: Return with Literal in W
RETLW 00 to FF
The W Register is loaded with the literal value. The Program Counter is loaded from the top of the stack (the return address).

RETURN  This means: Return from Subroutine
Return from Subroutine. The stack is POPed and the top of the stack (TOS) is loaded into the Program Counter.
The '508A does not have the RETURN instruction. Use RETLW. Assemblers interpret RETURN as RETLW and you have the added feature of having the value in W for use in the routine being returned to.  

RLF This means: Rotate the file Left through Carry
RLF 00 to 1F,0
(the result is stored in W)
RLF 00 to 1F,1
(the result is stored in f)
The contents of a file is rotated one bit to the left through the Carry Flag. This is a 9-shift to get the file back to original.

RRF  This means: Rotate the file Right through Carry
RRF 00 to1F.0
(the result is stored in W)
RRF 00 to 1F,1
(the result is stored in f)
The contents of a file is rotated one bit to the right through the Carry Flag. This is a 9-shift to get the file back to original.


SLEEP This means: Sleep
The power-down status bit is cleared. Time-out status bit, is set. Watchdog Timer and its prescaler are cleared. The processor is put into SLEEP mode with the oscillator stopped.

SUBLW     This means: Subtract W from Literal
SUBLW 00 to FF
The W register is subtracted (2's complement method) from the literal value. The result is placed in the W register.
This instruction is not available in the '508A instruction-set. Use the following  5 instructions:

E.g: SUBLW 80h

Replace with:
                MOVWF 13h         ;Move W to any file 
                MOVLW 80h         ;Put 80h into W
                MOVWF 14h         ;Move W to any file 
                MOVF 13h,0         ;Move 13h to W
                SUBWF 14h,0       ;SUBtract W from file 14h

SUBWF  This means: Subtract W from the file 
SUBWF 00 to 1F,0
( result is in W)
SUBWF 00 to 1F,1
(result is in f)
Subtract (2's complement method) W register from file 00 to 2F.

SWAPF  This means: Swap Nibbles in the file 
SWAPF 00 to 1F,0
(result is in W)
SWAPF 00 to 1F,1
(result is in f)
The upper and lower nibbles of a file 00 to 1F are exchanged.

TRIS 06 or just TRIS   This means: Load TRIS Register 
(See also OPTION 0DFh in the Library of Routines to make sure GP2 is an output line.)
This instruction loads TRIS with the contents of  W. We use TRIS 06 to remind you that you are referring to PortB (determining the input/output nature of each of the lines) when the program is transferred to a PICF84. 

The two set-up instructions are:

            MOVLW xxh        ;Load W with a literal value (remember Bit3 must be 1.)
            TRIS 06               ;Load TRIS with the value

Note: The '508A only has 6 lines in the port and uses only the 6 lower bits. If W is (0000 1000) all lines are output except GP3.Note: GP3 can  only be an INPUT.

If you don't want to use the TRIS instruction, the following 4 instructions are needed:

             BSF 03,5          ;Select Page1
             MOVLW 08       ;Make GP3 input
             MOVWF 06       ;Load TRIS file (this is not output port 6 as we are in Page1!!)
             BCF 03,5          ;Select Page0
           

XORLW  This means: Exclusive OR Literal with W
XORLW 00 to FF
The contents of the W register is XOR`ed with a literal value. The result is placed in the W register.

XORWF 
This means: exclusive OR W with the file 
XORWF 00 to 1F,0
(the result is in W)
XORWF 00 to 1F,1
(the result is in f)
Exclusive OR the contents of the W register with a file (00 to 1F).

 

 The FILES

 (also called REGISTERS)

There are 32 locations in the PIC12C508A capable of storing information similar to RAM locations in other microprocessors. They are 8-bits wide and called FILES or REGISTERS. It doesn't matter what name you give them however we have tried to separate the first 7 from the lower 25 by calling the top seven REGISTERS and the lower 25 FILES. The top seven are the Special Function Registers (SFR's) that store the flags, the value of the Program Counter, the Timer value, the INPUT or OUTPUT value for each line of the port (If the line is to be input, the value is 1. If the line is to be output, the value is 0), as well as other things. The other 25 FILES are called General Purpose files and are used during programming to store data and the micro is able to perform operations on all these files, such as rotate, swap nibbles, test a bit, set a bit or clear a bit as well as other things.
The files that are common to both the PIC16F84 (the Pseudo'508A) and the PIC12C508A are the 20 files between 0C and 1F. 
There are three other files (we call them registers) that do not have address values and these are: The Working register W, the TRIS register and the Option register.