Library of Routines
Library of Routines
for PIC12F629
Nearly all these instructions also work with
PIC16F628. Just check on Port value(s) and
first available file.
see: Talking Electronics website
A-E E-P P-Z
See the article Start Here with PIC12F629 for helpful programming notes and a map of the
files.
The following is a list of sub-routines, ideas, and help for the PIC12F629. They apply to any
project using the PIC12F629.
They can be put into your program and modified in any way - to suit the input/output lines.
The "Copy and Paste" version of these sub-routines can be found HERE.
Your program should be created on a template in a text editor such as NotePad, using
blank12F629.asm as a starting layout. It provides the correct layout and spacing.
An unusual problem you may get is a failure to compile your program due to hidden
formatting/characters. MPASM will not produce the needed .hex file if any problem exists in a
program but it will produce a .lst file containing the faults. If you open .lst and see unusual
mistakes, they will be due to hidden formatting characters. Simply retype all the wording
around the mistake (in the .asm file) and the program will compile. Do not use EditPad as it
produces hidden characters.
To use the Library of Routines below, go to the NotePad set of "Copy and Paste" Routines
and "Copy and Paste" them into your program in another NotePad, as needed.
Additional sub-routines can be found in the PIC Programming Course. This is on the
subscription section of the website.
Make sure each sub-routine uses a file (a register) with a name (a hex number) that doesn't
clash with any other sub-routine you have created.
Make sure CALLs go to a sub-routine that has a RETURN (use retlw 00) to bring the micro
back to the correct pace in the program and create Labels that let you know what the sub-
routine is doing.
The following library is presented in alphabetical order. Using these sub-routines will get you
started very quickly and will assist you with 70% - 90% of a new project.
Read through the entire library so you know what is possible.
Simply think of a word or requirement, go to the word and read about it. Many of the sub-
routines are also available in the "Copy and Paste" section.
Paste them into your program and modify them to suit. The micro will take each instruction
and carry it out. Make sure you have a RETLW 00 that brings the micro back to Main.
Get each sub-section working correctly before adding more instructions. Gradually build up
your program and save it as a NEW NAME so it can be recalled if a major problem develops.
Do not save it as the previous name as MPASM will sometimes not assemble it (it will think it
has already been assembled and - do nothing!!) and you will wonder why the improvements
do not work!!
bsf GPIO,5
If more than one line needs to be made "HIGH" or "LOW," at the same time, you need to use:
movlw xxh
iorwf GPIO,1
This is necessary as the PIC12F629 will fail to set two or more lines via the following
instructions:
bsf GPIO, 0
bsf GPIO, 2
The author has found the PIC12F629 will fail to set the second line.
Address a File
This means "to act on" or "work with" a file. It can be to "move a value into a file," "increment
a file," "decrement a file" or other similar operation. Only files can be addressed (the
instructions in the program cannot be address or altered). The files we are talking about are
the "empty" files from 20h to 5F. None of the program or the values in the tables can be
altered. The values in a table can be accessed and copied by a set of instructions covered in
CALL Table.
Typical addressing instructions are:
bank select: bsf 03,5 selects bank 1 - this is where the OPTION register is accessed.
bcf 03,5 selects bank 0 - this is the normal programming area
carry 03,0 Test the carry bit:
btfss 03,0 ;skip if a carry-out of the most significant bit has
occurred.
prescaler bsf Option_reg,3 see: Watchdog timer
assigned to or: Option register is in bank 1
WDT bsf 81h,3
prescaler bcf Option_reg,3 Timer0 is in bank 0
assigned to or:
Timer0 bcf 81h,3
GPIO file 05h see input/output port. "Setting the port bits HIGH or LOW"
TRISIO file 85h see "Setting the ports bits input or output"
Status file 03 Contains the carry, bank select and zero flags.
zero flag 03,2 zero flag is bit 2 of file 03 (the STATUS file)
zero bit 1 = The result of an arithmetic or logic operation is zero
0 = The result of an arithmetic or logic operation is not zero
You can look at bit 2:
btfss 03,2 ; skip if the zero bit is SET
OPERATOR Example
Current/Return Program
$ goto $ + 3
Counter
( Left parenthesis 1 + ( d * 4)
) Right parenthesis (length + 1) * 256
! NOT (logical complement) if ! (a == b)
– Negation (2's complement) – 1 * length
~ Complement flags = ~ flags
* Multiply a=b*c
/ Divide a=b/c
bz - branch on zero flag being set to "1" See more instructions in "MACRO."
timeout
CALL Delay
xxxxxxxxx
--------
--------
--------
Carry - see also ROTATE for one of the operations involving CARRY.
The carry bit is located in the STATUS register (file 03) and is bit 0.
The carry bit is SET when the result of an operation is larger than 0ffh.
The carry bit is SET when the result of an operation is less than zero.
It is cleared by the instruction:
BCF 03,0 - clear bit0 in file 03
Carry is SET by the instruction:
BSF 03,0
cblock 0x20 ;define the start of the files. The first "file" or "register" for a 12F629 is
20h
Lowbyte ;this will be file 20h
Medbyte ;this will be file 21h
Hibyte ;this will be file 22h etc etc etc
endc
This method of declaring variables is quite good, but macros cannot take advantage of it.
cblock can use other files, starting at say 4Ch: The 3 files d1, d2 and d3 are files for a
delay routine and are files 4Ch, 4Dh and 4Eh.
cblock 0x4C
d1
d2
d3
endc
org 20h
save ds 1
flag ds 2
count ds 1
csave ds 1
temp ds 2
ontime ds 1
oftime ds 1
timer ds 3
freq ds 2
mode ds 1
rate ds 1
hi ds 1
low ds 1
delay ds 3
The compiler will allow one file (20h) for "save" and two files for "flag." timer will be given 3
files.
When writing the program, you write the instruction such as incf flag,1 for the first flag file
and
btfss flag+1 for the second flag file.
Change Direction
The direction of an input/output line (or a whole port) can be changed at any time during the
running of a program. The file we are loading or setting or clearing is file 85. It is in Bank 1.
This file is called TRISIO.
See also: SetUp for setting up the Input/Output lines. See Input for the instruction(s) to make
a line Input. See Output to make a line Output. Lines are changed by setting or clearing bits
of the TRISIO register. This is called BIT MANIPULATION. This prevents touching (and
upsetting) other lines.
See Toggle to change the STATE of a line (from HIGH to LOW or LOW to HIGH).
Compare
To compare two values, you can use XOR.
To compare two numbers, they are XORed together and if they are the same, the Z flag will
be set. Take two numbers:
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.
Same: Z flag is SET (ie 1) when the two files are the SAME!
Comparison
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:
E.g:
OR
Suppose a file (file 3E) 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.
The SUBWF operation below subtracts the W register (via a process called the 2's
complement method) from file 3E and the carry flag in the Option register (file 03) will be SET
if 3E is equal to W or more than W (i.e: 4 or more).
The carry bit in the Option file is bit 0. Therefore we test bit 0 of file 03:
BTFSS 03,0
The result in 3E can only be 1, 2, or 3.
Config - configuration
The configuration word is located at address 2007h and is only accessed during
programming.
'__CONFIG' directive is used to embed configuration data within .asm file.
Note the double underbar at the front of "config."
Some of the configuration settings:
_CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _LVP_OFF
Each setting has a single underbar and a "&" sign, with a single space between "&" sign.
Oscillator:
000 = LP oscillator Low power crystal on GP4 and GP5
001 = XT oscillator Crystal/resonator on GP4 and GP5
010 = HS oscillator High Speed crystal/resonator on GP4 and GP5
011 = EC: I/O function on GP4 ClkIn on GP5
100 = INTOSC oscillator I/O on GP4 & GP5
101 = INTOSC ClkOut on GP4 I/O on GP5
110 = RC oscillator I/O function on GP4 RC on GP5
111 = RC oscillator ClkOut on GP4 RC on GP5
Count
If you want to count the number of pulses from a push-button or switch, the easiest way is to
increment a file. A file will hold up to 255. The result is stored as a hexadecimal number from
01 to FF.
If you want to count to three, pre-load a file with 3 and decrement it and detect zero.
Debug
This is not a term used in creating a program in PIC language, however we have two
suggestions for finding a "bug" or problem in a program.
1. Go back to your previously saved version and note the differences in the programs. Try to
visually detect the fault.
2. "Home-in" on the faulty section and see how far the micro is getting through by inserting a
Wait instruction. (See Wait) A LED on an output line will illuminate to indicate the micro has
reached the instruction.
0 0000 0000 0 16 0001 0000 10h 32 0010 0000 20h
1 0000 0001 1 17 0001 0001 11h 33 0010 0001 21h
2 0000 0010 2 18 0001 0010 12h 34 0010 0010 22h
0010
3 0000 0011 3 19 0001 0011 13h 35 23h
00110
4 0000 0100 4 20 0001 0100 14h 36 0010 0100 24h
5 0000 0100 5 21 0001 0101 15h 37 0010 0101 25h
6 0000 0110 6 22 0001 0110 16h 38 0010 0110 26h
7 0000 0111 7 23 0001 0111 17h 39 0010 0111 27h
8 0000 1000 8 24 0001 1000 18h 40 0010 1000 28h
9 0000 1001 9 25 0001 1001 19h 41 0010 1001 29h
10 0000 1010 A 26 0001 1010 1Ah 42 0010 0010 2Ah
0010
11 0000 1011 B 27 0001 1011 1Bh 43 2Bh
10110
12 0000 1100 C 28 0001 1100 1Ch 44 0010 1100 2Ch
13 0000 1101 D 29 0001 1101 1Dh 45 0010 1101 2Dh
14 0000 1110 E 30 0001 1110 1Eh 46 0010 1110 2Eh
15 0000 1111 F 31 0001 1111 1Fh 47 0010 1111 2Fh
48 0011 0000 30h 64 0100 0000 40h 80 0101 0000 50h
49 0011 0001 31h 65 0100 0001 41h 81 0101 0001 51h
50 0011 0010 32h 66 0100 0010 42h 82 0101 0010 52h
0101
51 0011 0011 33h 67 0100 0011 43h 83 53h
00110
52 0011 0100 34h 68 0100 0100 44h 84 0101 0100 54h
53 0011 0100 35h 69 0100 0101 45h 85 0101 0101 55h
54 0011 0110 36h 70 0100 0110 46h 86 0101 0110 56h
55 0011 0111 37h 71 0100 0111 47h 87 0101 0111 57h
56 0011 1000 38h 72 0100 1000 48h 88 0101 1000 58h
57 0011 1001 39h 73 0100 1001 49h 89 0101 1001 59h
58 0011 1010 3Ah 74 0100 1010 4Ah 90 0101 0010 5Ah
0101
59 0011 1011 3Bh 75 0100 1011 4Bh 91 5Bh
10110
60 0011 1100 3Ch 76 0100 1100 4Ch 92 0101 1100 5Ch
61 0011 1101 3Dh 77 0100 1101 4Dh 93 0101 1101 5Dh
62 0011 1110 3Eh 78 0100 1110 4Eh 94 0101 1110 5Eh
63 0011 1111 3Fh 79 0100 1111 4Fh 95 0101 1111 5Fh
96 0101 0000 60h 112 0111 0000 70h 128 1000 0000 80h
97 0101 0001 61h 113 0111 0001 71h 129 1000 0001 81h
98 0101 0010 62h 114 0111 0010 72h 130 1000 0010 82h
99 0101 0011 63h 115 0111 0011 73h 131 1000 00110 83h
100 0101 0100 64h 116 0111 0100 74h 132 1000 0100 84h
101 0101 0100 65h 117 0111 0101 75h 133 10000 0101 85h
102 0101 0110 66h 118 0111 0110 76h 134 1000 0110 86h
103 0101 0111 67h 119 0111 0111 77h 135 1000 0111 87h
104 0101 1000 68h 120 0111 1000 78h 136 1000 1000 88h
105 0101 1001 69h 121 0111 1001 79h 137 1000 1001 89h
106 0101 1010 6Ah 122 0111 1010 7Ah 138 1000 0010 8Ah
107 0101 1011 6Bh 123 0111 1011 7Bh 139 1000 10110 8Bh
108 0101 1100 6Ch 124 0111 1100 7Ch 140 1000 1100 8Ch
109 0101 1101 6Dh 125 0111 1101 7Dh 141 1000 1101 8Dh
110 0101 1110 6Eh 126 0111 1110 7Eh 142 1000 1110 2Eh
111 0101 1111 6Fh 127 0111 1111 1Fh 143 1000 1111 8Fh
144 1001 0000 90h 160 1010 0000 A0h 176 1011 0000 B0h
145 1001 0001 91h 161 1010 0001 A1h 177 1011 0001 B1h
146 1001 0010 92h 162 1010 0010 A2h 178 1011 0010 B2h
147 1001 0011 93h 163 10100011 A3h 179 1011 00110 B3h
148 1001 0100 94h 164 1010 0100 A4h 180 1011 0100 B4h
149 1001 0100 95h 165 1010 0101 A5h 181 11011 0101 B5h
150 1001 0110 96h 166 1010 0110 A6h 182 1011 0110 B6h
151 1001 0111 97h 167 1010 0111 A7h 183 1011 0111 B7h
152 1001 1000 98h 168 1010 1000 A8h 184 1011 1000 B8h
153 1001 1001 99h 169 1010 1001 A9h 185 1011 1001 B9h
154 1001 1010 9Ah 170 1010 1010 AAh 186 1011 0010 BAh
155 1001 1011 9Bh 171 1010 1011 ABh 187 1011 10110 BBh
156 1001 1100 9Ch 172 1010 1100 7Ch 188 1011 1100 BCh
157 1001 1101 9Dh 173 1010 1101 ADh 189 1011 1101 8Dh
158 1001 1110 9Eh 174 1010 1110 AEh 190 1011 1110 BEh
159 1001 1111 9Fh 175 1010 1111 AFh 191 1011 1111 BFh
192 1100 0000 C0h 208 1101 0000 D0h 224 1110 0000 E0h
193 1100 0001 C1h 209 1101 0001 D1h 225 1110 0001 E1h
194 1100 0010 C2h 210 1101 0010 D2h 226 1110 0010 E2h
195 1100 0011 C3h 211 1101 0011 D3h 227 1110 00110 E3h
196 1100 0100 64h 212 1101 0100 D4h 228 1110 0100 E4h
197 1100 0100 C5h 213 1101 0101 D5h 229 11100 0101 E5h
198 1100 0110 C6h 214 1101 0110 D6h 230 1110 0110 E6h
199 1100 0111 C7h 215 1101 0111 D7h 231 1110 0111 E7h
200 1100 1000 C8h 216 1101 1000 D8h 232 1110 1000 E8h
201 1100 1001 69h 217 1101 1001 D9h 233 1110 1001 E9h
202 1100 1010 6Ah 218 1101 1010 DAh 234 1110 0010 EAh
203 1100 1011 6Bh 219 1101 1011 DBh 235 1110 10110 EBh
204 1100 1100 CCh 220 1101 1100 DCh 236 1110 1100 ECh
205 1100 1101 CDh 221 1101 1101 7Dh 237 11100 1101 EDh
206 1100 1110 CEh 222 1101 1110 DEh 238 1110 1110 2Eh
207 1100 1111 CFh 223 1101 1111 DFh 239 1110 1111 EFh
240 1111 0000 F0h
241 1111 0001 F1h
242 1111 0010 F2h
243 1111 0011 F3h
244 1111 0100 F4h
245 1111 0100 F5h
246 1111 0110 F6h
247 1111 0111 F7h
238 1111 1000 F8h
249 1111 1001 F9h
250 1111 1010 FAh
251 1111 1011 FBh
242 1111 1100 FCh
243 1111 1101 FDh
254 1111 1110 FEh
255 1111 1111 FFh
Decrement
To decrement a file, use the instruction: DECF 3A,1. This puts the new value back into the
file. Do not use DECF 3A,0 as the new value goes into W!
To decrement a file twice, use:
DECF 3A,1
DECF 3A,1
To halve the value of a file, the contents is shifted right:
RRF 3A,1- the file must not have a bit in bit0.
A file can be decremented until it is zero:
DECFSZ, 3A,1
To decrement W we can use the instruction: addlw -1
Delay
A delay sub-routine is needed for almost every program. One of the main purposes is to slow
down the execution of a program to allow displays to be viewed and tones to be produced.
The shortest delay is NOP. This is a "do nothing" instruction that takes 1 micro-second.
You will need one million "NOP's" to produce a 1 second delay.
This is impractical as the program space will only allow about 1,000 instructions.
The answer is to create a loop. If a file is loaded with a value and decremented, it will create a
short delay. The two instructions: DECFSZ 3A,1 and GOTO DelA will take 3uS. 80h loops =
127 loops x 3 + 1 loop x 2uS + 2uS on entry + 1uS on exit = 386uS
Del MOVLW 80h ;Put 80h into W
MOVWF 3A ;Copy 80h into file 1A
DelA DECFSZ 3A,1 ;Decrement file 3A
GOTO DelA ;Loop until file 3A is zero
RETLW 00
A simpler delay routine below decrements a file with 256 loops. Each loop is 4uS and the
result is slightly more than 1,000uS = 1mS. The routine exits with 00h in the file. On the
second execution, the routine performs 256 loops - the file does not have to be pre-loaded.
The longest delay (such as the one below) using a single file is approx 1mS.
Del NOP
DECFSZ 3A,1 ;Decrement file 3A
GOTO Del ;Loop until file 3A is zero
RETLW 00
1mS delay
NESTED DELAYS
To produce delays longer than 1mS, two or more files are needed. Each file is placed around
the previous to get a multiplying effect. The inner delay produces 256 loops, the output file
produces 256 loops of the inner file. This results in 256 x 256 loops = 256mS.
The simplest delay decrements a file to zero. At the end of an execution, a delay contains 00
and this produces the longest delay, the next time it is used.
This means a file does not have to be pre-loaded.
The following is a two-file nested delay. The delay time is approx 260mS (say 1/4Sec):
Del NOP
DECFSZ 3A,1 ;Decrement file 3A
GOTO Del ;Loop until file 3A is zero
DECFSZ 3B,1 ;Decrement file 3B
GOTO Del ;Loop until file 3B is zero
RETLW 00
260mS Delay
If you want a delay between 1mS and 256mS, you will need to pre-load file 3B. For each
value loaded into file 3B, a delay of 1mS will be produced. A 125mS delay is shown below:
cblock 0x20 ;delay files d1, d2 and d3 are at 20h, 21h and 22h
d1
d2
d3
endc
Delay1 movlw 0x0B0h ;499994 cycles
movwf d1
movlw 17h
movwf d2
movlw 0x02
movwf d3
Delay2 decfsz d1, f
goto $+2
decfsz d2, f
goto $+2 ;this sends the micro to: goto Delay2
decfsz d3, f
goto Delay2
goto $+1 ;2 cycle instruction that advances the
goto $+1 ; micro to the next instruction
goto $+1 ; 2 more cycles
retlw 00 checked 3-2-08
The goto $+2 instruction is a 2-cycle instruction that sends the micro 2-instructions down the
program. This instruction has two advantages. It provides a delay of 2 microseconds @4MHz
and saves creating a label.
In the decrementing section of the sub-routine (Delay2), the instruction: goto $+2 makes the
micro go to 2 instructions down the program. This is simply a new and novel way to produce a
delay routine.
The second goto $+2 sends the micro to: goto Delay2. The first instruction could have been
goto $+4 but this routine is designed to "waste" time and every instruction adds to the delay.
A 2 second delay:
cblock 0x20 ;delay files d1, d2 and d3 are at 20h, 21h and 22h
d1
d2
d3
endc
Delay1 movlw 0x11 ;1,999,996 cycles
movwf d1
movlw 0x5D
movwf d2
movlw 0x05
movwf d3
Delay2 decfsz d1, f
goto $+2
decfsz d2, f
goto $+2 ;this sends the micro to: goto Delay2
decfsz d3, f
goto Delay2
goto $+1 ;2 cycle instruction that advances the
goto $+1 ; micro to the next instruction
retlw 00
Code generated by http://www.golovchenko.org/cgi-bin/delay
The following sub-routine will produce a delay of "X" minutes and "Y" seconds.
The following example make the micro wait EXACTLY 930,000,000 instruction cycles.
i.e: 15 minutes 30 seconds
Example:
movlw 0x0F ;This will produce 15 minutes. This the "X" value
call _WAIT_1Min
movlw 0x30 ;This will produce 30 seconds. This is the "Y" value
call _WAIT_1s
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cblock 0x20 - the names of the files below will start at file 20h
;......
minute
second
deci
milli
micro
;......
endc
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_WAIT_1Min ; W * 60,000,000
movwf minute
movlw 0x3B ; 59 * 1,000,000
call _WAIT_1s
movlw 0x09 ; 9 * 100,000
call _WAIT_100m
movlw 0x63 ; 99 * 1000
call _WAIT_1m
movlw 0x63 ; 99 * 10
call _WAIT_10us
goto dec4 ; <= 59,999,996 cycles
NOP
goto $+1
goto $+1
goto $+1
movlw 0x3B
call _WAIT_1s
movlw 0x09
call _WAIT_100m
movlw 0x63
call _WAIT_1m
movlw 0x63
call _WAIT_10us
dec4
decfsz minute,F
goto $-0x0D - micro will go to "NOP" above
return
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_WAIT_1s ; W * 1,000,000
movwf second
movlw 0x09 ; 9 * 100,000
call _WAIT_100m
movlw 0x63 ; 99 * 1000
call _WAIT_1m
movlw 0x63 ; 99 * 10
call _WAIT_10us
goto dec3 ; <= 999,996 cycles
NOP
goto $+1
goto $+1
goto $+1
movlw 0x09
call _WAIT_100m
movlw 0x63
call _WAIT_1m
movlw 0x63
call _WAIT_10us
dec3
decfsz second,F
goto $-0x0B
return
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_WAIT_100m ; W * 100,000
movwf deci
movlw 0x63 ; 99 * 1000
call _WAIT_1m
movlw 0x63 ; 99 * 10
call _WAIT_10us
goto dec2 ; <= 99,996 cycles
NOP
goto $+1
goto $+1
goto $+1
movlw 0x63
call _WAIT_1m
movlw 0x63
call _WAIT_10us
dec2
decfsz deci,F
goto $-9 - micro will go to "NOP" above
return
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_WAIT_1m ; W * 1000 cycles
movwf milli
movlw 0x63 ; 99 * 10?s = .99ms
call _WAIT_10us
goto dec
NOP
goto $+1
goto $+1
goto $+1
movlw 0x63
call _WAIT_10us
dec
decfsz milli,F
goto $-7 - micro will go to "NOP" above
return
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
_WAIT_10us ; W * 10cycles
movwf micro
goto dec5
NOP
goto $+1
goto $+1
goto $+1
dec5
decfsz micro,F
goto $-5
return
A delay can be created using the timers in the PIC12F629. Timer0 is 8-bit and is identified as:
tmr0 equ H'0001'
_1Sec
movlw 05h
movwf delC
nop
decfsz tmr1L,1
goto $-2
decfsz tmr1H,1
goto $-4
decfsz delC,1
goto $-6
retlw 00
Detect a value
If a file has been incremented in a sub-routine you may want to know the value it contains.
You may want to know its exact value or if it is higher or lower than a certain value.
To see if it is an exact value, it is XORed with a known value. See XOR.
To detect a particular value, they are XORed together. See XOR.
You can also detect a particular value by BIT TESTING. You must make sure that all the
numbers being tested can be distinguished by testing a single bit. For example: 1, 2, 4, 8, 10h
can be tested via bits 0, 1, 2, 3, 4. But if 3 is included in your requirement, you cannot test a
single bit.
Different
To find out if two numbers are different, they are XORed together. See XOR
Divide
Simple division such as divide by 2 can be performed by the RRF instruction. Successive
RRF's will divide by 4, 8, sixteen etc. Other divisions are beyond the scope of this course. The
number cannot have a bit in bit0, if an accurate division is required.
DS or ds
This term can be seen in some programs, as:
count ds 2
sum ds 1
dve ds 3
It is the code for: "advances the load pointer by the specified value."
Reserve the number of file locations. ORG must be set for this to work.
It simply allocates two locations for count, one for sum and three for dve This allows the
programmer to produce files for: count, count+1, sum, dve, dve+1, dve+2
DT - Data Table
The DT directive (command) allows you to create a table without having to write:
retlw 40h
retlw 79h
retlw 3Ah
retlw 4Ch
For example:
EEPROM
The PIC12F629 has 128 bytes of EEPROM, from 00h to 7F to permanently 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 for the running of the program. This information will be lost when power is removed.
The 128 bytes of EEPROM requires a special set of instructions to place data into EEPROM.
The actual writing time for this data is very long (in computer terms) and can be done in the
background, while the main program is executing. A flag will SET when the data has been
written and this will allow another byte of data to be entered.
Each EEPROM cell can be written about 1 million times.
Before reading a value in a location in the EEPROM, it must be loaded with a value during
"burning." To load the first location in EEPROM with a value, the following instructions are
placed in a program. The EEPROM starts at location 2100h and the term DE means: "define
EEPROM." There are 128 EEPROM locations and by following the layout in the second table,
any location can be addressed during burning.
READ EEPROM:
The sub-routine to read a value in the EEPROM is shown below.
WRITE TO EEPROM:
The sub-routine to write to EEPROM is shown below.
End
This is a directive, placed at the end of of program to tell the assembler to finish the job of
assembling the instructions into .hex code. The directive is:
end
End of Table
The end of a table can be detected in two different ways.
If a value such as FF is not used in any of the data, it can be used as an End of Table
marker. The sub-routine calling the table must look for 0FFh to detect End of Table.
The other method is to count the number of items in a table and make sure the sub-routine
calling the table doe not CALL values beyond this value.
Equal
To detect if two files are equal, they are XORed together. See XOR.
Equates "equ"
Equates assign an easy to remember label to a numeric value. This value can be a file or bit
in a file.
e.g:
delay1 equ 20h ;every time "delay1" is inserted into a program, the assembler will assign
file 20h.
in your program:
movlw 80h
movwf delay1 ;the assembler will put 80h into file 20h
switch equ 3
LED equ 5
in your program:
btfss GPIO,switch
or
bsf GPIO,LED
The above is a warning that you need to select the correct bank.
You can remove the warning message by inserting the following at the top of your .asm
program
GOTO
The GOTO instruction causes the micro to go to the address identified by a label such as
"No" or "Yes."
BTFSS GPIO,0 ;Is button pressed?
GOTO No ;No
GOTO Yes ;Yes
No RETLW 00
Yes MOVLW 3Fh
etc
btfss GPIO,0
goto $ + 2 ;this will end the micro to instruction y
instruction x
instruction y ;the micro will advance to this instruction
instruction z ;the micro will never execute this instruction!!!!
When writing a program, you must make sure the micro will get back to the "Main" routine.
If you use a "call" instructions such as:
retlw 00
as the "retlw 00" in the delay_1 routine will not take the micro back to "Main" as the return
address has not been remembered with a "goto" instruction.
You can also define a location by writing the LABEL (in this case "Step") and an offset - in this
case +1. The micro will go to "movwf tmr2." This has an advantage. It is easy to see, rather
than writing $–4, when a large number of instructions are to be counted.
Step movlw 25
movwf tmr2
btfsc Sw2
goto newpress
decfsz Sw2,f
goto Step+1
retlw 00
Halt
Do not use the word "Halt" as a label, the assembler does not like it. Use Loop, Pause, Stop,
Wait. See Loop and Stop.
Loop goto $ This will cause the microcontroller to keep looping the same
instruction.
Halve (Half)
To halve (half - divide by two) the value of the contents of a file, it is shifted RIGHT (RRF
1A,1). The number must be an even number (it cannot have a bit in bit0).
Higher
To find out if a number is higher than a know value, a comparison is made. See
Comparison.
Increment
To increment a file, use the instruction: INCF 2A,1. This puts the new value back into the file.
Using INCF 2A,0 puts the new value also into W!
To add two to a file, it can be incremented twice:
INCF 2A,1
INCF 2A,1
To double the value of a file, the contents is shifted left:
RLF 2A,1
A file can be incremented until it "rolls over to zero." Normally a file is decremented to zero
and a skip occurs when it is zero. But the same effect can be produced by incrementing a
file:
INCFSZ, 2A,1
To increment W, use ADDLW, thus: ADDLW 01 or ADDLW 3Bh
Indirect Addressing
A number of files can be addressed by a sub-routine and the information can be moved into
each file or read from each file. The files must be a group.
Suppose we have 8 files and need to read the contents and output it to a display.
The files are: 21h, 22h, 23h, 24h, 25h, 26h, 27h, and 28h.
There are two special files that allow a sub-routine to be created to look at the 8 files and read
the contents.
They are: INDF and FSR
The INDF file is not a real file. It is like a Robot Arm. It reaches down the list of files and picks
up the contents or delivers the contents of a file to the programmer. The file it reaches is
determined by the value in FSR.
FSR is loaded with the address of the file you wish to read or write.
This arrangement has an advantage. By loading FSR with a value, you can reach a file and
by incrementing FSR, you can reach the next file etc.
If you load a value into INDF, you will actually load the value into the file pointed to by FSR.
If you read INDF, you will actually read the contents of the file pointed to by FSR.
You can consecutively read 8, 10 or 20 files or clear 20 files or load into 20 or more files with
a simple looping sub-routine. It's a very powerful feature.
The following instructions put a value of 8Fh into file 21h.
MOVLW 21h ;Load W with start of 8 files
MOVWF 04 ;Load 21h into FSR
MOVLW 8Fh ;Put 8F into W
MOVWF 00 ;Put 8Fh into file 21h
The animation below shows how the information passes to the files:
The following instructions put a value of 8Fh into files 21h, 22h, 23h, 24h, 25h, 26h, 27h and
28h.
The following instructions read files 21h, 22h, 23h, 24h, 25h, 26h, 27h and 28h and outputs to
GPIO (file 05). Output Port 05 has only 5 lines: GP0, GP1, GP2, GP4 and GP5. GP3 is
missing and this makes it difficult to display values from a file.
Input
The six bits of the in/out port GPIO can be made input or output by the value of the bits in a
file called TRISIO. GP3 can only be INPUT.
To make a line INPUT, the corresponding TRISIO bit must be "1."
To make a line OUTPUT, the corresponding TRISIO bit must be "0."
To make a line INPUT (or OUTPUT), the instructions must be placed inside BSF 03,5 and
BCF 03,5.
For example, to make the lowest line of GPIO, an INPUT, the following instructions are
needed:
To make more than one line (with a single instruction) an input, the hex values are added.
Port direction can be changed at any time during the running of a program. You must make
sure that any input or output devices on the line will not upset the running of the program.
In this case it is best to SET or CLEAR a BIT. This involves setting or clearing an individual
bit. This prevents touching any other lines.
Eg: To make the lowest line of port B an input:
Carry out instructions using the input line, then make the line an output:
Jump
There is no "jump" instruction, however the "jump command" is included in instructions such
as
decfsz, btfsc, with the actual instruction meaning to skip or "jump over" the next instruction if
the file is not zero, or the bit is not clear.
The closest instruction is: "goto"
Normally the micro advances down the program, one instruction at a time and reads each
instruction as it comes to it. If you want to jump down a program, you can add a number
(literal) to the Program Counter and the micro will carry out the command.
The instruction is:
addwf pcl,1
Suppose you need to go to one of 5 different sub-routines. This is done by placing a value in
"w:"
movlw 01, or movlw 02, movlw 03, movlw 04, movlw 05
then the instruction: addwf pcl,1
To prevent a jump beyond the 5 "goto's, the instruction: andlw 05h is added here.
Label
This is the name given to each sub-routine. It is placed in the first column of your program
(called an assembly program).
Some names cannot be used as they are reserved by the assembler. Keep the length to less
than 8 letters. Do not use "-" or "/" Use "_" to separate.
Here are some examples:
Alarm Alarm_1 Beep Button Count Dec Delay Display Fast Find Flow Halt
HeeHaw Inc Look Loop Main Send Show Siren Sound Sw Switch Table
Table2 Table_3 Test Try Try_2 Toggle Tone Unit
Load a file
This operation cannot be done directly. A number (a value) is called a LITERAL. It is loaded
into W then the value in W is moved to a file. The two instructions are:
Look at an Input
There is no instruction called "look." If a switch or button is connected to an input line such as
the lowest line on GPIO, the instruction is:
This assumes the switch is connected to the positive rail and the input goes HIGH when the
button is pressed.
This instruction also works for a signal on line GPIO,1. You must make sure line GPIO,1 is an
INPUT via the SetUp routine.
The two instructions after BTFSS GPIO,1 can be "GOTO Yes", "GOTO No" by changing the
first instruction. The decision will depend on the number of instructions for the "Yes" or "No"
answer, as the instruction placed directly after BTFSS GPIO,1 must be a GOTO.
BTFSC GPIO,1 ;Is button pressed?
GOTO Yes ;Yes
GOTO No ;No
Loop
The action of looping is carried out for a number of reasons. The micro does not have a Halt
or Stop feature and must carry out instructions at all times. A loop will hold the micro in one
place.
To get out, a set of instructions such as "look" is needed inside the loop. These instructions
see if a button has been pressed etc. Alternatively, if the watchdog timer is SET, the micro will
come out of the loop and go to location 04. The instructions to create a loop are as follows:
Loop NOP
GOTO Loop
Loop goto $ This will cause the microcontroller to keep looping the same
instruction.
Lower
To find out if a number is lower than a know value, a comparison is made. See Comparison.
Macro
A Macro is similar to a sub-routine. You can call it from anywhere in a program. The aim of a
macro is to save lines of code.
Some assemblers have built-in macros and recognise abbreviations such as the following:
Do not use these instructions unless you know EXACTLY what you are doing.
fr = file register
For instance, we will explain the following instruction in the table below:
Branch on No Zero to addr = btfss 3, 2 goto addr. (file 3, bit 2 is the zero flag)
Test the zero flag. Skip if it is set. In other words skip if the zero flag is set, but BRANCH if it is
not zero!
The normal instructions are as follows:
btfss 3,2
goto tune1
next instruction
A macro can be created to move a number (a literal) into a file, using a single instruction. This
normally requires two instructions:
movlw 64h ;put 64h into W
movwf 2Ch ;move 64h to file 2C
movlf 64h,2Ch ;this instruction will put 64h into file 2C. (a macro must be included in the
program)
To create a macro for the instruction "movlf" the following is placed at the top of your
program:
When you write the instruction: movlf 4Ah,2Fh ;4A will be placed into file 2F.
Main
The Main routine is constantly looped and generally consists of sub-routines that are
CALLed.
Move a file to W
The contents of a file can be moved to W with the following instruction:
MOVF 2A,0 The contents are actually COPIED. The original file still holds the contents.
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:
To multiply any two numbers together requires a program. Since the PIC12F629 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.
The result is a 16 bit binary number of the form: file 2C, file 2B = 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 (also called a directive) that tells the assembler where to place
the next instruction. ORG must have a value. For ORG 000, the real instruction will be
placed at memory location 000.
For ORG 2Ch, the first instruction in Main will be placed at address location 2Ch as shown
below:
Output a Value
The output port for a PIC12F629 is actually a FILE or REGISTER! It is file 05.
The 6 lines of the port are called: GP0, GP1, GP2, GP3, GP4 and GP5.
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 TRISIO file. This file is located at 85h - in
Band 1.
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 TRISIO file is made "1." To make a
line OUTPUT, the corresponding bit in the TRISIO file is made"0."
There are two ways to get to the TRISIO file. One is directly via the instruction:
The other is via the two instructions: BSF 03,5 and BCF 03,5. These instructions allow you to
go to bank1 where the TRISIO file is located. It is in Bank1 and the TRISIO file is called 05.
Any lines that are made output can be made HIGH or LOW.
Loop goto $ This will cause the microcontroller to keep looping the same
instruction.
or
Loop nop
goto Loop
;M1 & M2 are the bits in Ghost file for the motors
;PWM1 is the file for motor 1 speed (0-15)
;PWM2 is the file for motor 2 speed (0-15)
;Ghost file holds the bits (M1 & M2) to turn on/off each motor
Preload the PWM files:
movf the file with the value,0 to w such as 3A,0
movwf the file that will save the value such as 4B
e.g: movf 3A,0 ; (the value in file 3A gets copied into the working register)
movwf 4B ; (the value in the working register gets copied to file 4B)
CALL Delay
Main other instructions
BTFSS 2F,0 ;Has button been pushed?
GOTO Main ;No.
BTFSC 2F,1 ;Yes. Has "action" already been performed?
GOTO Main ;Yes.
BSF 2F,1 ;No.
CALL Action
GOTO Main
The Delay sub-routine includes instructions to look at a button and set a flag (BSF 2F,0) when
it is pressed.
The micro comes out of the Delay routine with the flag 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 2F,1) prevents the action being carried out more
than once for a single button-press.
When the button is released, both flags are cleared.
Equivalent
Mnemonic Description Status
Operation(s)
BTFSC 3,0
ADDCF f,d Add Carry to File Z
INCF f,d
B k Branch GOTO k -
BTFSC 3,0
BC k Branch on Carry -
GOTO k
BTFSS 3,0
BNC k Branch on No Carry -
GOTO k
BTFSS 3,2
BNZ k Branch on No Zero -
GOTO k
BTFSC 3,2
BZ k Branch on Zero -
GOTO k
BCF/BSF 0x0A,3
LCALL k Long Call BCF/BSF 0x0A,4
CALL k
BCF/BSF 0x0A,3
LGOTO k Long GOTO BCF/BSF 0x0A,4
GOTO k
COMF f,1
NEGF f,d Negate File Z
INCF f,d
Skip on No Digit
SKPNDC Carry
BTFSC 3,1 -
Z
TSTF f Test File MOVF f,1
Pull-ups
The PIC12F629 has individual pull-ups of about 47k on each line, that can be activated via
the following code. When a line is made into a input, the pull-up is not active, but is re-
activated when the line is changed to output.
Pin 4, GPIO,3 (GP3) is an INPUT-ONLY pin and does not have a pull-up.
In the following instructions, GPIO,5 will have a weak pull-up. A push-switch is connected
between GPIO,5 and ground. When the switch is pressed, the input will go LOW. This is
called an active LOW input. GPPU is an active LOW enable bit. It is written: GPPU. In the
program it is identified as NOT_GPPU as the "Word" document does not overlines. When this
bit is cleared (bcf), pull-ups are enabled by individual pot latch values.
Get_Random movf tmr0,0 ;Read the PIC's timer register and put it in W
andlw 0x03 ;mask all but bit0 and bit1 to get values 0,1,2,3.
movwf 27h ;move random number to file 27h (the
retlw 00 RANDOM ; number file).
To get a random number: 0-7, mask all but bits 0,1,2: andlw 0x07
The random number file can be incremented if zero is not needed (this will be the second
instruction in the sub-routine above) and then "andlw" is carried out.
Here is another Random Number Generator form Andrew Warren [fastfwd at ix.netcom.com]
of Fast Forward Engineering - San Diego, California.
Load a register called "RANDOM" with any non-zero value, then call this routine each time
you'd like a new pseudo-random value: LFSR = Linear Feedback Shift Register.
LFSR: RLF RANDOM,W
RLF RANDOM,W
BTFSC RANDOM,4
XORLW 1
BTFSC RANDOM,5
XORLW 1
BTFSC RANDOM,3
XORLW 1
MOVWF RANDOM
RETLW 0
[with the double RLF at the start of this routine,] Andy is implementing 'roll left' where the most
significant bit of RANDOM will get copied to least significant position. This is how it works.
The first RLF will copy the most significant bit of RANDOM into the carry. What ever was in
the carry prior to the first RLF will get copied into the least significant bit position - but we don't
care. Also, since the destination is the W register, the variable RANDOM is unaffected. The
second RLF repeats the same rotate operation, but this time the carry has been initialized to
the MS bit of random. So this second rotate will copy the MS bit into the least significant bit.
All of the other bits are of course shifted left one bit position.
Read a File
Files can be used to store temporary data. A file can be read by moving (it actually gets
copied and the original is not altered) it into the W register via an instruction such as: movf
3Ch,0
A file can be "read" and an action performed if it is zero. Use instructions such as:
decfsz 3Ch,0
goto button-1 ;the file is not zero
goto button-2 ;the file is zero
Any bit in a file can be read and an action carried out. In this case bit 3 is tested:
btfss 3Ch,3
goto button-1 ;bit 3 is "0"
goto button-2 ;bit 3 is "1"
Read Data
The PIC12F629 does not have a feature such as "Read Data" or "Read an Instruction." Data
can be added to a program (when writing the program) in the form of a table (see Table) and
this data cannot be altered. Each item in the table can be read by writing an instruction such
as:
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).
The following two instructions will rotate the contents of a file register without loosing data in
the "Carry Flag".
Rotate right or left can be implemented. Note that the carry flag is changed.
Enter with "abcdefgh" and leave with "bcdefgha"
or
Enter with "abcdefgh" and leave with
"habcdefg"
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 TRISIO file. This file controls the
direction of the Input/Output lines of GPIO and when it contains 0's, all the lines are output.
Note: GP3 is input only
SLEEP
This instruction puts the micro to sleep. This is also called "power-down mode."
The micro stops executing the program when it comes to this instruction.
If the Watchdog Timer is enabled, it will be cleared but will keep incrementing.
The In/Out lines maintain the status they had before SLEEP was executed.
For the lowest consumption in SLEEP, all output lines must not drive any circuitry before the
SLEEP instruction.
On-chip pull-ups must also be turned off to reduce the current consumption during SLEEP.
The micro will wake up from SLEEP via one of the following:
1. Taking MCLR pin LOW
2. Watchdog Timer wake-up (if watchdog is enabled)
3. Interrupt from GP2/INT pin
4. GPIO change
5. Peripheral interrupt.
On wake-up from SLEEP, the WDT is cleared.
When the SLEEP instruction is being executed, the next instruction (PC + 1) is pre-fetched.
For the micro to wake up through an interrupt event, the corresponding interrupt enable bit
must be set (enabled). Wake up is regardless of the state of the GIE bit. If the GIE bit is clear
(disabled) the micro continues execution at the instruction after SLEEP. If the GIE bit is set
(enabled) the micro executes the instruction after SLEEP then branches to the interrupt
address (004h). In the case where the instruction following SLEEP is not desirable, the user
should have a NOP after the SLEEP instruction.
The TO and PD bits in the STATUS register can be used to determine the cause of RESET.
The PD bit, which is set on power-up, is cleared when SLEEP is invoked. The TO bit is
cleared if WDT wake-up occurred.
The SLEEP instruction is:
sleep
To send the micro to the "sleep_micro" sub-routine, the following instruction is needed:
sleep_micro
sleep
To set GPIO 3 for wake-up from sleep: This has been tested in Greeting card "Decision
Maker"
If the Watchdog timer is enabled, the micro will come out of SLEEP and goto main.
The watchdog will reset after 18,000uS (18m) and wake the micro.
If 18mS is too short, 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.3sec
The micro contains an 8-bit prescaler register that can be assigned to Timer0 or Watchdog.
This prescaler register is not readable or writable.
To set the prescaler to the WDT, bit 3 of the OPTION REGister must be set. The instruction
is:
The three lowest bits of the Option register set the timing for the WDT:
movlw b'xxxxx000'
movwf OPTION_REG ;WDT timer = 18mS
or:
movlw b'xxxxx001'
movwf OPTION_REG ;WDT timer = 36mS
or:
movlw b'xxxxx010'
movwf OPTION_REG ;WDT timer = 72mS etc etc
or:
movlw b'xxxxx111'
movwf OPTION_REG ;WDT timer = 2,304mS = 2.3 Seconds
GPIO CHANGE
If you want the micro to come out of sleep when the voltage-level changes on any input line,
the WDT must be turned off. This must be done during burning the program into the chip.
(You will not have the advantage of the watchdog timer to reset the micro if it runs off the end
of the program.)
movlw b'xxxx1xxx' ; Enable GPIO port change interrupt (but NOT GIE)
movwf INTCON
sleep_micro
sleep
Alternately, a SLEEP instruction can be added to a program. In the program below, the micro
will stop executing instructions when it reaches "sleep" and wait for a "time-out" of the WDT or
a change in GPIO (depending on the setting in INTCON.) If the GIE bit is set, the micro will
execute "instruction A" (after sleep) and go to the interrupt address (004h).
instruction
instruction
instruction
sleep
instruction A - this instruction can be anything but a GOTO instruction.
no further instructions
The Global Interrupt feature is enabled by setting bit 7 of the INTCON register. For this and
other features of SLEEP see: PIC12F629 Data Sheet (.pdf 4,926KB)
Sleep movf GPIO,W ; Read GPIO clears Int-On-Change flag. Must read
; into W not back to F as it reads port not the output
; latch which may result in output data being
; inadvertently altered.
bcf INTCON,GPIF
bsf STATUS,RP0 ; Sel bank 1
movlw 0xFF ; Setup W for TRISIO all input
movwf TRISIO ; Write to TRISIO. Reduce power in sleep mode
sleep ; Go to sleep
nop ;
movlw b'11101000' ; Wake from sleep and set
movwf TRISIO ; TRISIO for input and output for your project
bcf STATUS,RP0 ; Sel Bank 0
movf GPIO,W ; Read GPIO register
bcf INTCON,GPIF ; and clear GPIF flag in interrupt register
The following set of instructions have been tried in an Alarm project. Some of the instructions
may not be needed and you can try deleting and testing for yourself. The most important point
to remember is this: The micro goes to ISR (address 04) after waking up from sleep and the
instructions at address 04 must be included:
org 0
goto setup
nop
nop
nop
nop
goto Enable
Stack
This is an area where up to 8 addresses are placed. These address are RETURN address.
When a CALL is made, the address of the next instruction in your program is placed on the
STACK.
The micro will go to the sub-routine you have CALLed. In this sub-routine you can have
another CALL.
This is the second CALL. In the second sub-routine you can have another CALL. In fact this
can be done up to 8 times.
If more than 8 address are placed on the stack, the first address is lost. That's why you
cannot have more than 8 CALLs. When a return is made, the CALL number is reduced.
The PIC12F629 can have a CALL instruction that CALLs another sub-routine and the sub-
routine CALLs another sub-routine and that sub-routine CALLS another sub-routine until 8
CALLs are made.
The 8th sub-routine will have a RETLW 00 to go back the the previous sub-routine and each
sub-routine will have a RETLW 00 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.
Note: Return is used in '508 instruction-set and Retlw 00 is used in '629 instruction-set):
The PIC12F508 or 509 has only a "2-stack" and the low-cost PIC micro (reserved for high-
quantity orders) has only a "2-stack." It is important to realise this so your programs are
portable to other micro's. You can have a CALL from Main and a CALL from the sub-routine,
but the second sub-routine must only have a RETLW 00.
Store Data
The PIC12F629 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 PIC12F629 has 128 bytes of EEPROM.
This requires a special routine - found under EEPROM.
Sound
The following program produces a "rising Sound."
Note: the last digit is INVERTED. This is a handy way to invert a digit.
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.
movf X, w
subwf Y, w
addwf X, f
subwf Y, f
Another way to swap the contents of two files without using a temporary file:
movf X, w ; Get X
xorwf Y, f ; Y is now X^Y
xorwf Y,w ; W is now (X^y)^X==Y (say OldY)
movwf X ; Now X is OldY
xorwf Y, f ; finally Y is (OldX^Y)^Y==OldX
XORWF 3C,1
XORWF 3C,0
XORWF 3C,1
Main CALL Sw
BTFSC 2F,1 ;Test button-press flag to see if button was pressed
GOTO Main2 ;Button pressed
Display the values ;Button not pressed
on a display etc.
GOTO Main
Main2 CALL Increment ;Increment the display. (you provide the routine)
BCF 2F,1 ;Clear the button-press flag
GOTO Main
In a sub-routine, a value between 1 and 6 is created and put into "w" and Table is called thus:
To prevent the micro jumping beyond the end of the table, the instruction:
Table andlw 06h ;this prevents micro jumping beyond end of table
ADDWF pcl,1 ;Add W to the Program Counter to create a jump.
nop ;the micro jumps here for "movlw 00"
RETLW 3Fh
RETLW 06h
RETLW 5Bh
RETLW 4Fh
RETLW 66h
RETLW 6Dh
Toggle
The state of a line can be changed from High to LOW 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:
Tone NOP
DECFSZ 2A,1
GOTO Tone
MOVLW 02h ;Put 02 into W
XORWF GPIO,1 ;XOR 02 with GPIO. Toggle GP1
GOTO Tone
To get the micro out of the Tone routine is must have a "duration." The pitch of the tone can
also be changed:
The following instructions apply to the other lines: GP3 is input ONLY
The TRISIO 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 TRISIO file is located. When in
Bank1, the TRISIO file is file 05. See: SetUp for details of the instructions to be inserted
between BSF 03,5 and BCF 03,5.
BSF 03,5 ;Go to Bank 1
movlw
b'00000011'
movwf tris ;Go to Bank 0 - the program memory area.
BCF 03,5
Watchdog Timer (WDT)
The PIC12F629 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
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 2E holds the value 3C. (file 2E does contain the value 3C)
In this example, 3C is put into file 2E and the XOR operation is carried out:
MOVLW 3C ;Put 3C in W
MOVWF 2E :Put 3C into file 2E
XORLW 3C ;XOR 3C with file 2E and put the result into W.
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.
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.