Professional Documents
Culture Documents
Introduction to PIC18
Microcontroller
PIC Architecture
Harvard Architecture which includes:
Microprocessor unit (MPU)
Program memory for instructions
Data memory for data
I/O ports
Support devices such as timers
Data Memory
12-bit address bus
Address up to 212 = 4k bytes memory space
8-bit data bus
5
Registers
Control unit
Provides timing and control signals to various Read and
Write operations
6
Program Memory
Each PIC18 member has a 21-bit
address bus for program memory.
Can address up to 2MB program
memory space.
In PIC18F4520, only 32768 bytes were
implemented, capable of storing 16384
instructions (most instructions are 2
bytes).
The first byte in an instruction
specifies the operation to be executed
(such as addition) and the second byte
specifies the operands (literal (value)
or an address in data memory).
7
10
11
13
Outline
15
Hexadecimal Numbers
Four ways to show that hex representation
is used:
1. Put nothing in front or back of the number:
movlw 99. (Hex is the default representation)
2. Use h (or H) right after the number: movlw 99H
3. Put 0x (or 0X) before the number: movlw 0x99
4. Put h in front of the number, with single quotes
around the number: movlw h 99
18
movlw
movlw stands for move a literal into
WREG.
Literal means that an actual value must
be specified.
Format: movlw K where K is a 8-bit
value ranging from 0-255.
e.g., movlw 0x25 moves 25 (in hex) to
WREG
19
addlw
addlw stands for add literal and WREG.
addlw adds a literal and WREG and stores
the result in WREG
Format: addlw K where K is a 8-bit value
ranging from 0-255.
e.g., addlw 0x25 adds 25 (in hex) and
WREG
e.g., Add two numbers 25H and 34H:
movlw 0x25
addlw 0x34
20
Result: WREG = 0x59
21
9C
+ 64
00
26
movwf
movwf stands for MOVe WREG into File
Register
File Registers just refer to any register (i.e., SFR
or GPR)
Format: movwf f[, a] where
f is the 8-bit address of the destination file
register.
a (optional) specifies whether you use the access
bank (a = A = 0) or BSR (a = BANKED = 1).
55
27
28
addwf
addwf stands for ADDe WREG and File
Register
The destination of the move can be:
WREG (Typical)
the file register
addwf
30
addwf
e.g. 1: Move 22H into two file registers
with addresses 0x05 and 0x06 in access
bank, add the contents of all three
registers and put the result in WREG:
movlw 0x22
Address
Data
WREG
66
movwf 0x05, A
0x005
22
0x006
22
movwf 0x06, A
addwf 0x05, W
addwf 0x06, W
31
addwf
e.g. 2: Same as e.g. 1, but put the result
in file register 0x06 in the access bank
movlw 0x22
Address
Data
movwf 0x05, A
WREG
44
movwf 0x06, A
0x005
22
0x006
66
addwf 0x05, W, A
addwf 0x06, F, A
32
Status register
8-bit register, only 5 bits are used
These 5 bits represent five conditional flags, indicating
conditions after an instruction is executed.
movf
movf stands for MOVe file register
The destination of the move can be:
WREG (Typical)
the file register itself
movf
35
36
addwfc
addwfc stands for ADDe WREG, File
Register and Carry Bit
Format: addwfc f[, d, a]
Used in addition of values more than 8bit
37
addwfc
38
41