


===========================================================

            TALKING ELECTRONICS Interactive website

    http://www.talkingelectronics.com/

===========================================================


===========================================================

            ROUTINES for "Copy and Pasting"

		 by Colin Mitchell
		talking@tpg.com.au

===========================================================

The following are the ROUTINES for "Copy and Pasting" into
your program - commonly called your ".asm file."
They are in alphabetical order, so everything is easy to find 
- and easy to use.
Simply copy and paste them into your program: Hold the 
left mouse button down as you pass the mouse over the instructions. 
Then click the right mouse button and a box will appear. 
Select "Copy."  Go to your program - in an another NotePad 
or WordPad - and "Paste" the instructions. All the instructions
should "line-up" into three columns. 
 
If you use a set of instructions as a complete routine, 
it is called a sub-routine. 
Alternatively they can be added to another routine, such as "Main."

Make sure you understand what to do before using this library.
All these instructions are fully explained in "Library of Routines."


 
The instructions for your program are arranged in three columns. 

The first column contains the "labels." They identify the first
instruction for a sub-routine. 

The second column contains the "instruction" - called the 
mnemonic. The "half-computer" "half-English" instruction that
both computer and micro understands.

The third column contains the "comments." It must have a ";" before
each line so that they are not assembled - so they don't
appear in the final .hex file!

Note: All the instructions in this library will "run" when placed in
a program. But sometimes, something goes wrong. If an instruction 
has hidden "formatting," the assembler may create the wrong code. 
To see if an instruction has hidden formatting, simply hold down 
the left button on the mouse and drag across the instruction. 
The background will go black and will not extend past the actual word.

Try the following:     DECFSZ 1A,1

This instruction has hidden formatting:   DECFSZ 1A,1     

Simply remove any hidden formatting by holding the mouse on the 
unwanted spaces and use the back-space arrow to remove the spaces
(or re-type the complete instruction) and the problem will be fixed. 
(It does not matter if "comments" have extra spaces. "Comments" are 
not assembled.)

Start by copying TEMPLATE. Delete the unwanted instructions and add
the instructions you want. Make sure the label is correct. 


-----------------------------------------------------
        ADD A VALUE TO A FILE
-----------------------------------------------------
A file contains a known value. To add a value to the file use:

Add	MOVLW 0CCh	;Put CCh into W
	ADDWF 0Eh	;CC will be added to the contents of file "E."

If the file overflows, the Carry/Borrow bit in the Status file 
(file 03,0) will be SET. 

Add	MOVLW 0CCh	;Put CCh into W
	ADDWF 0Eh	;CC will be added to the contents of file "E."
	BTFSS 03,0	;Test the carry bit in the Status file
	GOTO AAA	;Micro will go to AAA if no overflow
	GOTO BBB	;Micro will go to BBB if overflow occurs 
           

-----------------------------------------------------
        BEEP   -   TONE - this is a constant tone
-----------------------------------------------------

To use RB0 as the output: MOVLW 01h  (see line 8 in subroutine below)
To use RB1 as the output: MOVLW 02h
To use RB2 as the output: MOVLW 04h
To use RB3 as the output: MOVLW 08h
To use RB4 as the output: MOVLW 10h
To use RB5 as the output: MOVLW 20h
To use RB6 as the output: MOVLW 40h


Tone	MOVLW 40h	;The duration of the tone or "beep"
	MOVWF 1B
Tone1	MOVLW A0h	;The length of  HIGH and LOW - frequency of tone
	MOVLW 1A
Tone2	NOP
	DECFSZ 1A,1
	GOTO Tone2
	MOVLW 80h	;Put 80 into W
	XORWF 06,1	;XOR 80 with Port B. Toggle RB7
	DECFSZ 1B,1
	GOTO Tone1
	RETURN


-----------------------------------------------------
        BEEP  -  BEEP  -  BEEP - repeat
-----------------------------------------------------
This beep routine is suitable for debugging a program. The 
instruction GOTO Beep1 is inserted in a program to see how far the 
micro has advanced through the instructions. 
Beep1 produces an endless beep . . . beep . . . beep . . . 

In the sub-routine you are trying to debug, put the instruction:

	GOTO Beep1

At the beginning of memory, after Tables, put the following:


Beep1	CALL Beep
	CALL BeepDel
	GOTO Beep1


Beep	MOVLW 40h	;The duration of the beep
	MOVWF 1B
Beep1	MOVLW A0h	;The length of  HIGH and LOW - frequency of beep
	MOVLW 1A
Beep2	NOP
	DECFSZ 1A,1
	GOTO Beep2
	MOVLW 80h	;Put 80 into W
	XORWF 06,1	;XOR 80 with Port B. Toggle RB7
	DECFSZ 1B,1
	GOTO Beep1
	RETURN


BeepDel	DECFSZ 1A,1
	GOTO BeepDel
	DECFSZ 1B,1
	GOTO BeepDel
	RETURN

-----------------------------------------------------
     BUTTON   -   SWITCH   -   PUSH BUTTON
-----------------------------------------------------

In this routine a push button is connected to RA0. 
When it is pushed, a HIGH is delivered to RA0.
To make RA1 a button-line, the second instruction in 
SetUp is: MOVLW 02.  To make RA2 an input: MOVLW 04 

The button-file must be cleared in SetUp.
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	BSF 03,5	;Go to Bank 1		
	MOVLW 01	;Put 01 into W to
	MOVWF 05	;    make RA0 input
	BCF 03,5	;Go to Bank 0
	CLRF 1F		;Clear the button-press file
	GOTO Main

Sw1	BTFSS 05,0	;Test the button. Button is "Active HIGH"
	GOTO Sw2	;Button not pressed
	BTFSC 1F,0	;Button pressed first time?  Test debounce flag
	RETURN		;Button already pressed. Return to Main
Sw1A	DECFSZ 1A,1	;Create short delay
	GOTO Sw1A	;Look again
	BTFSS 05,0	;Is switch still pressed?
	GOTO Sw1B	;It was only noise
	BSF 1F,1	;Button Pressed.  Set button-pressed flag
	BSF 1F,0	;Set debounce flag
	RETURN		;Return to Main
Sw1B	BCF 1F,0	;Clear debounce flag
	RETURN		;Return to Main

Main	CALL Sw
	BTFSC 1F,1	;Test button-press flag to see if button was pressed
	GOTO Main1	;Button pressed
	Display the 	;Button not pressed - carry out a sub-routine to 
	values on a 	;    display values on a display etc. 	
	display etc.
	GOTO Main
Main1	CALL Increment	;Increment the display. (you provide the routine)
	BCF 1F,1	;Clear the button-press flag
	GOTO Main



To connect two buttons to a micro.
The second instruction in SetUp must be the addition of the hex values
of the input lines
E.g: RA1 and RA2 = 0000 0010 + 0000 0100 = 0000 0110 = 06h
     RA3 and RA5 = 0000 1000 + 0010 0000 = 0010 1000 = 28h


SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 03	;Put 03 into W to
	MOVWF 05	;    make RA0 and RA1 input
	BCF 03,5	;Go to Bank 0
	CLRF 1F		;Clear the button-press file
	GOTO Main

Sw1	BTFSS 05,0	;Test the first button. Button1 is "Active HIGH"
	GOTO Sw2	;Button not pressed
	BTFSC 1F,0	;Button pressed first time?  Test debounce flag
	RETURN		;Button already pressed. Return to Main
Sw1A	DECFSZ 1A,1	;Create short delay
	GOTO Sw1A	;Look again
	BTFSS 05,0	;Is switch still pressed?
	GOTO Sw1B	;It was only noise
	BSF 1F,1	;Button Pressed.  Set button-pressed flag
	BSF 1F,0	;Set debounce flag
	RETURN		;Return to Main
Sw1B	BCF 1F,0	;Clear debounce flag
	RETURN		;Return to Main

Sw2	BTFSS 05,0	;Test the second button. Button2 is "Active HIGH"
	GOTO Sw2	;Button not pressed
	BTFSC 1F,2	;Button pressed first time?  Test debounce flag
	RETURN		;Button already pressed. Return to Main
Sw2A	DECFSZ 1A,1	;Create short delay
	GOTO Sw2A	;Look again
	BTFSS 05,0	;Is switch still pressed?
	GOTO Sw2B	;It was only noise
	BSF 1F,3	;Button Pressed.  Set button-pressed flag
	BSF 1F,2	;Set debounce flag
	RETURN		;Return to Main
Sw2B	BCF 1F,2	;Clear debounce flag
	RETURN		;Return to Main

Main	CALL Sw1
	BTFSC 1F,1	;Test button-press flag to see if button was pressed
	GOTO Main1	;Button pressed
	Display the 	;Button not pressed - carry out a sub-routine to 
	values on a 	;    display values on a display etc. 	
	display etc.
	CALL Sw2
	BTFSC 1F,3	;Test button-press flag to see if button was pressed
	GOTO Main2	;Button pressed
	GOTO Main
Main1	CALL Increment	;Increment the display. (you provide the routine)
	BCF 1F,1	;Clear the button-press flag
	GOTO Main
Main2	CALL Decrement	;Decrement the display. (you provide the routine)
	BCF 1F,3	;Clear the button-press flag
	GOTO Main



-----------------------------------------------------
                  CALLS - a list of suitable Calls:
-----------------------------------------------------
Any names can be used. Keep the length to less than 8 letters.
	
	CALL Alarm	
	CALL Beep
	CALL Button
	CALL Count
	CALL Dec
	CALL Delay
	CALL Display
	CALL Find
	CALL HeeHaw
	CALL Inc
	CALL Look
	CALL Loop
	CALL Main
	CALL Send
	CALL Show
	CALL Siren
	CALL Sound
	CALL Sw
	CALL Switch
	CALL Table
	CALL Test
	CALL Toggle
	CALL Tone


-----------------------------------------------------
                  CALL A TABLE
-----------------------------------------------------

Load a value into W and CALL Table1. The micro will look down the
table. If the value you have loaded into W is 00, the first value 
in the table will be fetched. If the value is 01, the second value 
will be fetched. 
The micro adds the value in W to the Program Counter and this
causes the micro to jump down the table. The PC naturally increments 
to the next instruction and that's why 3Fh is selected if the loaded 
value is 00. 


	
Table1	ADDWF 02h,1	;Add W to the Program Counter to create a jump.
	RETLW 3Fh	;0    format= gfedcba
	RETLW 06h	;1
	RETLW 5Bh	;2
	RETLW 4Fh	;3
	RETLW 66h	;4
	RETLW 6Dh	;5
	RETLW 7Dh	;6
	RETLW 07h	;7
	RETLW 7Fh	;8
	RETLW 6Fh 	;9


Main	- - - - -
	MOVLW 00h	;load a value into W. 00h will pick up 3Fh from table
	CALL Table1	;The micro will return with 3Fh in W
	MOVWF 06	;Move 3Fh to output Port B (file 06)
	- - - - -
	- - - - - 


-----------------------------------------------------
                  DECREMENT A FILE
-----------------------------------------------------

	DECF 1A,1	;Puts the new value into the file



-----------------------------------------------------
       DECREMENT A FILE and SKIP WHEN IT IS ZERO
-----------------------------------------------------

	DECFSZ 1A,1	;Puts the new value into the file
	Not zero	;Goes here if file is not zero
	zero!		;Goes here if file is zero!

-----------------------------------------------------
                  DELAYS
-----------------------------------------------------

This delay creates FFh loops. Each loop takes 4uS
Total time: 256 x 4 = 1024uS =  say 1mS

Del	NOP
	DECFSZ 1A,1	;Decrement file 1A
	GOTO Del	;Loop until file 1A is zero
	RETURN


This delay creates 80h loops. Each loop takes 4uS
Total time: 128 x 4 = 512uS

Del	MOVLW 80h	;Put 80h into W
	MOVWF 1A	;Copy 80h into file 1A
DelA	NOP
	DECFSZ 1A,1	;Decrement file 1A
	GOTO DelA	;Loop until file 1A is zero
	RETURN


NESTED DELAY
This delay creates FFh loops (determined by file 1B). 
Each loop takes 4uS and there are 256 inner loops = 1024uS = 1mS
This inner loop is executed 256 times via file 1B. 
Total time = 260mS

Del	NOP
	DECFSZ 1A,1	;Decrement file 1A
	GOTO Del	;Loop until file 1A is zero
	DECFSZ 1B,1	;Decrement file 1B
	GOTO Del	;Loop until file 1B is zero
	RETURN


For a delay between 1mS and 260mS, you will need to pre-load file 1B:

Del 	MOVLW 7Dh
	MOVLW 1B
Del1	NOP
	DECFSZ 1A,1	;Decrement file 1A
	GOTO Del1	;Loop until file 1A is zero
	DECFSZ 1B,1	;Decrement file 1B
	GOTO Del1	;Loop until file 1B is zero
	RETURN


-----------------------------------------------------
                  EEPROM ROUTINES
-----------------------------------------------------


To load EEPROM:

	ORG 2100h  
	DE 84h, 16h, 23h, 80h, 0CAh, 32h, 7Bh, 0A2h 
	DE 34h, 53h, 25h, 02h, 0FFh, 20h, 03h, 04h 



The sub-routine to read a value in the EEPROM is shown below. 
It reads EEPROM data at location specified in EEADR and 
returns the value in W 


EERead	BSF Status, RP0 ;Go to Bank 1
	BSF EECON1, RD	;Set the RD bit   	
	BCF Status, RP0 ;Back to bank 0  
	MOVF 08, W	;EE Data is file 08.  Put into W 
	RETURN


The sub-routine to write to EEPROM is shown below.     


Delay1	MOVLW 13h	;Create a 20mS delay
	MOVWF 1B	;20 loops
Delay1a	NOP
	DECFSZ 1A,1	;4uS x 256 loops=approx 1mS
	GOTO Delay1a
	DECFSZ 1B
	GOTO Delay1a
	RETLW 00

EEWrite	MOVWF 08	;W to EEData
	BSF 03,5	;Switch to bank 1
	BCF 0B,7	;Disable interrupts
	BSF 08,2	;Write Enable bit set
	MOVLW 55h	;Toggle EEPROM Control 
	MOVWF 09	;    Register bits
	MOVLW 0AAh	;Toggle EEPROM Control
	MOVWF 09	;    Register bits
	BSF 08,1	;Begin write sequence
	CALL Delay1	;Call Delay1
	BCF 08,2	;Disable any further writes
	BSF 0B,7	;Enable Interrupts
	BCF 03,5	;Back to data bank 0
	RETURN



-----------------------------------------------------
                  GOTO
-----------------------------------------------------
To make the micro go to another part of your program, use the 
GOTO instruction. Simply write the word "GOTO." Following 
GOTO, we place a "label" The label may be "Sw1" or "Count" or "Test." 
"Sw1" or "Count" or "Test." will appear in the first column
of your program. The instruction "GOTO Test" will take the micro
to the line containing the label: Test. 
The micro will not return to the instruction following GOTO Test,
unless you have a label at the line such as "Togu1" and include 
an instruction GOTO Togu1 in your program. 
If you GOTO a sub-routine, the last instruction in the 
sub-routine cannot be RETURN as the micro does not have a return 
address in its memory - on the STACK. 
The only time a return address is placed on the stack is when a CALL
is executed. 

	GOTO Alarm	
	GOTO Beep
	GOTO Button
	GOTO Count
	GOTO Dec
	GOTO Delay
	GOTO Display
	GOTO Find	
	GOTO HeeHaw

At the end of the sub-routine will be a GOTO Main or GOTO another 
sub-routine. It does not have to be the last instruction in a 
sub-routine but it is the only exit instruction you can use. 
You cannot use "RETURN."



-----------------------------------------------------
                  HEX VALUES
-----------------------------------------------------
All the files in a micro have 8 bits. 
All the input/output ports have 8 lines.
The lowest bit or line is shown as a "1" in the folling value:

   0000 0001

The lowest bit is called Bit0   
The lowest line is called RA0  or RB0  or GP0

The next line (in/out line) or "bit" is called RA1, RB1 or GP1:

    0000 0010

The lowest line has a value of zero when not active and ONE when active. 
The next has a vaue of TWO when active.
The next has a value of FOUR when active.
The next has a value of EIGHT when active.

The 4 in/out lines have the following 16 possibilities:

	0000
	0001
	0010
	0011
	0100
	0101
	0110
	0111
	1000
	1001
	1010
	1011
	1100
	1101
	1110
	1111

They are given the following values:

	0000 = 0
	0001 = 1
	0010 = 2
	0011 = 3
	0100 = 4
	0101 = 5
	0110 = 6
	0111 = 7
	1000 = 8
	1001 = 9
	1010 = A
	1011 = B
	1100 = C
	1101 = D
	1110 = E
	1111 = F

The four HIGH lines have the same format:


	0000 0000  = 00
	0001 0000  = 10
	0010 0000  = 20
	0011 0000  = 30
	0100 0000  = 40
	0101 0000  = 50
	0110 0000  = 60
	0111 0000  = 70
	1000 0000  = 80
	1001 0000  = 90
	1010 0000  = A0
	1011 0000  = B0
	1100 0000  = C0
	1101 0000  = D0
	1110 0000  = E0
	1111 0000  = F0

The four HIGH and LOW lines produce 256 combinations. 
Here are some: 

	0011 0101  = 35 
	1011 0000  = B0
	1110 1001  = E9
	1011 0000  = B0
	0110 1110  = 6E

	0101 0000  = 50
	1001 1011  = 9B
	0110 1011  = 6B


The value to make RA2 and RA5 input:

	0010 0100  = 24

-----------------------------------------------------
                  INCREMENT A FILE
-----------------------------------------------------

	INCF 1A,1	;Puts the new value into the file



-----------------------------------------------------
            INPUT
-----------------------------------------------------
To make a line (or lines), an INPUT, it must be given the 
value "1."  This can be done anywhere in the program 
but it is best to put the instruction in SetUp. 
This makes it easy to locate when trouble-shooting. 
The instructions between BSF and BCF in the routine below are 
operating on a register in bank 1 and this is the in/out
control register for PortA.  It is NOT PortA. It is the 
register that determines the IN or OUT condition of 
each line in PortA and is called the TRIS register. 


SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 0F	;Put 0000 1111 into W to
	MOVWF 05	;  make RA0, RA1, RA2, and RA3 input
	BCF 03,5	;Go to Bank 0			
	GOTO Main


-----------------------------------------------------
            INPUT A VALUE
-----------------------------------------------------
The input lines for a 12C508A are: GP0, GP1, GP2, GP3, GP4 and GP5.
GP3 is input-only and this line is usually the chosen input.
The input lines for a PIC16F84A are: RA0,RA1,RA2,RA3,RA4,RA5,RA6,RA7.
RB0,RB1,RB2,RB3,RB4,RB5,RB6,RB7. 

To input a value (A HIGH or LOW), make sure the line is an 
input. This is done in SetUp:

SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 01	;Put 0000 0001 into W to
	MOVWF 05	;  make RA0 input
	BCF 03,5	;Go to Bank 0			
	GOTO Main

In Main, test the input line:

Main	BTFSS 05,0
	GOTO - - -
	- - - - -
	- - - - -



-----------------------------------------------------
                  INVERT
-----------------------------------------------------
An output line can be made an input (or vise versa) at any time during 
the running of a program. The following instructions work for both a 
PIC12C508A and a PIC16F84. 
The GPIO (General Purpose In/Out) lines on a PIC12C508A are GP0, GP1, 
GP2, GP3, GP4 and GP5, (GP3 as an input-only line). These lines 
correspond to RB0 to RB5 on a PIC16F84A. 

Suppose GP0 (RB0) is an output. To make GP0 (RB0) an input, use the following:
Note: file 06 in bank1 is the TRIS file. It determines the input or 
output nature of each line in Port6. The lowest bit in the TRIS file 
is SET to make line RB0 an input. The line is then looked-at to see if
it is HIGH and then the line is turned into an output. 

	- - - - -
	- - - - -
	BSF 03,5	;Go to Bank 1		
	BSF 06,0	;Make the lowest bit in the TRIS file an input
	BCF 03,5	;Go to Bank 0
	BTFSC 06,0	;Look at input line GP0 (RB0)
	GOTO xxxx	;GOTO xxxx if line RB0 is HIGH	
	BSF 03,5	;Go to Bank 1		
	BCF 06,0	;Make the lowest bit in the TRIS file an output
	BCF 03,5	;Go to Bank 0	
	- - - - -
	- - - - -	
	

-----------------------------------------------------
                  LAYOUT OF A PROGRAM
-----------------------------------------------------
Use the following when layingout your program:


SetUp


Table1
Table2


LabelA - put all labels in alphabetical order so they can be found
               quickly.

LabelB

LabelC

LabelD


Main 
	
-----------------------------------------------------
            LOOK for a Table-value
-----------------------------------------------------

See Table-value


-----------------------------------------------------
            LOOP
-----------------------------------------------------
When writing a program, you can use a loop to create a STOP function. 

Loop	NOP
	GOTO Loop 

Inside the loop you can put a tone routine so you can see if the 
micro has reached the location. 

	
Loop	MOVLW A0h	;The length of  HIGH and LOW - frequency of tone
	MOVLW 1A
Tone	NOP
	DECFSZ 1A,1
	GOTO Tone
	MOVLW 80h	;Put 80 into W
	XORWF 06,1	;XOR 80 with Port B. Toggle RB7
	GOTO Loop 	


-----------------------------------------------------
            MAIN
-----------------------------------------------------
Main is the last routine in your program. It is a loop routine
and has a number of CALL and/or GOTO instructions: (If you GOTO 
a sub-routine, the sub-routine must have a GOTO as the exit).


Main 	- - - - -
	- - - - -
	- - - - -
	CALL Delay
	- - - - -
	- - - - -
	- - - - -
	GOTO Main

-----------------------------------------------------
            OUTPUT
-----------------------------------------------------
To make a line (or lines), an OUTPUT, it must be given the 
value "0."  This can be done anywhere in the program 
but it is best to put the instruction in SetUp. 
This makes it easy to locate when trouble-shooting. 
The instructions between BSF and BCF in the routine below are 
operating on a register in bank 1 and this is the in/out
control register for PortB.  It is NOT PortB. It is the 
register that determines the IN or OUT condition of 
each line in PortB and is called the TRIS register. 


SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 00	;Put 0000 0000 into W to
	MOVWF 06	;    make all PortB lines output
	BCF 03,5	;Go to Bank 0			
	GOTO Main

In Main, output a HIGH on PortB (file 06):

Main	- - - - -
	MOVLW 0FFh	;
	MOVWF 06	;Output a HIGH to the 8 lines of PortB
	- - - - -
	- - - - -

-----------------------------------------------------
                  OVERFLOW
-----------------------------------------------------
If  a value is added to a file, overflow may occur. 
To see if overflow occurs, test the Carry/Borrow bit in the Status file 
(file 03,0). It will be SET if overflow occurs. 

Add	MOVLW 0CCh	;Put CCh into W
	ADDWF 0Eh	;CC will be added to the contents of file "E."
	BTFSS 03,0	;Test the carry bit in the Status file
	GOTO AAA	;Micro will go to AAA if no overflow
	GOTO BBB	;Micro will go to BBB if overflow occurs 
           

-----------------------------------------------------
                  RETURN
-----------------------------------------------------
The PIC12C508A does not have a RETURN instruction.  
If you use RETURN in your program, the assembler will replace 
it with RETLW 00 when creating a .hex file for a PIC12C508A.
 


-----------------------------------------------------
                  REVERSE
-----------------------------------------------------
An output line can be made an input (or vise versa) at any time during 
the running of a program. The following instructions work for both a 
PIC12C508A and a PIC16F84. 
The GPIO (General Purpose In/Out) lines on a PIC12C508A are GP0, GP1, 
GP2, GP3, GP4 and GP5, (GP3 as an input-only line). These lines 
correspond to RB0 to RB5 on a PIC16F84A. 

Suppose GP0 (RB0) is an output. To make GP0 (RB0) an input, use the following:
Note: file 06 in bank1 is the TRIS file. It determines the input or 
output nature of each line in Port6. The lowest bit in the TRIS file 
is SET to make line RB0 an input. The line is then looked-at to see if
it is HIGH and then the line is turned into an output. 

	- - - - -
	- - - - -
	BSF 03,5	;Go to Bank 1		
	BSF 06,0	;Make the lowest bit in the TRIS file an input
	BCF 03,5	;Go to Bank 0
	BTFSC 06,0	;Look at input line GP0 (RB0)
	GOTO xxxx	;GOTO xxxx if line RB0 is HIGH	
	BSF 03,5	;Go to Bank 1		
	BCF 06,0	;Make the lowest bit in the TRIS file an output
	BCF 03,5	;Go to Bank 0	
	- - - - -
	- - - - -	
	

-----------------------------------------------------
            SetUp
-----------------------------------------------------
This is the first sub-routine in your program. It sets up the 
port (or ports) in the microcontroller. A port has up to 8 lines
and these can be inputs or outputs. (GP3 for a PIC12C508A can 
be input-only).
The input lines for a 12C508A are: GP0, GP1, GP2, GP3, GP4 and GP5.
GP3 is input-only and this line is usually the chosen input.
The output lines for a 12C508A are: GP0, GP1, GP2, GP4 and GP5.
An input line can be changed to an output at any time during 
the running of a program. 
An output line can be changed to an input at any time during 
the running of a program. 
The input/output lines of a PIC12C508A are in file 06.
To make GP0 an input, place the following instructions in SetUp:

SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 01	;Put 0000 0001 into W to
	MOVWF 06	;    make GP0 input
	BCF 03,5	;Go to Bank 0			
	GOTO Main

To make GP0 an output, place the following instructions in SetUp:

SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 00	;Put 0000 0000 into W to
	MOVWF 06	;    make GP0 input
	BCF 03,5	;Go to Bank 0			
	GOTO Main

To make any GP line an input, place "1" in the corresponding 
 position for the value loaded into W:

SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 08	;Put 0000 1000 into W to
	MOVWF 06	;    make GP3 input
	BCF 03,5	;Go to Bank 0			
	GOTO Main

To create two input lines:

SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 12	;Put 0001 0010 into W to
	MOVWF 06	;    make GP1 and GP4 input
	BCF 03,5	;Go to Bank 0			
	GOTO Main

The input lines for a PIC16F84A are: RA0,RA1,RA2,RA3,RA4,RA5,RA6,RA7.
RB0,RB1,RB2,RB3,RB4,RB5,RB6,RB7. 


SetUp	BSF 03,5	;Go to Bank 1		
	MOVLW 03	;Put 03 into W to
	MOVWF 05	;    make RA0 and RA1 input
	BCF 03,5	;Go to Bank 0			
	GOTO Main



-----------------------------------------------------
            STATUS FILE
-----------------------------------------------------
The STATUS file is file 03. 
Bit0 is the Carry/Borrow
Bit1 is the Digit Carry/Borrow
Bit2 is the Zero bit
Bit3 is the Power Down bit
Bit4 is the Time-out bit
When bit5 is SET Bank1 is selected 
When bit5 is CLEAR Bank0 is selected

-----------------------------------------------------
            STOP
-----------------------------------------------------
The micro does not have a STOP or HALT instruction. 
When writing a program, you can use a loop to create a STOP function. 

Loop	NOP
	GOTO Loop 

Inside the loop you can put a tone routine so you can see if the 
micro has reached the location. 

	
Loop	MOVLW A0h	;The length of  HIGH and LOW - frequency of tone
	MOVLW 1A
Tone	NOP
	DECFSZ 1A,1
	GOTO Tone
	MOVLW 80h	;Put 80 into W
	XORWF 06,1	;XOR 80 with Port B. Toggle RB7
	GOTO Loop 	



-----------------------------------------------------
            TABLE  - see also CALL A TABLE
-----------------------------------------------------

Load a value into W and CALL Table1. The micro will look down the
table. If the value is 00, the first value in the table will be 
fetched. If the value is 01, the second value will be fetched. 
The micro adds the loaded-value into the Program Counter and this
causes the micro to jump down the table. The PC naturally increments 
and that's why 3Fh is selected if the loaded value is 00. 
Any values with a letter as the first part of the hex value must 
begin with a "0"  E.g: FFh = 0FFh    CDh = 0CDh
The end of a table can be identified by using 0FFh (providing FFh is 
not a required table value).  In Main, look for 0FFh as a table value. 


	
Table1	ADDWF 02h,1	;Add W to the Program Counter to create a jump.
	RETLW 3Fh	;0    format= gfedcba
	RETLW 06h	;1
	RETLW 5Bh	;2
	RETLW 4Fh	;3
	RETLW 66h	;4
	RETLW 6Dh	;5
	RETLW 7Dh	;6
	RETLW 07h	;7
	RETLW 7Fh	;8
	RETLW 6Fh 	;9
	RETLW 0FFh


Main	- - - - -
	MOVLW 00h	;load a value into W. 00h will pick up 3Fh from table
	CALL Table1	;The micro will return with 3Fh in W
	MOVWF 06	;Move 3Fh to output Port B (file 06)
	- - - - -
	- - - - - 



-----------------------------------------------------
            Table-value
-----------------------------------------------------

To "Look for" or "fetch" a table value, you need 2 instructions. 
To fetch the first table value, load W with 00. 
To fetch the second table value, load W with 01 etc. 
The second instruction is CALL Table1
The micro will return with the table-value in W:


Table1	ADDWF 02h,1	;Add W to the Program Counter to create a jump.
	RETLW 3Fh	;0    format= gfedcba
	RETLW 06h	;1
	RETLW 5Bh	;2
	RETLW 4Fh	;3
	RETLW 66h	;4
	RETLW 6Dh	;5
	RETLW 7Dh	;6	

Main	- - - - -
	- - - - -
	MOVLW 04
	CALL Table1
	- - - - -
	- - - - -

The micro will return with 66h in W


-----------------------------------------------------
            TEMPLATE
-----------------------------------------------------
Use this template as a start to writing your program. 
This is not a fully-functional program.
It is a set of example-instructions.
Delete the unwanted instructions. 

	;Expt1.asm
        ;Project: Turning on a -----
	;This program can be assembled for a PIC12C508A or PIC16F84
	

	ORG 0		;This is the start of memory for the program
SetUp	BSF 03,5	;Go to Bank 1
	MOVLW 01	;Load W with 0000 0001
	MOVWF 05	;Make RA0 input
	MOVLW 01	;Load W with 0000 0001
	MOVWF 06	;Make RB0 or GP0 input
	BCF 03,5	;Go to Bank 0 - the program memory area
	GOTO Main

Delay	NOP		;Create approx 250mS delay
	DECFSZ 1A,1
	GOTO Delay
	DECFSZ 1B,1
	GOTO Delay
	RETURN

Look	CLRF 0C		;Count-down file		
	- - - - -
	CALL Delay	;250mS delay
	BSF 03,5	;Go to Bank 1	
	MOVLW 04	;Load W with 0000 0100
	MOVWF 05	;Make RA2 input & RA3 output
	BCF 03,5	;Go to Bank 0 - the program memory area.	
	RETURN


		
Main	BTFSS 05,0	;Test the input line on port A
	GOTO Main2	;Button not pressed
	BSF 06,0	;Button pressed. Turn on LED
	CALL Delay
	- - - - - 
	CALL Look
Main2	BCF 06,0	;Button not pressed. Turn off LED
	GOTO Main	;Loop Main	

	END		;Tells assembler end of program




-----------------------------------------------------
                  TOGGLE
-----------------------------------------------------
Toggle changes the state of an output. You do not have to know 
the state of the output before the operation. The output changes 
from HIGH to LOW or LOW to HIGH. You do not know the output after
the operation. This operation is not suitable if you want to 
leave an output low when not in use so that the output current 
is low. 

To toggle RB0 use: MOVLW 01h  (see line 8 in subroutine below)
To toggle RB1 use: MOVLW 02h
To toggle RB2 use: MOVLW 04h
To toggle RB3 use: MOVLW 08h
To toggle RB4 use: MOVLW 10h
To toggle RB5 use: MOVLW 20h
To toggle RB6 use: MOVLW 40h

Toggle	MOVLW 80h	;Put 80 into W
	XORWF 06,1	;XOR 80 with Port B. Toggle RB7


-----------------------------------------------------
            TRIS
-----------------------------------------------------
"Tris" is the file (register) that determines if each bit in a  
Port is input or output. 
See Library of Routines for more details. 
 
















