Pic 18
Pic 18
Pic 18
E b dd d S
Embedded
Systems
t
Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey
E g Husam
Eng.
m Alzaq
q
The Islamic Uni. Of Gaza
1-1
Chapter
p
2:
PIC Architecture And
A
Assembly
bl L
Language
Programming.
Programming
The WREG Register
The PIC File Register
Using instruction with the default
access bank
PIC Microcontroller
and
d Embedded
E b dd d S
Systems
Muhammad Ali Mazidi,
Rolin McKinlay and
D
Danny
Causey
C s , February
F b
2007.
1-2
Outline
PIC
P Status Register
PIC data format and directive
Intro. To PIC assembly language
Assembling and linking a PIC program
The Program Counter and program ROM
1-3
Objective
Examine
E
the
h data
d
RAM fileReg
fl
of
f the
h PIC
P uC
Manipulate
p
data using
g the WREG & MOVE
Perform simple operations such ADD and
1-4
operation.
The WREG (WORking Register) Register is
one of the most widely
y used registers
g
of
the PIC
8-bit
register
g
any
y data larger
g than 8 bits
must be broken into 8-bits chunks before it is
processed.
There is only one .
D7 D6 D5 D4 D3 D2 D1 D0
3-5
MOVLW
Moves 8-bit
b d
data into WREG
EG
MOVLW k; move literal value k into WREG
Example
MOVLW 25H
MOVLW A5H
Is the following code correct?
MOVLW 9H
MOVLW A23H
The PIC uCs
1-6
ADDLW
ADDLW
DDLW k
k; Add
dd literal
li
l value
l k to WREG (k +WREG)
WREG)
Example:
MOVLW 12H
ADDLW 16H
ADDKW 11H
ADDLW 43H
;
;
;
;
1-7
1-8
General Purpose
RAM
GP RAM
Special Function
Registers
EEPROM
1-9
07h
ALU
08h
Data Bus
D
09h
0Ah
0Bh
0Ch
0Dh
0Eh
0Fh
10h
WREG
Decoded Instruction
from Program Memory:
Opcode
Arithmetic/Logic Function to
be Performed
The
The PIC
PICuCs
uCs
d a
Result
Destination
Address
Address of Second Source
Operand
Register File
Concept: All of data
memory is part of
the register file, so
any location in data
memory may be
operated on
directly
All peripherals are
mapped into data
memory as a series
of registers
Orthogonal
g
Instruction Set:
ALL instructions
can operate
p
on ANY
data memory
location
1-10
1-11
8-bit registers
g
Their numbers varies from one chip to
another
another.
1-12
1-13
(Bytes)
32
80
512
1792
768
1792
4096
2048
SFR
(Bytes)
7
12
256
256
256
256
158
160
GPR
(Bytes)
25
68
256
1536
512
1536
3938
1888
1-14
1-15
1-16
GPRAM VS.
VS EEPROM
An add-on
dd
memory
Can be zero size
1-17
Bytes.
The File Register
has
1-18
1-19
1-20
1-21
1-22
MOVWF instruction
F indicates
d
for
f a file
f l register
MOVWF Address
It tells the CPU to copy the source
register, WREG, to a destination in the file
register.
A
WREG
The PIC uCs
1-23
Example 2-1
MOVLW 99H
MOVWF 12H
MOVLW 85H
MOVWF 13H
MOVLW 3FH
MOVWF 14H
MOVLW 63H
MOVWF 15H
MOVLW 12H
MOVWF 16H
WRFG
99
85
3F
63
12
Address Data
Add
D t
012H
013H
014H
015H
016H
Address
012H
013H
014H
015H
016H
Data
99
85
3F
63
12
1-24
Note
We cannot move literal
l
l values
l
directly
d
l into
1-25
ADDWF
Adds
dd together
h the
h content of
f WREG
EG and
da
If D=0 or (D=w)
The result will be placed in the WREG
If D=1 or (D=f)
The
Th result
l will
ill be
b placed
l
d in
i the
h file
fil register
i
The PIC uCs
1-26
Example 2-2
State the
h content of
f file
f l register location
l
MOVLW 0
Address
Address
Data
Address Data
Data
0
012H
012H
88
MOVWF 12H
66
012H 22
44
0
013H
013H
013H
MOVLW 22H
22
014H
014H
014H
015H
015H
ADDWF 12H,
12H F
015H
016H
016H
016H
ADDWF 12H, F
ADDWF 12H, F
ADDWF
DDWF 12H,
H, F
The PIC uCs
1-27
Example 2-3
State the
h content of
f file
f l register location
l
MOVLW 0
Address
0
MOVWF 12H
012H
013H
MOVLW 22H
22
014H
ADDWF 12H,
12H F
015H
016H
44
ADDWF 12H, W
66
ADDWF 12H, W
ADDWF
DDWF 12H,
H, W
88
The PIC uCs
Data
0
22
1-28
1-29
COMF instruction
COMF
F File
F l Reg. Address,
dd
D
It tells the CPU to complement
p
the content
of fileReg and places the results in WREG
or in fileReg.
g
1-30
Example 2-4
Write a simple
l program to toggle
l the
h SFR
F
Solution
MOVLW
MOVWF
B1 COMF
F
GOTO
55H
PORTB
P
PORTB,
B F
B1
55
Address Data
Address Data
F81H
55H
F81H
AAH
F82H
F82H
F83H
F83H
1-31
DECF instruction
DECF
DE
F File
F l Reg. Address,
dd
D
It tells the CPU to decrement the content
of fileReg and places the results in WREG
or in fileReg.
g
Address Data
3
012H
23
Example:
MOVLW
3
MOVWF 20H
DECF 20H,
20H F
DECF 20H, F
DECF 20H,
20H F
The PIC uCs
013H
014H
015H
016H
1-32
DECF instruction
DECF
DE
F File
F l Reg. Address,
dd
D
It tells the CPU to decrement the content
of fileReg and places the results in WREG
or in fileReg.
g
Address Data
3
012H
3
Example:
013H
2
MOVLW 3
MOVWF
20H
DECF 20H,
20H w
DECF 20H, w
DECF 20H,
20H w
The PIC uCs
1
0
014H
015H
016H
1-33
MOVF instruction
MOVF
F File
F l Reg. Address,
dd
D
It is intended to p
perform MOVFW
MOVFW
isnt existed
If D=0
D 0
Copies the content of fileReg (from I/O pin) to
WREG
If D=1
The content of the fileReg is copied to itself.
itself
(why?)
The PIC uCs
1-34
MOVF instruction
MOVF
F File
F l Reg. Address,
dd
0
WREG
The PIC uCs
1-35
Example 2-5
Write a simple
l program to get data
d
from
f
MOVWF PORTC
GOTO AGAIN
F81H
F81H
F82H
F82H
F83H
F83H
XX
XX
1-36
Example 2-6
Write a simple
l program to get data
d
from
f
Solution
MOVF PORTB,W
ADDLW 05H
MOVWF PORTC
55
5A
Address
F81H
F82H
F83H
Data
55H
55H
5AH
1-37
MOVFF instruction
It copies data
d
f
from one location
l
in FileReg
Fl
1-38
Example 2-7
Write a simple
l program to get data
d
from
f
GOTO AGAIN
F81H
F81H
F82H
F82H
F83H
F83H
XX
XX
XX
1-39
C
DC
Z
OV
N
Carry Flag
Digital Carry Flag
Z
Zero
Fl
Flag
Overflow Flag
Negative Flag
1-40
Figure 2
2-7
7. Bits of Status Register
1-41
Example 2-8
Show
h
the
h status of
f the
h C, DC,
D Z flags
fl
after
f
MOVLW 38H
ADDLW 2FH
Solution
38H + 2FH = 67H WREG=67H
WREG 67H
C=0
DC 1
DC=1
Z=0
The PIC uCs
1-42
Example 2-9
Show
h
the
h status of
f the
h C, DC,
D Z flags
fl
after the following addition instruction
MOVLW 9CH
ADDLW 64H
Solution
9CH + 64H = 100H WREG=
WREG 00H
C=1
DC 1
DC=1
Z=1
The PIC uCs
1-43
1-44
1-45
1-46
1-47
1-48
Assembler Directives
What
h is the
h difference
d ff
between
b
SET
Defines a constant or fixed address
Maybe reassigned later
ORG (Origin)
END
LIST
The PIC uCs
1-49
1-50
assembly
m y code into machine
m
code (object
( j
code)
1-51
Structure of Assembly
Language
Series of
f lines
l
Instruction
Directives
Consists of four field
1-52
1-53
1-54
List File
1-55
1-56
1-57
3-58
Example 2
2-11
11
Find
F d the
h ROM Memory Address
dd
of
f each
h of
f
1-59
P
Powering
i UP
At what address does the
CPU wake up when power
applied?
The uC wakes up at
mem r address 0000
memory
The PC has the value 0000
ORG directive put the
address of the first op
p
code at the memory
location 0000
The PIC uCs
1-60
1-61
Program Memory
1-62
byte
CPU
1-63
1-64
location
The high byte goes to the high memory
location
Intel uP and many uCs use little endian
1-65
1-66
for a chip
p
1-67
1-68
the op-code
Most of PIC18 instructions are 2
2-Byte
Byte
MOVLW
ADDLW
MOVWF
0000 1110
0000 1111
0110 111a
1-69
is 2M)
1110
1111
ssss
dddd
ssss
dddd
(0 fs FFF)
(0 fd FFF)
(th
(the code
d address
dd
b
bus width
idth iis 21,
21 which
hi h
1111
k7kkk kkkk0
k19kkk kkkk kkkk8
1-70
CPU
1.
2.
3.
1-71
CISC
Simple
p and Small
Complex
p
and large
g
instruction set
Regular and fixed
instruction format
Simple address
modes
Pipelined instruction
execution --> 95%
executed in one cycle
The PIC uCs
instruction set
Irregular instruction
format
Complex address
modes
May also pipeline
instruction execution
1-72
CISC
Provide large
g number
of CPU registers
Separated data and
program memory
Most operations are
register to register
Take shorter time to
design and debug
The PIC uCs
of CPU registers
Combined data and
program memory
Most operations can be
register to memory
Take longer time to
design and debug
1-73
1-74
1-75
1-76
1-77
Chapter 2: Summary
Sample PIC18
Instructions
Move,
Move
add,
add subtract
Next:
Branch,, Call and Time
Delay Loo
3-78