Professional Documents
Culture Documents
Chapter 2: Assembly Language Programming: The PIC18 Microcontroller
Chapter 2: Assembly Language Programming: The PIC18 Microcontroller
Label Field
- Must start from column 1 and followed by a tab, a space, a colon (:), or the
end of a line.
- Must start with an alphabetic character or underscore (_).
- May contain alphanumeric characters,
characters underscores and question marks (?).
(?)
- May contain up to 32 characters and is case-sensitive by default.
wait
btfss
sum,7
; wait is a label
_again
decf
loop_cnt,F
; _again is a label
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
addlw 0x10
loop
incf
0x30,W,A
; incf is a mnemonic
false
equ
true
decf
loop_cnt,F
equ
Comment field
- Is optional
- A comment starts with a semicolon.
- All characters to the right of the semicolon are ignored by the assembler
- Comments provide documentation to the instruction or assembler directives
- A comment may explain the function of a single statement or the function of
a group of instructions
too high decf
too_high
mean F A
mean,F,A
too_high
is a label
decf
decf
is a mnemonic
mean,F,A
Assembler Directives
- Control directives
- Data
D t directives
di ti
- Listing directives
- Macro directives
- Object file directives
Control Directives
if <expr>
else
endif
Example.
if version
i == 100
movlw D10
movwf io1,A
else
movlw D26
movwf io2,A
endif
end
reset
code
goto
0x00
start
#define
#define
#define
loop_cnt
30
sum3(x,y,z) (x + y + z)
seed
103
#undefine <label>
#include <include_file>
<include file> (or #include <include_file>)
<include file>)
#include lcd_util.asm
#include <p18F8680.inc>
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
radix
dec
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>
db
text_string
; define a string
dw
<expr>,,<expr>
dw
text_string
; define a string
dt
<expr>, , <expr>
<label> set
<expr>
<label> equ
<expr>
; defines a constant
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
msg2
dw
The humidity is ,0
results dt
1,2,3,4,5
sum_hi set
0x01
sum_lo set
0x00
TH
equ
200
TL
equ
30
What is a macro?
-
Macro Directives
macro
endm
exitm
macro
movlw
movwf
movlw
movwf
endm
0x55
EECON2
0xAA
EECON2
; WREG [arg1]+[arg2]+[arg3]
arg1,W,A
addwf arg2,W,A
addwf arg3,W,A
endm
Object
j File Directives
banksel <label>
-
generate the
h iinstruction
i sequence to set active
i data
d bank
b k to the
h one where
h
<label> is located
<label> must have been defined before the banksel directive is invoked.
bigq
set
0x300
b k l bigq
banksel
bi
; this
thi di
directive
ti will
ill cause the
th assembler
bl to
t
; insert the instruction movlb 0x03
<expr>
- sets the program origin for subsequent code at the address defined in <expr>.
- <label> will be assigned the value of <expr>.
reset
org
0x00
goto
start
start
led_pat
org
0x1000
db
0x7E,0x30,0x6D,0x79,0x33,0x5B,0x5F,0x70,0x7F,0x7B
<processor_type>
- Sets
S the
h processor type
processor p18F8680
Algorithm Representation
Step
p1
Step 2
Step 3
Flowchart Symbols
Terminal
Process
Subroutine
Input or
output
B
off-page connector
yes
Decision
A
on-page connector
no
Figure 2.1 Flowchart symbols used in this book
A
Assembly
bl P
Program Template
T
l
start
org
goto
g
org
org
end
0x0000
start
0x08
0x18
start
org
goto
org
retfie
org
retfie
tfi
end
0x0000
start
0x08
0x18
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.
Little-Endian Method
address
P+1
P+2
P+3
P+1
P+2
P+3
value
12
34
56
78
78
56
34
12
(in hex)
Figure 02_t1
02 t1 Byte order example
Copyright @ 2005 Thomson Delmar Learning
0x00
goto
start
org
0x08
retfie
org
0x18
retfie
start
movf
0x20,W,A
; WREG [0x20]
addwf
0x30,W,A
addwf
0x40,W,A
movwf 0x50,A
end
Copyright @ 2005 Thomson Delmar Learning
start
#include <p18F8720.inc>
org
0x00
goto
start
org
0x08
retfie
org
0x18
retfie
tfi
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]
0x30 [0x10] - [W REG]
W REG [0x21]
0x31 [0x11] - [W REG] - B
Three-operand
subtraction
W REG [0x22]
[0 22]
0x32 [0x12] - [W REG] - B
Three-operand
subtraction
W REG [0x23]
[
]
0x33 [0x13] - [W REG] - B
Three-operand
subtraction
Stop
Figure 2.2 Logic flow of Example 2.7
0x24,W,A
addwf
0x25,W,A
daw
movwf
0x30,A
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
Copyright @ 2005 Thomson Delmar Learning
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
mulwf
movff
movff
0x10,W,A
0x11 A
0x11,A
PRODH,0x21
PRODL,0x20
8-bit
8-bit
8-bit
8-bit
upper byte
b t lower
l
byte
b t
partial
ti l product
d t P LQH
partial product P HQH
Address
R +3
R +2
msb
R +1
Fi l product
Final
d
PQ
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:
movwf
movf
mulwf
movff
ff
movff
movf
mulwf
movff
ff
movff
movf
mulwf
movff
addwf
movf
addwfc
movlw
l
addwfc
movf
mulwf
movff
N,A
M+1,W,A
N+1,A
PRODL PR+2
PRODL,PR+2
PRODH,PR+3
M,W,A
N,A
PRODL PR
PRODL,PR
PRODH,PR+1
M,W,A
N+1,A
PRODL W A
PRODL,W,A
PR+1,F,A
PRODH,W,A
PR+2,F,A
0
PR+3,F,A
M+1,W,A
N,A
PRODL W A
PRODL,W,A
"
; compute MH NH
; compute ML NL
; compute ML NH
; add
dd ML NH to
t PR
;
"
;
"
;
"
;
"
; add carry
; compute MH NL
; add
dd MH NL to
t PR
addwf
movf
addwfc
movlw
addwfc
nop
end
PR+1,F,A
PRODH,W,A
PR+2,F,A
0
PR+3,F,A
;
"
;
"
;
"
;
"
; add carry
Program Loops
- Enable the microcontroller to perform repetitive operations.
- A loop may be executed a finite number of times or infinite number of times.
Program Loop Construct
1. Do S forever
i i1
i i2 ?
i i2
no
i i1 ?
yes
yes
i i+ 1
i ii- 1
(a) For i = i1 to i2 Do S
no
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
fals
e
Figure 2.8 T he Repeat ... Until looping construct
Changing
g g the Program
g
Counter
- Microcontroller executes instruction sequentially in normal condition.
- PIC18 has a 21-bit program counter (PC) which is divided into three registers: PCL,
PCH,, and PCU.
- PCL can be accessed directly. However, PCH and PCU are not directly accessible.
- One can accessed the values of PCH and PCU indirectly by accessing the PCLATH and
PCLATU.
- Readingg the PCL will cause the values of PCH and PCU to be copied
p into the
PCLATH and PCLATU.
- Writing the PCL will cause the values of PCLATCH and PCLATU to be written into the
PCH and PCU.
- In normal pprogram
g
execution, the PC value is incremented byy either 2 or 4.
- To implement a program loop, the processor needs to change the PC value by a value
other than 2 or 4.
f,a
f,a
fa
f,a
f,d,a
f,d,a
f,d,a
f,d,a
f,a
f,b,a
fba
f,b,a
ff,d,a
d
f,d,a
Example 1
i_cnt
i_loop
equ
clrf
movlw
cpfseq
goto
Example
p 2
n
lp_cnt
loop
p
equ
set
movlw
movwf
decfsz
goto
20
0x10
n
lp_cnt
lp_cnt,F,A
loop
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
i > n?
yes
no
sum sum + i
i i+ 1
Stop
Figure 2.12 Flowchar for computing 1+2+...+n
n
sum_hi
hi
sum_lo
i
start
sum_lp
#include <p18F8720.inc>
radix
dec
equ
D'50'
sett
0 01
0x01
; high
hi h byte
b t off sum
set
0x00
; low byte of sum
set
0x02
; loop index i
org
0x00
; reset vector
goto
t
start
t t
org
0x08
retfie
org
0x18
retfie
tfi
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
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
add_lpp
movf
addwf
movlw
addwfc
i f
incf
bra
exit_sum nop
bra
end
i,W,A
, ,
sum_lo,F,A
0
sum_hi,F,A
i FA
i,F,A
sum_lp
; place
p
i in WREG
; add i to sum_lo
; add carry to sum_hi
; increment
i
t loop
l
index
i d i by
b 1
exit_sum
_
Start
Example 2.13
Write a program
to find the
l
largest
t element
l
t
stored in the
array that is
stored in data
memory
locations from
0x10 to 0x5F.
arr_max arr[0]
i 1
i n?
yes
arr[i] > arr_max?
no
no
yes
arr_max arr[i]
[i]
i i+ 1
Stop
Figure 2.13 Flowchart for finding the maximum array element
replace
next i
next_i
done
cpfsgt
bra
bra
movwf
incf
goto
nop
end
arr_max,A
arr
max A
replace
next_i
arr_max,A
i FA
i,F,A
again
; is arr_max
arr max > arr[i]?
; no
; yes
; update the array max
Table pointer
TBLPTRU
TBLPTRH
TBLPTRL
Table latch
TABLAT
Program memory
Table pointer
TBLPTRU
TBLPTRH
TBLPTRL
Table latch
TABLAT
V i
Versions
off table
t bl read
d and
d ttable
bl write
it iinstructions
t ti
Table 2.11 PIC18 MCU table read and write instructions
Mnemonic,
Mnemonic
operator
TBLRD*
TBLRD*+
TBLRD*TBLRD
TBLRD+*
TBLWT*
TBLWT*+
TBLWT*TBLWT+*
Description
Table read
Table read with post-increment
Table read with post-decrement
post decrement
Table read with pre-increment
Table write
Table write with post-increment
Table write with post-decrement
T bl write
Table
it with
ith pre-increment
i
t
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
1000
1001
1010
1011
1100
1101
1110
1111
Status
affected
none
none
none
none
none
none
none
none
Reading
ead g the
t e program
p og a memory
e o y location
ocat o p
prog
og_loc
oc involves
vo ves two steps:
Step 1. Place the address of prog_loc in TBLPTR registers
movlw
l
movwf
movlw
movwf
movlw
movwf
upper prog_loc
l
TBLPTRU,A
high prog_loc
TBLPTRH,A
low prog_loc
TBLPTRL,A
Logic Instructions
Table 2.12 PIC18 MCU logic instructions
Mnemonic,
operator
andwf f,d,a
comf f,d,a
iorwf f,d,a
g
f,a
,
negf
xorwf f,d,a
andlw k
iolw
k
xorlw k
Description
AND WREG with f
Complement f
Inclusive OR WREG with f
Negate
g
f
Exclusive OR WREG with f
AND literal with WREG
Inclusive OR literal with WREG
Exclusive OR literal with WREG
To set bits 7,
7 6,
6 and 0 of PORTA to 1
movlw
iorwf
B11000001
PORTA,F,A
B 11101001
B11101001
PORTB,F,A
B10101010
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..
2.
A number
u be must
us have
ave thee
lowest 3 bits equal to 0 to
be a multiple of 8
Use the Repeat S until C
looping
p g construct
iN
count 0
prod array[i] AND 0x07
prod 0?
no
yes
count count + 1
ii-1
no
i = 0?
yes
Stop
ilimit
count
ii
mask
start
i_loop
#include <p18F8720.inc>
equ
0x20
set
0x00
set
0x01
equ
0x07
org
0x00
goto
start
clrf
count,A
movlw
ilimit
movwf
ii
movlw
upper array
movwf
TBLPTRU,A
movlw
high array
movwf
TBLPTRH,A
movlw
low array
movwf
TBLPTRL,A
movlw
mask
tblrd*+
andwf
TABLAT,F,A
bnz
next
; initialize ii to ilimit
next
array
incf
decfsz
bra
nop
db
db
db
db
end
count,F,A
count
FA
ii,F,A
i_loop
; is a multiple of 8
; decrement loop count
0x00,0x01,0x30,0x03,0x04,0x05,0x06,0x07,0x08,0x09
0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13
0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D
0x1E,0x1F
dup_nop
macro
kk
variable i
i=0
while i < kk
nop
i += 1
endw
d
endm
y
oscillator
To create 0.5 ms time delayy with 40 MHz crystal
loop_cnt
again
radix
equ
movlw
movlw
dup_nop
decfsz
bra
dec
PRODL
250
loop_cnt,A
17
; insert 17 nop instructions
loop_cnt,F,A ; 1 instruction cycle
again
; 2 instruction cycles
Example
p 2.18 Write a pprogram
g
to create a time delay
y 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.
lp_cnt1
lp_cnt2
p_
loop1
loop2
radix
equ
equ
q
movlw
movwf
movlw
movwf
dup_nop
decfsz
bra
decfsz
bra
dec
0x21
0x22
200
lp_cnt1,A
250
lp cnt2 A
lp_cnt2,A
17
lp_cnt2,F,A
loop2
lp_cnt1,F,A
loop1
; 17 instruction cycles
; 1 instruction cycle (2 when [lp_cnt1] = 0)
; 2 instruction cycles
Rotate Instructions
rlcf
f, d, a
Figure 22.17
17 Operation performed by the rlcf f,d,a
f d a instruction
rlncf f, d, a
rrcf
f,, d,, a
; rotate right
g f through
g carryy
rrncf
f, d, a
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 Compute
p 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
Original value
New value
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
Original value
New value
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
0
original value
new value
[0x10]
[0
0] = 001100 11100 [0
[0x10]
0] = 00
0011 0111
0
0
The result is
0
0
Before
After
Bit Operation
p
Instructions
bcf f, b, a
bsf f, b, a
btg
g f, b, a
Examples
1.
2.
3.
bcf STATUS,C,A
bsf sign,0,A
btg sign,0,A
loop
movlw
bcf
rlcf
rlcf
rlcf
decfsz
goto
0x03
; set loop count to 3
STATUS, C, A; clear the C flag
0x00, F, A
; shift left one place
0x01, F, A
;
0x02, F, A
;
loop
movlw
bcf
rrcf
rrcf
rrcf
decfsz
goto
0x04
STATUS, C, A
0x12, F, A
0x11, F, A
0x10, F, A
WREG,W,A
loop