LIBRARY
OF
ROUTINES
Timing to Z

TIMING
Working with a high-speed micro, you have to understand timing values such as nano seconds, microseconds and milliseconds:

1nS = 1 nano second
1uS = 1 microsecond
1mS = 1 millisecond
1 Sec = 1 second
100nS = 1/10th microsecond
1,000nS = 1 microsecond = 1uS
1,000uS = 1mS
1,000mS = 1 second
1,000,000uS = 1 second

An example of timing: An instruction to poll an input line (MOVF 06) takes 1microsecond so that if a pulse is present on the input for 100nano seconds, it will be too brief for the micro to detect. The
pulse will have to be "extended" to greater than 1,000 nano-seconds.
1nS = one thousandth of a microsecond!

TOGGLE
TOGGLE changes the condition of a line from output to input or input to output. You do not have to know the condition of the line before the instruction.

In Main, call ToggGP0

CALL ToggGP0

After main, include the Togg sub-routine:

To Toggle GP0:
ToggGP0       MOVLW 01       ;Put 01 into W
XORWF 06,1     ;XOR 01 with file 06

To Toggle GP1:
ToggGP1       MOVLW 02         ;Put 02 into W
XORWF 06,1      ;XOR 01 with file 06

To Toggle GP2:
ToggGP2       MOVLW 04         ;Put 04 into W
XORWF 06,1        ;XOR 01 with file 06

To Toggle GP4:
ToggGP4       MOVLW 10h         ;Put 10h into W
XORWF 06,1        ;XOR 01 with file 06

To Toggle GP5:
ToggGP5      MOVLW 20h          ;Put 20h into W
XORWF 06,1         ;XOR 01 with file 06

TONES
See sound.

TRACE SIGNAL
A trace signal can be placed in a program to see how far the micro has progressed through the instructions.
See Sound 5. Beep - repeat and 6. Beep...Beep...Beep...

TRANSPARENT SUB-ROUTINE
-See sub-routine.

TRIS
In the PIC'508A, the TRIS file is 06. The TRIS file (or TRIS register) makes each pin (or line or bit) of the port (called the INPUT or OUTPUT port) either an input line or output line. The in/out port for the
PIC'508A is 06.
When a line is OUTPUT, it can be made HIGH or LOW. When it's an INPUT, making it high or low does not have any affect as the line is read by the micro and the value appearing ON THE LINE is passed to the W register.
In the PIC'84, the TRIS files (there are two of them: TRISA and TRISB), are hidden below the two ports, with TRISA below PortA and TRISB below PortB (or at least in a different part of the register bank).
The TRIS files can be accessed by the instruction: TRISA or TRISB or by 5 instructions shown in "setUp."
To make programs transportable between the PIC'508A and PIC'84 (for the PSEUDO'508A), only the TRISB register is common.
To be absolutely accurate: use TRISB for the Pseudo'508A (it's really a PIC'84 chip acting like a PIC'508A) and TRIS 06 for the '508A.

TRUTH TABLE
A table that shows all the possible combinations of inputs and the results of a logic operation.

TURN ON AN OUTPUT

To turn on an output, use the instruction:

MOVWF 06,1

Two things must be done before this instruction can be used.
1. The output bit (line) or port (all the bits or lines) must be made output bits.
This is done by loading W with "0" for each line thus:

For all outputs:                                                  0000 0000
For only GP0 as an output:                                1111 1110
For GP0, GP1, GP2, GP4, GP5  as outputs:       0000 1000

To make one or more lines output, the instructions must be placed in SetUp or at the beginning of the program.

For '508A:

MOVLW 08
TRIS 06

For Pseudo'508A:

BSF 03,5             ;Select Page1
MOVLW 08
MOVWF 06
BCF 03,5             ;Select Page0

2. The following two instructions must be placed within the routine:

To make output line GP0 HIGH:

MOVLW 01             ;0000 0001
MOVWF 06

TYING OUTPUT LINES
When making a PC board, the unused pins of the port(s) should not be connected to ground or positive but left OPEN. It is best to take each one to a solder land and connect another solder land (having
a hole) to the pad so that a component such as a test LED can be connected at some later date.
Leave the port line OPEN and configure the line as an OUTPUT. Write a zero (LOW) to the line and it will consume the least power.
Tying an output line to negative is very dangerous. If you make a mistake in your program and make the line an output and write a "1" to the line, it will be instantly damaged!

VALUE
For example: XORLW value - after the instruction XORLW a value (or number) from zero to FF is placed on the same line as the instruction XORLW. The word value is the same as "literal" or "number" and has the value 00 to FF.

WAIT
See SLEEP or Delay.

WATCHDOG TIMER
The watchdog timer (WDT) is an 18msec (18 millisecond) free-running on-chip RC oscillator that does not require any external components. It is designed to guard against software failure and must be reset at regular intervals to
prevent it overflowing. When enabled it will reset the program when it overflows.
A program must periodically reset the WDT to prevent an overflow and this requires careful programming. The simplest solution is to disable the WDT.
If a microcontroller is turned on via an event such as a sensor-switch in an alarm, you must be sure the program will start-up correctly. This is the purpose of the Watchdog Timer.
Before entering a long routine, such as a delay, the WDT must be reset. If the microcontroller starts up and enters a part of the program and performs random instructions, the WDT will not be reset and eventually it will "time-out" and reset the micro to address 000.
Setting the WDT ON or OFF is done during programming - in the "fuses" section. It is reset in the program with the instruction: CLRWDT.
To increase the time-delay for the watchdog timer, a pre-scaler in the Option register can be set via three bits (bit0, bit1, bit2).
(It's called a pre-scaler or divider circuit but in fact it actually multiplies the duration). The bit values are read as: bit2, bit1, bit0:
 Bit Value: WDT Rate 000 001 010 011 100 101 110 111 1:1 1:2 1:4 1:8 1:16 1:32 1:64 1:128
For example, by setting bits 0, 1 and 2 in the OPTION register, the WDT time-interval can be increased to 128 x 18mS = 2.3 seconds.
In a program, to increase the WDT x 128,  the bits are set as follows:

BSF 01h,0         ;Prescaler bit0
BSF 01h,1         ;Prescaler bit1
BSF 01h,2         ;Prescaler bit2

The pre-scaler must be assigned to the WDT as follows:

BSF 01h,3         ;Prescaler assigned to WDT

To reset the WDT in a program, (to prevent resetting), the instruction is:

CLRWDT

WRITE
Write normally means to write into a memory storage space such as EEPROM so that the data can be kept, even when the microprocessor is turned off. The '508A does not have EEPROM, so any data you require must be placed in Program space - in the form of a table - (in the first 00 to FF bytes) so it can be accessed.
The data in this table cannot be altered however instructions can be written to input data from say GP0, GP1, GP2 and GP3 and loaded into one of the files. The data is input in nibbles and when a second nibble is input, the data can be combined to form a byte. This data can be used in place of stored data. Stored data is stored in the form of a table.
To produce a table, place StartUp at address 000, then type Table1.

StartUp        -----------
----------
GOTO Main

RETLW MM
RETLW NN
RETLW RR
RETLW TT etc

In Main, you will need a sub-routine (such as Display) that requires Table1.

Main            CALL -----
CALL Display
CALL ------

Display         ---- A ------
---- B ------
MOVF 0C,0
CALL Table1
---- E -----
RETURN

The instruction on line C will load W with a jump value for the table. It works like this:
Suppose you require the 4rd value in table1. At line C the instruction will be MOVLW 03. The micro will advance to the instruction CALL Table1 and the program counter will be loaded with the value of the start of Table1. The micro will then go to the location "Table1". At Table1, the instruction is to ADD 03 (the value in W) to file 02.
File 02 is the Program Counter and it is increased by 3. This causes the micro to advance (jump) four addresses down the program to the line RETLW TT. The instruction it finds is: RETURN with TT in W. It returns to line E.

- 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.

E.g:

MOVLW 3C           ;Put 3C in W
MOVWF 1E           :Put 3C into file 1E
XORLW 3C            ;XOR 3C with file 1E

The micro will XOR file 1E with the value 3C. We know file 1E contains the value 3C, so the operation is:
1E:    0011 1100
3C:    0011 1100
0000 0000

BTFSS 03,2          ;Test the Zero flag
GOTO clear
GOTO SET

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.

To see if file 0C is FF:

MOVLW FF          ;Put FF into W
XORWF 0C,0        ;Put result in W
BTFSS 03,2          ;Test the zero flag
------------------          ;Micro goes here if no match
------------------           ;Micro goes here if MATCH!

The second capability of the XOR function is to change a bit to its complement. XOR a bit with "1" will change the bit to its complement.

E.g:
Number:            0110 0101
XOR value:        1111 1111
Result:              1001 1010
Note: Each bit in the result is the complement of the bit in the Number.

Z FLAG  - ZERO FLAG
The Zero flag is located in the STATUS register (File 03) bit2. It is tested after a logic operation to see if the answer is 0000 0000 or if any 1's are in the result.
If the result is 0000 0000, the Z flag is SET i.e. Z=1.

Example:

If a number (also called a literal or value or constant) is XORed with an identical number, the result is 0000 0000 and the Z flag = 1 = SET. This is a COMPARE situation.

e.g: To see if File 0C = 05

MOVF 0C,0         ;Load file 0C into W
XORLW 05h         ;XOR W with 05
BTFSS 03,2         ;Test Z flag in status file.
-----------------          ;Micro goes here if not Match
-----------------          ;Micro goes here if MATCH!

File 0C is loaded into W. W is XORed with 05. If W and 05 are identical the result of the XOR operation will be 0000 0000. The Z flag in the Status file will be SET. Remember: "Not Match" in the above example has only one line of code available. If NOT MATCH needs many lines of code you can use: BTFSC 03,2 and "Match" will have only one line of code available for a GOTO instruction.

TO TEST FOR ZERO IN A FILE:
To test a file to see if it is zero:

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

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.

'508A Proto-1 Module - not complete yet!

The '508A Proto-1 is the name given to an Experimenter's PC board carrying a '508A microcontroller and a prototyping area. The board connects to a 6v battery box with a built-in switch.
The board already contains some components and if you need extra switches or LEDs etc, they can be added to the prototyping area.
During the program development stage, you can use a PSEUDO'508A and when the program is finalised, burn a real '508A.
To use the '508A Proto-1 Module, here is the suggested stages of development.
1. Read the Library of Routines (this chapter)
2. Read the PIC12C508A Instruction set.
- Pseudo '508A
4. Study the projects using the '508A
- Park-O-Timer
- Don't Gamble use SHELL
- Sonar Range Finder
- O's & X's
5. Purchase the PIC burner, with discs, and cable
6. Purchase a few blank '508A's
7. Purchase a PSEUDO'508A
8. Build the '508A Proto-1'See the article:'508A Proto-1
9. Open a Template and call it PTplate-2.ASM
10. Dream up a project and write the code on the template.
11. Use xxxx to convert the code to a .LST file.
12. Put a PSEUDO'508A in the burner location on the Designer Board?
13. useccccc to download the file to the PIC burner on the designer? board?
14. Construct the circuit on the designer section of the '508A Proto-1
15. Take the Pseudo'508A and place it in the socket on the '508A Proto-1 board.
16. Switch on the power and the '508A Proto-1 will run the program.
17. Gradually modify and improve the program until you are completely satisfied with its performance.
18. Burn a '508A chip, design a PC board, and the project is complete.

END OF LIBRARY OF ROUTINES.   Not Copywrite by Colin Mitchell