Library of Routines
Goto P10    

More  than  - see Comparison

Move a file to W
The contents of a file can be moved to W with the following instruction:
MOVF 1A,0  The contents are actually COPIED. The original file still holds the contents. 

Move a file to another file
The contents of a file can be moved to another file via the following instructions. 
It is firstly copied to W then W is copied to the new file:

MOVF 1A,0
MOVWF 1B
;The contents of file 1A is copied to W
;W is copied to file 1B

Multiply
Simple multiplication such as multiply by 2 can be performed by the RLF instruction. Successive RLF's will multiply by 4, 8, sixteen etc. You need to be careful as this is called a "blind" operation. 
A number such as 80h (128) will not be doubled as 1000 0000 will be moved to the left and the top bit will be passed to the Carry. Only numbers up to 7F (127) can be doubled. 
To multiply by 2:
  RLF 0C,1 ;The contents of file 1A is doubled

To multiply any two numbers together requires a program. Since the PIC16F84 does not have any multiply function, it is carried out by successive ADDITIONS. A number from 01 to 255 can be multiplied by 01 to 255. 
To multiply 75(4Bh) by 122(7A), 122 is added to a file 75 times. It needs two files to hold the answer.





M1



M2
CLRF 1B
CLRF 1C
MOVLW 7Ah 
MOVWF 1A,1 MOVLW 4B 
ADDWF 1B,1 BTFSS 03,0 
GOTO M2 
INCF 1C,1 
DECFSZ 1A,1
GOTO M1
RETURN
;Clear the receiving file
;Clear the receiving file
;122
;file 1A holds 122
;75
;ADD 75 to file 1B
;Test Carry bit in status
;file. CLEAR = no carry
; SET = carry
The result is a 16 bit binary number of the form: file 1C, file 1B
  = 0000 0000 0000 0000
To multiply two numbers and obtain a decimal result requires a different program. 

Nested Delay
See Delay 

Origin  -  ORG
This is a pseudo instruction that tells the assembler where to place the next instruction.   ORG must have a value. For ORG 000, the next real instruction will be placed at memory location 000.
For ORG 1CC, the next instruction will be placed at address location 1CC as shown below:
 

SetUp





Main


 
ORG 000
MOVLW 08
TRIS 06
OPTION 0DFh
- - - - - - - - - -
- - - - - - - - - -
ORG 1CC
CALL Switch
CALL Display
CALL Beep
GOTO Main
;Start of program in memory
;

;


;Next instruction will be placed at location 1CC

Output a Table Value see also Table
Suppose file 1A contains 05 and this corresponds to the figure "5" on a 7-segment display. 
Here are the instructions:
 

Table













ADDWF 02h,1
RETLW 3Fh
RETLW 06h
RETLW 5Bh
RETLW 4Fh
RETLW 66h
RETLW 6Dh
RETLW 7Dh
RETLW 07h
RETLW 7Fh
RETLW 6Fh

MOVF 1A,0
CALL Table
MOVWF 06
;Add W to the Program Counter to create a jump. 
;0    format= gfedcba
;1    
;2    
;3
;4
;5
;6
;7
;8
;9 

;File 1A contains 05. Move it to W
;W will return with display-value for "5" (6D)
;Output display value to 7-segment display

Output a Value
The output ports for a PIC16F84 are actually FILES or REGISTERS!  Port A is file 05 and Port B is file 06. 
Port A has 5 lines, called RA0, RA1, RA2, RA3, and RA4. 
Port B has 8 lines, called RB0, RB1, RB2, RB3, RB4, RB5, RB6 and RB7.  
Each line can deliver approx 25mA. The maximum total current for the chip is about 150mA.
An output line can be HIGH or LOW. Each output line corresponds to a bit in the file associated with the port. When the bit is SET, the line is HIGH. When the bit is CLEAR, the line is LOW. 
Before you can make a line HIGH or LOW, the file must be "configured." This means each bit must be made an OUTPUT. This is done via the TRIS file. The TRIS 05 file operates on file 05 = Port A , and TRIS 06 operates on file 06 = Port B. 
Any line can be made either an input or an output at any time during the running of a program and to make a line INPUT, the corresponding bit in the TRIS file is made "1." To make a line OUTPUT, the corresponding bit in the TRIS file is made"0."
There are two ways to get to the TRIS file. One is directly via the instruction TRIS 05 or TRIS 06.

MOVLW 03
TRIS 05
;Load 0000 0011 into W
;Make RA2 and RA3 and RA4 output.

The other is via the two instructions: BSF 03,5 and BCF 03,5. These instructions allow you to go to  bank1 where the TRIS file is located. When in Bank1, the TRIS file is called 05 and 06.


BSF 03,5
MOVLW 00
MOVWF 06
BCF 03,5
;Go to Bank 1
;Load W with 0000 0000
;Make all Port B output
;Go to Bank 0 - the program memory area.

Any lines that are made output can be made HIGH or LOW.

MOVLW 1Ch
MOVWF 05
;Load 0001 1100 into W
;Make RA2 and RA3 and RA4 HIGH.

MOVLW 0FFh
MOVWF 06
;Load W with 1111 1111
;Make all Port B HIGH

Poll See also Switch
The action of POLLING means to "look at - on a regular basis."  It generally refers to an input device such as switch or push button. A push button can be pressed and released in less than 10th second and this means it must be scanned or polled 10 times a second (100mS).
This means you have 100,000 machine cycles available between "looks." 
Most programs consist of a Main routine and this is looped on a regular basis. As a program gets larger and larger, the Main routine may loop at a slower rate (mainly due to delay sub-routines needed for some of the operations) and you may exceed the 100,000 limit. 
The answer is to place the "look" feature inside the delay sub-routine.  The following sub-routine has a "Look" feature inside a Delay:

Delay

Delay1






Not

Delay2





Main







MOVLW 01h
MOVWF 1C
NOP
DECFSZ 1A,1
GOTO Delay1
BTFSS 05,0
GOTO Not
BSF 1F,0
RETURN
BCF 1F,0
BCF 1F,1
DECFSZ 1B,1
GOTO Delay1
DECFSZ 1C,1
GOTO Delay1
RETURN

CALL Delay
other instructions
BTFSS 1F,0
GOTO Main
BTFSC 1F,1
GOTO Main
BSF 1F,1
CALL Action
GOTO Main
;Approx 300mS per "unit"




;Look at push-button line on port A
;Button not pressed
;Set button-press flag

;Clear button-press flag
;Clear "action" flag








;Has button been pushed?
;No.
;Yes. Has "action" already been performed?
;Yes.
;No.

The Delay sub-routine includes instructions to look at a button and set a flag (BSF 1F,0) when it is pressed. 
The micro comes out of the Delay routine with the flag is SET. The flag is then looked at in Main and the appropriate sub-routine is executed. 
This sequence may be executed very quickly and the micro may return to Delay before the button is released. The "action" flag (BTFSC 1F,1) prevents the action being carried out more than once for a single button-press. 
When the button is released, both flags are cleared. 

Random number see also Mask
A truly random number cannot be generated by a computer as a sequence is nearly always produced. However it can be created by monitoring human intervention - such as the pressing of a button etc. Put the random number file in a place where it will increment regularly and where human involvement will cause the number of cycles etc to change by an unknown amount. 
  INCF 11h,1  ;Increment the random number
The random number file will then need to be MASKED to obtain a set of bits and incremented if zero is not needed.

Read a File
Files can be used to store temporary data.  

Read Data
The PIC16F84 does not have a feature such as "Read Data" or "Read an Instruction." Data can be added into a program in the form of a table (see Table) and this data cannot be altered. 
Temporary data can be stored in a file or set of files. There are 68 files (from 0C to 4F) in the  PIC16F84.   

Remove bits see Mask

Return 
This instruction must be somewhere in each sub-routine. It is generally at the end, but clever programming may allow the micro to Return part-way through the routine. 

RETLW 00 to FF - Return with Literal in W
A sub-routine can carry out an operation and set a flag. Alternatively it can return with a value in W. 
If a sub-routine generates two or three different result, the RETLW 00 to FF instruction can Return with the appropriate value in W. 
RETLW 00 to FF is used for each value of data in a Table. (see Table)

Rotate
This is the same as SHIFT.  All the bits in a file are moved to the left or right through the CARRY. 
Carry is located in the STATUS file (03,0).
It requires 9 shifts (rotates) to get the bits back to the original position. 
The CARRY must be cleared (BCF 03,0) if you don't want it to appear in the file. 
RLF (Rotate Left
File) increases the value of the contents (doubles the value).
RRF (Rotate Right File) decreases the value of the contents (halves the value).  

Same
To find out if two numbers are the same, they are XORed together. See XOR and Comparison

SetUp
The first sub-routine in a program is SetUp. It sets the direction for each Input/Output line and clears any other files to get them ready for incrementing etc. 
To make a line INPUT or OUTPUT, see INPUT, OUTPUT.
Instructions between BSF 03,5 and BCF 03,5 are dealing with files files in Bank 1. For instance, if files 05 is loaded with 02, this is actually the TRIS A file. CLRF 06 is clearing the file called TRIS B. This file controls the direction of the Input/Output lines of Port B and when it contains 0's, all the lines are output.

SetUp







ORG 0
BSF 03,5
MOVLW 02
MOVWF 05
CLRF 06
BCF 03,5
CLRF 1F
CLRF 05
CLRF 06
GOTO Main
;This is the start of memory for the program.
;Go to Bank 1
;Load W with 0000 0010
;Make RA1 input
;Make all port B output
;Go to Bank 0 - the program memory area.
;Clear flag file
;Clear Port A (RA0 - RA4)
;Clear Port B (RB0 - RB7)

To make SetUp suitable for both a PIC12C508A and PIC16F84, use the following:  

SetUp


ORG 0
MOVLW 08
TRIS 06
OPTION 0DFh
MOVLW 17h
MOVWF 06
GOTO Main
;This is the start of memory for the program.
;Load W with 0000 1000
;Make RB3, (GP3) input
;Make GP2 an output & disable weak pullups
;Make GP0, GP1, GP2, GP4, GP5 HIGH


Shift  see Rotate   

Stack
This is an area where up to 8 addresses are placed, when a CALL is made - 8 CALLs- 8 addresses in the stack. When an address is put on the stack it is called PUSH and when it is removed, it is called POP. If more than 8 address are placed on the stack, the first address is lost off the end. That's why you cannot have any more than 8 CALLs. When a return is made, the CALL number is reduced.
The PIC16F84 can have a CALL instruction that CALLs another sub-routine and the sub-routines CALLs another sub-routine and that sub-routine CALLS another sub-routine until 8 CALLs are made. 
The 8th sub-routine will have  a RETURN to go back the the previous sub-routine and each sub-routine will have a RETURN until finally the micro returns to Main. 
The animation below shows a CALL in a sub-routine CALLing another sub-routine and this sub-routine CALLs another sub-routine.  The Program Counter makes the micro carry out the instructions:

A PIC12C508A has only a 2-CALL stack. This means the Main routine can CALL a sub-routine and the sub-routine can call another sub-routine. This is the limit of the chip.

Stop see also Wait.
The microcontroller does not have a Stop instruction. Do not use the word "Halt" as a label, the assembler does not like it. Use Loop, Pause, Wait.  See Loop
To create a "waiting loop" use the following instructions:
Loop NOP
GOTO Loop 
;Hold the micro in a loop

When testing a program, you may need to know if the microcontroller has advanced to a certain part of the program. 
Insert the instruction: GOTO Wait.
Insert the following instructions. They should include an output to a LED etc so you can prove the micro has entered the loop. 
Wait NOP
MOVLW 1Ch
MOVWF 06
GOTO Wait 
;Hold the micro in a loop

;Put "u" on 7-segment display

Store Data
The PIC16F84 has two ways to store data. 
If you need to store only a few bytes of data for a short period of time, use files that are not required in any of the running of the program.
This information will be lost when power is removed. 
If you want to store data permanently, the PIC16F84 has 64 bytes of EEPROM.
This requires a special routine - found under EEPROM.  

SWAP
PIC language has a SWAP NIBBLES instruction. It swaps the HIGH nibble with the LOW nibble.  Swapping INDF (file 00) actually swaps the nibbles in the file pointed to by FSR.
  SWAPF 1A,1 ;Before: File 1A = 81h     After: File 1A = 18h

SWAP THE CONTENTS OF TWO FILES  
Example: File 0C = 81h
               File 0D = 47h
               File 0E = temp storage
  MOVF 0C,0 
MOVWF 0E,1 
MOVWF 0D,0 
MOVWF 0C,1 
MOVF 0E,0 
MOVWF 0D,1 
;Move 0C to W
;Move W to 0E
;Move 0D to W
;Move W to 0C
;Move 0E to W
;Move W to 0D

Switch (button, key) see also Poll
There is no difference between "Switch," "Button" and "Key."  They all refer to a push button that has momentary action. It can be an individual push button, one of a number of buttons or in a matrix of buttons such as a keypad. 
With all button instructions, there are three important things to remember. They are mechanical devices and their action is very slow in "microprocessor-time." They produce a lot of pulses when pushed and also when released (this is called switch-noise or switch-bounce). In any button routine, you must also prevent a button-press being registered more than once. 
Many sub-routines already have a switch or button feature included in them.  Select an existing sub-routine or use the following. 
The most important part of adding a switch or button routine is creating the debounce feature. 
This is to prevent "double-counting" when the button is pushed or released. Try slow-pressing and slow-release to see if the program produces a false result. If the program "double-counts," you will have to increase the debounce-time. 
Debounce can take up a lot of computer-time. Debounce is a delay routine that "masks" (hides - or disallows) the time when the button is pressed or released, so that only one pulse is detected. Instead of producing a separate debounce routines, you may be able to use the time taken to execute other sub-routines. In other words, the program looks before and after the execution of another routine and if the button is still pressed, the micro detects it as a "button-press." 
Finally, you need to detect the first press of a button and prevent the program operating on the button during the second pass of the program. 
The basis of detecting a button-press consists of 6 separate items that must be placed in the following order:
There are two flags. Bit0 in file 1F is the Debounce Flag and Bit1 in file 1F is the Button Pressed flag. 
The microprocessor executes Main and CALLs Sw. If Sw detects a key-press, two flags are SET. The first is the Button Pressed flag and the second is the Debounce flag.
The micro returns to Main and tests the Button Pressed flag to see if it is SET. If is is SET, the micro goes to a sub-routine such as CALL Increment, where a value can be incremented. The Button Pressed flag is then cleared and the micro CALLs Sw. If the switch is still pressed, the micro will return. The program is looking for the button to be released. When the button is released, the Sw sub-routine is ready to detect another button-push.


SetUp






Sw



Sw1






Sw2


Main





Main2
BSF 03,5
MOVLW 01
MOVWF 05
BCF 03,5
CLRF 1F
GOTO Main

BTFSS 05,0
GOTO Sw2
BTFSC 1F,0
RETURN
DECFSZ 1A,1
GOTO Sw1
BTFSS 05,0
GOTO Sw2
BSF 1F,1
BSF 1F,0
RETURN
BCF 1F,0
RETURN

CALL Sw
BTFSC 1F,1
GOTO Main2
Display the values on a display etc.
GOTO Main
CALL Increment
BCF 1F,1
GOTO Main
;Go to Bank 1
;Put 01 into W
;Make RA0 input
;Go to Bank 0
;Clear the button-press file


;Test the button. Button is "Active HIGH"
;Button not pressed
;Button pressed first time?  Test debounce flag
;Button already pressed. Return to Main
;Create short delay
;Look again
;Is switch still pressed?
;It was only noise
;Button Pressed.  Set button-pressed flag
;Set debounce flag
;Return to Main
;Clear debounce flag
;Return to Main


;Test button-press flag to see if button was pressed
;Button pressed
;Button not pressed


;Increment the display. (you provide the routine)
;Clear the button-press flag

Table see also Output a Table Value and CALL Table
The layout for a Table is shown below:

Table









ADDWF 02h,1
RETLW 3Fh
RETLW 06h
RETLW 5Bh
RETLW 4Fh
RETLW 66h
RETLW 6Dh
RETLW 7Dh
RETLW 07h
RETLW 7Fh
RETLW 6Fh
;Add W to the Program Counter to create a jump. 
;0    format= gfedcba
;1    
;2    
;3
;4
;5
;6
;7
;8
;9 

Toggle
The direction of a line can be changed from Input to Output or vice versa to create a tone or other feature. 
The instructions below do not know the initial condition of the line. They simply reverses the state. The instructions are:  

MOVLW 01h
XORWF 06,1
;Put 01 into W
;XOR 01 with Port B, lowest line (RB0)
The following instructions apply to the other lines:  Port A (05) can also be toggled.

MOVLW 02h
XORWF 06,1

MOVLW 04h
XORWF 06,1

MOVLW 08h
XORWF 06,1

MOVLW 10h
XORWF 06,1

MOVLW 20h
XORWF 06,1

MOVLW 40h
XORWF 06,1
;Put 0000 0010 into W
;XOR 02 with Port B, for RB1

;Put 0000 0100 into W
;XOR 04 with Port B, for RB2

;Put 0000 1000 into W
;XOR 08 with Port B, for RB3

;Put 0001 0000 into W
;XOR 10h with Port B, for RB4

;Put 0010 0000 into W
;XOR 20h with Port B, for RB5

;Put 0100 0000 into W
;XOR 40h with Port B, for RB6

Tone see also Beep
When a line is taken HIGH then LOW at a rapid rate, a tone is produced in a piezo or speaker. A buffer transistor (amplifier) may be needed.  
The simplest tone takes a line HIGH, then executes a delay. The line is taken LOW and a delay is executed. This process is repeated. The following routine shows how a tone is produced. The micro never comes out of the Tone sub-routine. It is only suitable as an "end of the line" sub-routine.
Tone




NOP
DECFSZ 1A,1
GOTO Tone
MOVLW 80h
XORWF 06,1
GOTO Tone



;Put 80 into W
;XOR 80 with Port B. Toggle RB7
To get the micro out of the Tone routine is must have a "duration." The pitch of the tone can also be changed:
Tone

Tone1

Tone2
MOVLW 10h
MOVWF 1B
MOVLW 80h
MOVLW 1A
NOP
DECFSZ 1A,1
GOTO Tone2
MOVLW 80h
XORWF 06,1
DECFSZ 1B,1
GOTO Tone1
RETURN
;The duration of the tone or "beep"

;The length of  HIGH and LOW - frequency of tone




;Put 80 into W
;XOR 80 with Port B. Toggle RB7

TRIS
The file that determines the in/out state of each line in a Port is the TRIS file. 
It is located in Bank1 of the microcontroller.  It has a value of 05 for Port A and 06 for Port B. 
 Port A only has RA0, RA1, RA2, RA3, and RA4.  Eg: MOVLW 0FFh will only affect the 5 input lines of port A.  Bits 5, 6, and 7 of a value will not have any effect.
There are two ways to get to the TRIS file. It can be accessed directly by adding the following two instructions directly into your program at any location:

MOVLW 01h
TRIS 05
;Put 01 into W
;Make RA0 an input and all others output
The following instructions apply to the other lines:  Port B (06) can also be operated on.
PortA:

















PortB:
MOVLW 02h
TRIS 05

MOVLW 04h
TRIS 05

MOVLW 08h
TRIS 05

MOVLW 10h
TRIS 05

MOVLW 20h
TRIS 05

MOVLW 1Fh
TRIS 05

MOVLW 40h
TRIS 06
;Put 0000 0010 into W
;Make RA1 an input and all others output

;Put 0000 0100 into W
;Make RA2 an input and all others output

;Put 0000 1000 into W
;Make RA3 an input and all others output

;Put 0001 0000 into W
;Make RA4 an input and all others output

;Put 0010 0000 into W
;Will make all output!

;Put 0001 1111 into W
;Will make all input!

;Put 0100 0000 into W
;Make RB6 an input and all others output
;(most PortB lines are used as outputs.)

 
The TRIS file can also be accessed by inserting BSF 03,5  BCF 03,5 instructions into your program.
The BSF 05,3 instruction takes you to Bank1 where the TRIS file is located. When in Bank1, the TRIS file for PortA is file 05 and the TRIS file for PortB is file 06. See SetUp for details of the instructions to be inserted between BSF 03,5 and BCF 03,5.
 




BSF 03,5
- - - - - - -
- - - - - - -
BCF 03,5
;Go to Bank 1


;Go to Bank 0 - the program memory area.

Wait  see also Stop.
When testing a program, you may need to know if the microcontroller has advanced to a certain part of the program. 
Insert the instruction: GOTO Wait.
Insert the following instructions. They should include an output to a LED etc so you can prove the micro has entered the loop. 
Wait NOP
MOVLW 1Ch
MOVWF 06
GOTO Wait
;Hold the micro in a loop

;Put "u" on 7-segment display

Watchdog Timer (WDT)
The PIC16F84 has an inbuilt timer that increments all the time. It has a normal time-out period of 18mS (18,000 instruction-cycles). You cannot turn the timer on or off via the program. It is turned on (or off) during the burning of the program by setting WDT in the __config file. 
To turn the watchdog timer off:    & _WDT_OFF
To turn the watchdog timer on:    & _WDT_ON
If it is active (turned ON) the micro will go to 000 (reset) after 18,000uS.  
This action has a purpose. 
If a program is badly designed, it may "run off the end of the program" and start executing "junk" commands. A glitch may cause the micro to go to an incorrect part of a program to cause it to "Freeze." To prevent this from occurring, the WDT takes the micro back to the beginning and starts the program over again. 
To prevent the WDT constantly resetting a program, the WDT must be constantly reset via the instruction CLRWDT. This gives the programmer another 18,000 instructions before having to reset the WDT. 
To keep WDT constantly reset, CLRWDT must be placed inside delays. 
Normal programming (experimenting etc) is carried out with WDT OFF. 
If 18mS is insufficient a prescaler can be added to increase the WDT time by 2, 4, 8, 16, 32, 64 or 128. The maximum time is 18mS x 128 = 2.4sec  

XOR - XOR detects a MATCH! 
XOR is called exclusive OR. 
It means the only time the output of a gate is HIGH is when ONE (and ONLY one) of the inputs is HIGH. 
When two numbers are XORed together, the result is "1" when one (and only one) of the numbers is "1." [The OR function is called inclusive OR see IOR]. 

To find out if two numbers are the same, they are XORed together. Since each binary digit will be the same (i.e. either a 0 or 1) the result will be 0000 0000. The result will set the zero flag in the status (03) file and by testing bit 2 (the Z flag) you can skip when SET.
You also have to decide where you want the result to be placed. If you don't want the value of the file to be altered, the result should be put into W.

Example:  To see if file 1E holds the value 3C. (file 1E does contain the value 3C)

MOVLW 3C  XORWF 1E,0 ;Put 3C in W
;XOR 3C with file 1E and put the result into W.
The micro will XOR file 1E with the value 3C. 
We know file 1E contains the value 3C, so the operation is: 
W:  0011 1100 
1E: 0011 1100 
      0000 0000  - this answer will be put into W

In this example, 3C is put into file 1E and the XOR operation is carried out:

MOVLW 3C MOVWF 1E XORLW 3C,0 
BTFSS 03,2 
GOTO clear
GOTO SET
;Put 3C in W
:Put 3C into file 1E
;XOR 3C with file 1E and put the result into W.
;Test the Zero flag
The zero flag will be set. (The zero flag is SET to show the answer is zero) i.e: a MATCH! and the program will go to the line GOTO SET. 

Zero Flag
The zero flag is found in the STATUS register (file 03).  It is bit 2.  When the result of an operations is zero, the zero flag is SET. In other words it is  = 1.  

Zero a file 
A file can be made zero in two ways. To zero a file is the same as clear a file.  
The instruction CLRF 1A makes the contents zero. 
CLRF 1A ;Make file 1A zero. 

You can load 00 into W and move W to a file:

MOVLW 00
MOVWF 1A
;Put 00 into W
;Make file 1A zero.

Zero in a file (test for
To see if a file is zero, move it in and out of itself. The Z flag will be affected. If it is SET, the file contains 00h.

MOVF 1A,1
BTFSS 03,2
___________
___________
;Move 1A in and out of 1A
;Test the zero flag
;Micro goes here if 1A is NOT zero
;Micro goes here if 1A is zero.

NEXT