Chapter 3
Introduction to PIC Microcontroller
Yu Luo
Assistant Professor
Department of Electrical and Computer Engineering
Office: 239 Simrall Engineering Building
Email: [Link]@[Link]
1
Memory Organization
• Memory on the PIC33/PIC24 µC is split into two types
• Program memory:
A. Program memory stores instructions
B. Program memory is non-volatile (contents are retained when power is lost)
C. Size: 4M x 24-bit. Each instruction is 24 bits (3 bytes) and the memory
can store up to 4M instructions
Operation code: 8-bit
• Data memory: data or memory location: 16-bit
A. Data memory stores data
B. Data memory is volatile (contents are lost when power is lost)
C. Size: 65535 x 8-bit. Each data is 8 bits (1 byte) and the memory can
store up to 65535 instructions —> Address of the data memory is 16 bits
!2
Storing Multi-byte Values (1)
Data memory is 65535 x 8 —> Each memory location can
store only 1-byte value
If the representation of a value is more than 1 byte, how to store it?
The value is stored in the data memory from least significant
byte to most significant byte, in increasing memory locations
!3
Storing Multi-byte Values (2)
Example: two 16-bits values Example: two 32-bits values
0x3DA1 and 0x1F2A 0x3DA1 135A and 0x9E6C 25E3
Most Least
significant byte significant byte
Location Contents Location Contents
0x1000 xxxxxx Address 0x1000 0x135A
0x1002 0x3DA1 0x1002
0x1002 0x3DA1
0x1004 0x1F2A Address 0x1004 0x25E3
0x1003
0x1006 xxxxxx 0x1006 0x9E6C
0x1008 xxxxxx 0x1008 xxxxxx
Important: The address index in the example only represents the location of the
least significant byte. The address of the most significant byte should plus 1
!4
Storing Multi-byte Values (3)
• The LSB of a 16-bit or 32-bit value must begin at even address
• If there are multiple 8-bit values, how to store them?
Example: four 8-bit values
0x3D, 0xA1, 0x1F, and 0x5A
Location Contents Address0x1000
0x1000 0xA13D
Address 0x1001
0x1002 0x5A1F
0x1004 xxxxxx Address0x1002
0x1006 xxxxxx Address 0x1003
0x1008 xxxxxx
!5
MOV Instruction — Register to Register (1)
• Mov instruction copies contents of source register to destination register
A. Source register can be one of the 16 working registers W0 through W15
B. Destination register can be one of the 16 working registers W0 through W15
C. mov.b —> byte mode mov —> word mode
Word mode: mov W2, W1 It copies all 16-bit value in W2 to W1
Byte mode: mov.b W2, W1 It copies the least significant byte in W2 to the
least significant byte in W1. The most significant
byte in W1 remains unchanged
!6
MOV Instruction — Register to Register (2)
Word mode: mov W2, W1 It copies all 16-bit data in W2 to W1
Byte mode: mov.b W2, W1 It copies the least significant byte in W2 to the least
significant byte in W1. The most significant byte in
W1 remains unchanged
Important !
!7
MOV Instruction — Register to Data Memory
• Mov instruction copies contents of source register to data memory location
A. Source register can be one of the 16 working registers W1 through W15
B. Destination is the memory address in data memory
C. It only supports WORD mode, so the memory location must be even address
Example: mov W3, 0x1002 It copies all 16-bits data in W3 to data
memory location 0x1002
!8
MOV Instruction — Data Memory to Register
• MOV instruction copies contents of data memory location to destination register
A. Source is the memory address in data memory
B. Destination is one of the 16 working registers W1 through W15
C. It only supports WORD mode, so the memory location must be even address
Example: mov 0x1002, W3 It copies all 16-bit data in data memory
location 0x1002 to W3
!9
MOV Instruction with Default Register W0 (1)
• W0 is the default register in PIC. It has another name: WREG
• Although W0 and WREG represent the same register. These
two names cannot exchange !!!
• If W0 interacts with the data memory in the direct mode, then we
must use WREG; otherwise, we use W0
For example:
A. Copy value from W0 to data memory location: mov WREG, 0x1002
B. Copy value from data memory location to W0: mov 0x1002, WREG
!10
MOV Instruction with Default Register W0 (2)
• If copy contents from the data memory to W1- W15, it only
supports WORD mode (16-bit)
Example: mov 0x1002, W3
• If copy contents from the data memory to W0, it supports either
WORD mode (16-bit) or BYTE mode (8-bit)
Example: mov.b 0x1001, WREG
It copies 1 byte content in memory location 0x1001 to the least significant
byte in W0. The most significant byte in W0 remains unchanged
!11
MOV Instruction with Default Register W0 (3)
• If copy contents from W1- W15 to the data memory, it only
supports WORD mode (16-bit)
Example: mov W3, 0x1002
• If copy contents from W0 to the data memory, it supports either
WORD mode (16-bit) or BYTE mode (8-bit)
Example: mov.b WREG, 0x1001
It copies the least significant byte in W0 to memory location 0x1001
!12
Move a Literal into a Working Register (1)
• General form of moving a literal into a working register:
A. Word mode: mov #lit16, Wn
It moves a 16-bit literal value into register Wn, where Wn can be W0 to W15
B. Byte mode: mov.b #lit8, Wn
It moves a 8-bit literal value into the least significant byte of register Wn, where
Wn can be W0 to W15. The most significant byte of Wn remains unchanged
Examples:
A. mov #0x1000, W2 It moves literal value 0x1000 into W2
B. mov.b #0xA3, W3 It moves literal value 0xA3 into least significant byte
of W3. The most significant byte of Wn remains
unchanged
It can be binary, hex, or dec (e.g., #0b1001, or #32, or #0xAC14)
!13
Move a Literal into a Working Register (2)
• Difference between mov #0xxxxx, Wn and mov 0xxxxx, Wn
A. mov #0x1000, W2 It moves literal value 0x1000 into W2
B. mov #32, W2 It moves literal value 32 into W2
C. mov.b #0xAD, W2 It moves literal value 0xAD into the least
significant byte of W2. The most significant byte
of W2, remains unchanged
D. mov 0x1000, W2 It moves content stored in the memory
location 0x1000 into W2
Example:
Original contents in W2 New contents in W2
mov.b #0xAD, W2
0x13CF 0x13AD
!14
ADD Instruction (1)
• General form of addition instruction
A. Word mode: add Wa, Wb, Wc
Values of Wa and Wb are added. The result is placed in Wc. Wa,
Wb, Wc can be register W0 to W15.
B. Byte mode: add.b Wa, Wb, Wc
Lest significant byte of Wa and Wb are added. The result is placed in the
least significant byte of Wc. The most significant byte of Wc remains
unchanged. Wa, Wb, Wc can be register W0 to W15
!15
ADD Instruction (2)
!16
ADD with Literals
• Addition instruction with a literal:
A. Word mode: add Wa, #lit5, Wb
Add value in Wa with a 5-bit literal value. The result is placed
in Wb. Wa and Wb can be register W0 to W15.
B. Byte mode: add.b Wa, #lit5, Wb
Add the least significant byte of Wa with a 5-bit literal value. The result is
placed in the least significant byte of Wb. The most significant byte of Wb
remains unchanged. Wa and Wb can be register W0 to W15
Important: #lit5 is a 5-bit literal value. That means its range is
0 — (25 - 1), i.e., 0 - 31
add W0, #4, W2 add W0, #60, W2
4 < 31 60 > 31
!17
ADD with Data Memory (1)
• Addition instruction with data memory:
A. Word mode: add f, WREG
Add data in the memory location f with value in W0. The result is placed in W0.
Register must be W0. The register W0 interacts with the data memory, so must
use name WREG
B. Byte mode: add.b f, WREG
Add the 1byte value in the memory location f with the least significant byte
in W0. The result is placed in the least significant byte of W0. The most
significant byte of W0 remains unchanged.
C. Two operand word mode: add f
Add data in the memory location f with value in W0. The result is placed in
memory location f
D. Two operand byte mode: add.b f
Add the 1 byte value in the memory location f with the least significant byte in
W0. The 1 byte result is placed in the memory location f.
!18
ADD with Data Memory (2)
!19
SUB Instruction (1)
• General form of subtraction instruction
A. Word mode: sub Wa, Wb, Wc
Values of Wa and Wb are subtracted. The result is placed in Wc. Wa,
Wb, Wc can be register W0 to W15.
B. Byte mode: sub.b Wa, Wb, Wc
Lest significant byte of Wa and Wb are subtracted. The result is placed in
the least significant byte of W2. The most significant byte of Wc remains
unchanged. Wa, Wb, Wc can be register W0 to W15
!20
SUB Instruction (2)
!21
SUB with Literals
• Subtraction instruction with Literals:
A. Word mode: sub Wa, #lit5, Wb
Subtract a 5-bit literal from value in Wa. The result is placed in Wb.
Wa and Wb can be register W0 to W15.
B. Byte mode: sub.b Wa, #lit5, Wb
Subtract the 5-bit literal from the least significant byte of Wa. The result is
placed the least significant byte of Wb. The most significant byte of Wb
remains unchanged. Wa and Wb can be register W0 to W15
Important: #lit5 is a 5-bit literal value. That means its range is
0 — (25 - 1), i.e., 0 - 31
sub W0, #4, W2 sub W0, #60, W2
4 < 31 60 > 31
!22
SUB with Data Memory (1)
• Subtraction instruction with data memory:
A. Word mode: sub f, WREG
Subtract value in W0 from the data in the memory location f. The result is
placed in W0. Register must be W0. The register W0 interacts with the data
memory, so must use name WREG
B. Byte mode: sub.b f, WREG
Subtract the least significant byte in W0 from the 1 byte data in the memory
location f. The result is placed in the least significant byte of W0. The most
significant byte of W0 remains unchanged.
C. One operand word mode: sub f
Subtract value in W0 from data in the memory location f. The result is placed
in memory location f
D. One operand byte mode: sub.b f
Subtract the least significant byte in W0 from the 1 byte data in the memory
location f. The 1 byte result is placed in the memory location f.
!23
Increment Instruction (1)
• Increment instruction from register to register:
A. Word mode: inc Wa, Wb
Value in Wa plus 1 and the result is placed in Wb
B. Byte mode: inc.b Wa, Wb
Least significant byte in Wa plus 1 and the result is placed in least significant
byte of Wb. Most significant byte in Wb remains unchanged
• Increment instruction from memory to memory:
A. Word mode: inc f, f
Value in memory location f plus 1 and the result is placed in memory location f
B. Byte mode: inc.b f, f
Least significant byte in memory location f plus 1 and the result is placed in
least significant byte of memory location f. Most significant byte in f remains
unchanged
!24
Increment Instruction (2)
• Increment instruction from memory to W0:
A. Word mode: inc f, WREG
Value in memory location f plus 1 and the result is placed in W0
B. Byte mode: inc.b f, WREG
Least significant byte in memory location f plus 1 and the result is placed in
least significant byte of W0. Most significant byte in W0 remains unchanged
Example:
Assume contents in W2 is 0x40FF, and in W4 is 0xAD12
inc W2, W4 —> Contents in W4 will be changed to 0x4100
inc.b W2, W4 —> Contents in W4 will be changed to 0xAD00
In the byte mode, the most significant byte remains unchanged
!25
Decrement Instruction (1)
• Decrement instruction from register to register:
A. Word mode: dec Wa, Wb
Value in Wa minus 1 and the result is placed in Wb
B. Byte mode: dec.b Wa, Wb
Least significant byte in Wa minus 1 and the result is placed in Least
significant byte of Wb. Most significant byte in Wb remains unchanged
• Decrement instruction from memory to memory:
A. Word mode: dec f, f
Value in memory location f minus 1 and the result is placed in f
B. Byte mode: dec.b f, f
Least significant byte in memory location f minus 1 and the result is placed
in least significant byte of memory location f. Most significant byte in f
remains unchanged
!26
Decrement Instruction (2)
• Decrement instruction from memory to W0:
A. Word mode: dec f, WREG
Value in memory location f minus 1 and the result is placed in W0
B. Byte mode: dec.b f, WREG
Least significant byte in memory location f minus 1 and the result is placed in
least significant byte of W0. Most significant byte in W0 remains unchanged
Example:
Assume contents in W2 is 0x1100, and in W4 is 0xAD12
dec W2, W4 —> Contents in W4 will be 0x10FF
dec.b W2, W4 —> Contents in W4 will be 0xADFF
In the byte mode, the most significant byte remains unchanged
!27
From C to Assembly — Naming Rules (1)
A. u8_a: Unsigned 8-bit variable a
B. u16_a: Unsigned 16-bit variable b
C. u32_a: Unsigned 32-bit variable c
D. i8_a: signed 8-bit variable a
E. i16_b: signed 16-bit variable b
D. i32_c: signed 32-bit variable c
E. au8_a: array a, each element in the array is an unsigned 8-bit value
F. au16_b: array b, each element in the array is an unsigned 16-bit value
G. au32_c: array c, each element in the array is an unsigned 32-bit value
!28
From C to Assembly — Naming Rules (2)
H. ai8_a: array a, each element in the array is a signed 8-bit value
I. ai16_b: array b, each element in the array is a signed 16-bit value
J. ai32_c: array c, each element in the array is a signed 32-bit value
K. pu8_a: pointer a, which points to an unsigned 8-bit value
L. pu16_b: pointer b, which points to an unsigned 16-bit value
M. pu32_c: pointer c, which points to an unsigned 32-bit value
N. pi8_a: pointer a, which points to a signed 8-bit value
O. pi16_b: pointer b, which points to a signed 16-bit value
P. pi32_c: pointer c, which points to a signed 32-bit value
!29
From C to Assembly — A Simple Program
C code Assembly code
mov.b #100, W0
u8_i = 100;
mov.b WREG, _u8_i
u8_i = u8_i + 1; inc.b _u8_i
mov.b _u8_i, WREG
u8_j = u8_i;
mov.b WREG, _u8_j
u8_j = u8_j - 1; dec.b _u8_j
mov.b _u8_i, WREG
u8_k = u8_i + u8_j; add.b _u8_j, WREG
mov.b WREG, _u8_k
• Literals cannot be directly moved to data memory
• Use the name WREG when W0 interacts of data memory
• In mov.b, only W0 can be used to interact with data memory
• You cannot directly move data from one memory location to another:
mov _u8_i, _u8_j
!30
From C to Assembly — Memory Allocation
.include pic24_all.h
.global _reset Preamble
.bss
_u8_i: .space 1 Allocate 1 byte
_u8_j: .space 1 memory for each 8-bit
_u8_k: .space 1 variable
.text
._reset:
mov #__SP_init, W15 Initialize the stack
First instruction located mov #__SPLIM_init, W0
at _reset label. That mov W0, SPLIM
means compiler will
convert instructions mov.b #100, W0
below “_reset” to mov.b WREG, _u8_i
machine code line-by-line inc.b _u8_i
mov.b _u8_i, WREG
mov.b WREG, _u8_j Main code
dec.b _u8_j
add.b _u8_j, WREG
mov.b _u8_i, WREG
mov.b WREG, _u8_k
!31
From C to Assembly with 16-bit Variable (1)
C code Assembly code
mov #2047, W0
u16_i = 2047;
mov WREG, _u16_i
u16_i = u16_i + 1; inc _u16_i
mov _u16_i, WREG
u16_j = u16_i;
mov WREG, _u16_j
u16_j = u16_j - 1; dec _u16_j
mov _u16_i, WREG
u16_k = u16_i + u16_j; add _u16_j, WREG
mov WREG, _u16_k
!32
From C to Assembly with 16-bit Variable (2)
.include pic24_all.h
.global _reset Preamble
.bss
_u16_i: .space 2
Allocate 2 bytes for
_u16_j: .space 2 each 16-bit variable
_u16_k: .space 2
.text
._reset:
mov #__SP_init, W15 Initialize the stack
mov #__SPLIM_init, W0
mov W0, SPLIM
mov #2047, W0
mov WREG, _u16_i
inc _u16_i
mov _u16_i, WREG
mov WREG, _u16_j Main code
dec _u16_j
add _u16_j, WREG
mov _u16_i, WREG
mov WREG, _u16_k
!33
Clock Cycles and Instruction Cycles
• Clock signal is used to synchronize different components in PIC
• Sources of clock signal:
A. External crystal oscillator
B. External RLC circuit
C. Internal RC oscillator
• For PIC33/PIC24 µC, the maximum clock frequency is Fosc = 80 MHz
• Important: An instruction cycle is equal to two clock cycles !!!
• Most PIC instructions take 1 instruction cycle, but some of them may
take more than 1
In general, if an instruction makes the program counter load new address
from the input, then it will take more than 1 instruction cycle
!34
Calculate Total Instruction Cycles
.include pic24_all.h
.global _reset
.bss
_u8_i: .space 1
_u8_j: .space 1
_u8_k: .space 1
.text
._reset: Instruction cycle
mov #__SP_init, W15 1
First instruction located mov #__SPLIM_init, W0 1
at _reset label. That mov W0, SPLIM 1
means compiler will
convert instructions mov.b #100, W0 1
below “_reset” to mov.b WREG, _u8_i 1
machine code line-by-line inc.b _u8_i 1
mov.b _u8_i, WREG 1
mov.b WREG, _u8_j 1
dec.b _u8_j 1
add.b _u8_j, WREG 1
mov.b _u8_i, WREG 1
mov.b WREG, _u8_k 1
!35
Total Time Consumption
Instruction Instruction cycle
mov #__SP_init, W15 1
mov #__SPLIM_init, W0 1
mov W0, SPLIM 1
mov.b #100, W0 1
mov.b WREG, _u8_i 1
inc.b _u8_i 1
mov.b _u8_i, WREG 1
mov.b WREG, _u8_j 1
dec.b _u8_j 1
add.b _u8_j, WREG 1
mov.b _u8_i, WREG 1
mov.b WREG, _u8_k 1
Total 12
Question: If the clock frequency is Fosc = 80 MHz. How long it will take to execute
the above instructions?
Step 1: total clock cycles = 2 x instruction cycles = 2 x 12 = 24
Step 2: total time consumption = total clock cycles / clock frequency
= 24 / 80 MHz = 0.2875 µs
!36