Programming PIC 18
Programming PIC 18
Han-Way Huang
- Assembler directives
- Comments
- Label
- Mnemonics
- Operands
- Comment
Label Field
- Must start from column 1 and followed by a tab, a space, a colon (:), or the
end of a line.
Mnemonic Field
- Can be an assembly instruction mnemonic or assembly directive
- Must begin in column two or greater
- Must be separated from the label by a colon, one or more spaces or tabs
Comment field
- Is optional
- All characters to the right of the semicolon are ignored by the assembler
“too_high” is a label
“decf”
decf is a mnemonic
Assembler Directives
- Control directives
- Data
D t directives
di ti
- Listing directives
- Macro directives
Control Directives
Example.
if version
i == 100
movlw D’10’
movwf io1,A
else
movlw D’26’
movwf io2,A
endif
#define loop_cnt 30
#define sum3(x,y,z) (x + y + z)
#define seed 103
#include “<include_file>”
“<include file>” (or #include <include_file>)
<include file>)
radix <default_radix>
- sets the default radix for data expression
- the
th default
d f lt radix
di values
l are: hex,
h dec,
d or octt
while <expr>
p
endw
- The lines between while and endw are assembled as long as <expr> is true.
Data Directives
db <expr>
<expr>,…,<expr>
<expr> ; define 1 or multiple byte values
db “text_string” ; define a string
dw <expr>,…,<expr> ; define 1 or multiple word constants
dw “text_string” ; define a string
dt <expr>, …, <expr> ; generates a series of retlw instructions
<label> set <expr> ; assign a value (<expr>) to label
<label> equ <expr> ; defines a constant
led_pat db 0x30,0x80,0x6D,9x40,0x79,0x20,0x33,0x10,0x5B,0x08
msg11 db “Pl
“Please enter your choice
h i (1/2)
(1/2):”,0
”0
array dw 0x1234,0x2300,0x40,0x33
results dt 1,2,3,4,5
TH equ 200
TL equ 30
What is a macro?
Macro Directives
macro
endm
exitm
Object
j File Directives
banksel <label>
- generate the
h iinstruction
i sequence to set active
i data
d bank
b k to theh one where
h
<label> is located
- <label> must have been defined before the banksel directive is invoked.
- sets the program origin for subsequent code at the address defined in <expr>.
- <label> will be assigned the value of <expr>.
processor <processor_type>
- Sets
S theh processor type
Algorithm Representation
Step
p1
…
Step 2
…
Step 3
…
Flowchart Symbols
Terminal A
Process Subroutine
Input or
output B
off-page connector
yes
Decision A
on-page connector
no
A
Assembly
bl PProgram Template
T l
Case Issue
- The PIC18 instructions can be written in either uppercase or lowercase.
- MPASM allows the user to include “p18Fxxxx.inc” file to provide register
definitions for the specific processor.
processor
- All special function registers and bits are defined in uppercase.
- The convention followed in this text is: using lowercase for instructions and
directives, using uppercase for special function registers.
Figure 02_t1
02 t1 Byte order example
Example 2.4 Write a program that adds the three numbers stored in data
registers at 0x20, 0x30, and 0x40 and places the sum in data register at 0x50.
Solution:
Algorithm:
Step 1
Load the number stored at 0x20 into the WREG register.
St 2
Step
Add the number stored at 0x30 and the number in the WREG register and
leave the sum in the WREG register.
Step
p3
Add the number stored at 0x40 and the number in the WREG register and
leave the sum in the WREG register.
Step 4
Store the contents of the WREG register in the memory location at 0x50.
0x50
#include <p18F8720.inc>
org 0x00
goto start
org 0x08
retfie
org 0x18
retfie
tfi
start movlw 0x05 ; WREG 0x05
subwf 0x10,F,A ; 0x10 [0x10] – 0x05
subwf 0x11,F,A, , ; 0x11 [[0x11]] – 0x05
subwf 0x12,F,A ; 0x12 [0x12] – 0x05
subwf 0x13,F,A ; 0x13 [0x13] – 0x05
end
Example 2.7 Write a program that subtracts the number stored at 0x20..0x23
from the number stored at 0x10..0x13 and leaves the difference at 0x30..0x33.
Solution:
Start
W REG [0x20]
W REG [0x21]
Three-operand
0x31 [0x11] - [W REG] - B
subtraction
W REG [0x22]
[0 22]
Three-operand
0x32 [0x12] - [W REG] - B subtraction
W REG [0x23]
[ ]
Three-operand
0x33 [0x13] - [W REG] - B subtraction
Stop
Example 2.9 Write an instruction sequence that adds the decimal numbers stored at
0x10...0x13 and 0x14...0x17 and stores the sum in 0x20..0x23.
Solution:
#include <p18F8720.inc>
…
start movf 0x10,W ; add the least significant byte
addwf 0x14,W ; “
daw ; adjust for valid BCD
movwf 0x20 ; save in the destination
movf 0x11 ; add the second to least significant byte
addwfc 0x15,W ; “
daw ; “
movwf 0x21 ; “
movf 0x12 ; add the second to most significant byte
addwfc 0x16 ; “
daw ; “
movwf 0x22 ; “
movf 0x13 ; add the most significant byte
addwfc 0x17 ; “
daw ; “
movwf 0x23 ; “
end
Multiplication
- PIC18 has two instructions for 8-bit multiplication: mulwf f and mullw k.
- The products are stored in the PRODH:PRODL register pair.
- The multiplication of numbers larger than 8 bits must be synthesized.
synthesized
- The following instruction sequence performs 8-bit multiplication operation:
movf 0x10,W,A
mulwf 0x11 A
0x11,A
movff PRODH,0x21 ; upper byte of the product
movff PRODL,0x20 ; lower byte of the product
P = PHPL
Q = QH QL
upper byte
b t lower
l byte
b t partial
ti l product
d t P LQH
Address R +3 R +2 R +1 R Fi l product
Final d P×Q
msb lsb
Note: msb stands for most significant byte and lsb stands for least significant byte
Fi
Figure 22.4
4 16
16-bit
bi bby 16
16-bit
bi multiplication
l i li i
Instruction sequence to multiply two numbers that are stored at N:N+1 and M:M+1:
Program Loops
1. Do S forever
i i1 i i2
no no
i i2 ? i i1 ?
yes yes
S S
i i+ 1 i ii- 1
Figure
g 2.6 A Fo r-lo o p looping
p g construct
3. while C do S
true
C S
false
4. repeat S until C
initialize C
true
C
fals
e
Figure 2.8 T he Repeat ... Until looping construct
Changing
g g the Program
g Counter
iincff ff,d,a
d
decf f,d,a
Example 1
Example
p 2
Example 2.12 Write a program to compute 1 + 2 + 3 + … + n and save the sum at 0x00
and 0x01.
Solution:
1. Program logic
St t
Start
i1
sum 0
yes
i > n?
no
sum sum + i
i i+ 1
Stop
#include <p18F8720.inc>
radix dec
n equ D'50'
sum_hi
hi sett 0 01
0x01 ; high
hi h byte
b t off sum
sum_lo set 0x00 ; low byte of sum
i set 0x02 ; loop index i
org 0x00 ; reset vector
gotot start
t t
org 0x08
retfie
org 0x18
retfie
tfi
start clrf sum_hi,A ; initialize sum to 0
clrf sum_lo,A ; "
clrf i,A ; initialize i to 0
i f
incf i FA
i,F,A ; i starts
t t from
f 1
sum_lp movlw n ; place n in WREG
cpfsgt i,A ; compare i with n and skip if i > n
bra add_lp ; perform addition when i 50
bra exit sum ; it is done when i > 50
exit_sum
Start
Example 2.13
Write a program
arr_max arr[0]
to find the i 1
l
largest
t element
l t
stored in the no
array that is i n?
yes
stored in data
memory no
arr[i] > arr_max?
locations from
0x10 to 0x5F. yes
arr_max arr[i]
[i]
i i+ 1
Stop
cpfsgt arr_max,A
arr max A ; is arr_max
arr max > arr[i]?
bra replace ; no
bra next_i ; yes
replace movwf arr_max,A ; update the array max
next i
next_i incf i FA
i,F,A
goto again
done nop
end
Program
g memory
y
Table pointer
Table latch
TBLPTRU TBLPTRH TBLPTRL
TABLAT
Program memory
Table pointer
Table latch
TBLPTRU TBLPTRH TBLPTRL
TABLAT
• TBLPTRU (6 bits)
bi )
• TBLPTRH (8 bits)
• TBLPTRL (8 bits)
V i
Versions off table
t bl read
d and
d ttable
bl write
it iinstructions
t ti
Reading
ead g the
t e program
p og a memory
e o y location
ocat o pprog
og_loc
oc involves
vo ves two steps:
movlw
l upper prog_loc
l
movwf TBLPTRU,A
movlw high prog_loc
movwf TBLPTRH,A
movlw low prog_loc
movwf TBLPTRL,A
tblrd
Logic Instructions
To set bits 7,
7 6,
6 and 0 of PORTA to 1
movlw B’11000001’
iorwf PORTA,F,A
movlw B 11101001
B’11101001
andwf PORTB,F,A
movlw B’10101010’
xorwf PORTC
Example 2.16 Write a program to find out the number of elements in an array of 8-bit
elements that are a multiple of 8. The array is in the program memory.
Solution:
Start
1.. A number
u be must us have
ave thee iN
lowest 3 bits equal to 0 to count 0
be a multiple of 8
2. Use the Repeat S until C prod array[i] AND 0x07
looping
p g construct
no
prod 0?
yes
count count + 1
ii-1
no
i = 0?
yes
Stop
#include <p18F8720.inc>
ilimit equ 0x20 ; loop index limit
count set 0x00
ii set 0x01 ; loop index
mask equ 0x07 ; used to masked upper five bits
org 0x00
goto start
… ; interrupt service routines
start clrf count,A
movlw ilimit
movwf ii ; initialize ii to ilimit
movlw upper array
movwf TBLPTRU,A
movlw high array
movwf TBLPTRH,A
movlw low array
movwf TBLPTRL,A
movlw mask
i_loop tblrd*+ ; read an array element into TABLAT
andwf TABLAT,F,A
bnz next ; branch if not a multiple of 8
incf count,F,A
count FA ; is a multiple of 8
next decfsz ii,F,A ; decrement loop count
bra i_loop
nop
array db 0x00,0x01,0x30,0x03,0x04,0x05,0x06,0x07,0x08,0x09
db 0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13
db 0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D
db 0x1E,0x1F
end
- The PIC18 uses a crystal oscillator or a RC circuit to generate the clock signal
needed to control its operation.
- The instruction execution time is measured by using the instruction cycle
clock.
- One instruction cycle is equal to four times the crystal oscillator clock period.
- Select an appropriate instruction that will take a multiple of 10 or 20
instruction cycles to execute.
- A desirable time delay is created by repeating the chosen instruction sequence
for certain number of times.
radix dec
loop_cnt equ PRODL
movlw 250
movlw loop_cnt,A
again dup_nop 17 ; insert 17 nop instructions
decfsz loop_cnt,F,A ; 1 instruction cycle
bra again ; 2 instruction cycles
Example
p 2.18 Write a pprogram
g to create a time delayy of 100 ms for the demo
board that uses a 40 MHz crystal oscillator to operate.
Solution: Repeat the previous instruction sequence for 200 times can create a
100 ms time delay.
radix dec
lp_cnt1 equ 0x21
lp_cnt2
p_ equ
q 0x22
movlw 200
movwf lp_cnt1,A
loop1 movlw 250
movwf lp cnt2 A
lp_cnt2,A
loop2 dup_nop 17 ; 17 instruction cycles
decfsz lp_cnt2,F,A ; 1 instruction cycle (2 when [lp_cnt1] = 0)
bra loop2 ; 2 instruction cycles
decfsz lp_cnt1,F,A
bra loop1
Rotate Instructions
7 6 5 4 3 2 1 0 C
Figure 22.17
17 Operation performed by the rlcf f,d,a
f d a instruction
7 6 5 4 3 2 1 0
C 7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
Figure 2.20
2 20 Operation performed by the rrncf f,d,a
f d a instruction
Example
p 2.19 Computep the new values of the data register
g 0x10 and the C flag
g after the
execution of the rlcf 0x10,F,A instruction. [0x10] = 0xA9, C = 1
Solution:
The result is
0 1 0 1 0 1 0 0 1
Original value New value
[0x10] = 1010 1001 [0x10] = 01010010
1 0 1 0 1 0 0 1 0 C=0 C=1
Example 2.20 Compute the new values of the data register 0x10 and the C flag after the
execution of the rrcf 0x10,F,A
0x10 F A instruction.
instruction [0x10] = 0xC7,
0xC7 C = 1
Solution:
The result is
1 1 0 0 0 1 1 1 1
Original value New value
[0x10] = 1100 0111 [0x10] = 1110 0011
1 1 1 0 0 0 1 1 1 C=1 C=1
Example 2.21 Compute the new values of the data memory location 0x10 after the
execution of the rrncf 0x10,F,A instruction and the rlncf 0x10,F,A instruction,
respectively. [0x10] = 0x6E
Solution:
The result is
0 1 1 0 1 1 1 0
original value new value
[0x10]
[0 0] = 001100 11100 [0
[0x10]
0] = 00
0011 0111
0
0 0 1 1 0 1 1 1
The result is
0 1 1 0 1 1 1 0
Before After
[[0x10]] = 0110 1110 [[0x10]] = 1101 1100
1 1 0 1 1 1 0 0
Bit Operation
p Instructions
Examples