Beruflich Dokumente
Kultur Dokumente
Microprocessor (P)
C intended as a single chip solution, P requires external
support chips (memory, interface)
C has on-chip non-volatile memory for program storage,
P does not.
C has more interface functions on
on-chip
chip (serial interfaces,
interfaces
analog-to-digital conversion, timers, etc.) than P
C does not have virtual memory support (i.e., could not
run Linux),
Linux) while P does
does.
General purpose Ps are typically higher performance
(clock speed, data width, instruction set, cache) than Cs
Division between Ps and Cs becoming increasingly
blurred
V 0.2
Comments
Instruction width
24 bits
Clock speed
DC to 80 MHz
16 bit Architecture
16-bit
On-chip modules
24
D M
Data
Mem
Inst. Reg
23
address
Program Memory,
non-volatile,
non
volatile, up to
4M words (4M x 24)
16
address
16
16 x 16
Working
ki
Reg array
16
16
DOUT
D t
Data
16
16
ALU
16
17 x 17 Multiplier
p
not shown
3
Memoryy Organization
g
Memory on the PIC24 C family is split into two types:
Program Memory and Data Memory.
Memory
PIC24 instructions are stored in program memory, which is
non-volatile ((contents are retained when ppower is lost).
)
A PIC24 instruction is 24 bits wide (3 bytes).
PIC24HJ32GP202 program memory supports 11264
instructions; the PIC24 architecture can support up to 4M
instructions.
PIC24 data is stored in data memory, also known as the file
registers, and is a maximum size of 65536 x 8. Data memory
is volatile ((contents are lost when ppower is lost).
)
V 0.2
Program Memory
V 0.2
88-bit
bit, 16
16-bit
bit, 32
32-bit
bit Data
We will deal with data that is 8 bits, 16 bits (2 bytes), and
32 bits (4 bytes) in size.
size Initially we will use only 8 bit and
16 bit examples.
Size
8-bits
16-bit
32-bit
Unsigned
g
Range
g
0 to 28-1 (0 to 255, 0 to 0xFF)
0 to 216-1 (0 to 65536, 0 to 0xFFFF)
0 to 232-1 (0 to 4,294,967,295),
4 294 967 295) 0 to 0xFFFFFFFF)
V 0.2
() read as contents of
Memory
Literal
Memory
MOV
Wns, fALL
Register MOV{.B} WREG, f
direct
(Wns/WREG) f{ALL}
Register
indirect
Key:
MOV{.B} #lit8/16, Wnd
lit Wnd
f: near memory (08095)
Register direct
Register indirect
PIC24 assembly
Data transfer
Yellow shows
varying forms of the
ssamee instruction
s uc o
(Wso) Wdo
Wso is
W
i one off the
h 16 working
ki registers
i
W0 through
h
h W15 (s
( indicates
i di
Wso
W is
i an
operand source register for the operation).
Wdo is one of the 16 working registers W0 through W15 (d indicates Wdo is
an operand destination register for the operation).
mov W3, W5
mov b W3
mov.b
W3, W5
(W3) W5
(W3
(W3.LSB)
LSB) W5.LSB
W5 LSB
(word operation)
(byte operation)
The addressing mode used for both the source and destination
operands
d is
i called
ll d register direct.
d
Th mov instruction
The
i
i supports
other addressing modes which are
V 0.2not shown.
12
(Wns) f
(W3) 0x1000
15
V 0.2
16
V 0.2
17
(f) Wndd
(0x1000) W3
18
V 0.2
19
20
WREG, f
(WREG) f
WREG, 0x1000
(W0) 0x1000
word operation
p
21
A byte
y copy
py operation
p
is shown.
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
From: Reese/Bruce/Jones, Microcontrollers: From Assembly to C with the PIC24 Family.
V 0.2
22
V 0.2
23
f, WREG
MOV{.B} f
(f )) WREG
(f ) f
word operation
only
y lower 8-bits of W0 are affected.
Copies contents of 0x1000 back to
MOV 0x1000
itself, will see usefulness of this later
Word copy must be from even (word-aligned) data memory location.
Note: The MOV f,Wnd instruction cannot be used for byte operations!
V 0.2
24
MOV{ B} f {,WREG}
MOV{.B}
{ WREG} Format
V 0.2
25
V 0.2
26
lit16 Wnd
(word operation)
0x1000 W2
MOV.B #0xAB, W3
0xAB W3.lsb
V 0.2
27
More on Literals
Observe that the following two instructions are very different!
MOV #0x1000, W2
MOV 0x1000,W2
V 0.2
29
V 0.2
30
Indirect Addressing
M with
Mov
i h indirect
i di
Addressing:
Add
i
mov{.b} [Wso], [Wdo]
((Wso)) (Wdo)
Indirect Addressing
MOV E
Example
ample
V 0.2
33
Wb, Ws, Wd
(Wb) + (Ws) Wd
W0, W1, W2
(W0) + (W1)
ADD
W2,
W2 W2,
W2 W2
W2 = W2 + W2 = W2*2
W2
35
0x1AF3
0x8B1A
0x64DE
0xFB90
Before
0x1AF3
+ 0x8B1A
0xA60D
W0
W1
W2
W3
0x1AF3
0x8B1A
0xA60D
0xFB90
After
Modified
W0
W1
W2
W3
0x1A23
0x8B1A
0x64 0D
0 FB90
0xFB90
After
Modified
W0
W1
W2
W3
0x1AF3
0x8B1A
0xC9BC
0xFB90
After
Modified
0x1A F3
0x8B 1A
0x64DE
0 FB90
0xFB90
Before
0xF3
+ 0x1A
0x0D
Result limited
to 8-bits!
0x1AF3
0x8B1A
0x64DE
0xFB90
Before
0x64DE
+ 0x64DE
0xC9BC
V 0.2
36
(Wb) (Ws) Wd
W0, W1, W2
(W0) (W1) W2
SUB
W1,W0, W2
(W1) (W0) W2
SUB B
SUB.B
W0,
W0 W1,
W1 W2
Lower 8 bits of W0
W0, W1 are subtracted
and placed in the lower 8-bits of W2
V 0.9
37
V 0.9
38
(Wb) #lit5 Wd
(Wb) #lit5 Wd
W0, #4, W2
(W0) + 4 W2
SUB.B
W1,#8, W3
(W1) 8 W3
ADD
W0, #60, W1
(f) + (WREG) f
ADD 0x1000
ADD 0x1000,WREG
V 0.2
40
(0x1000) W1
(W0) + (W1) W1
(W1) 0x1000
41
V 0.2
42
(f) (WREG) f
SUB{.B} f, WREG
WREG is W0
W0, f is limited to first 8192 bytes of memory.
memory
One of the operands, either f or WREG is always destroyed!
SUB 0x1000
SUB 0x1000,WREG
V 0.2
0.9
43
Increment
Increment operation,
operation register
register-to-register
to register form:
INC{.B} Ws, Wd
(Ws) +1 Wd
I
Increment
t operation,
ti memory to
t memory/WREG
/WREG form:
f
INC{.B}
INC{.B} f, WREG
(f) + 1 f
(f) + 1 WREG
W2,, W4
((W2)) + 1 W4
INC.B W3, W3
(W3.lsb) + 1 W3.lsb
INC
(0x1000) +1 0x1000
0x1000
44
Decrement
Decrement operation,
operation register
register-to-register
to register form:
DEC{.B} Ws, Wd
(Ws) 1 Wd
I
Increment
t operation,
ti memory to
t memory/WREG
/WREG form:
f
DEC{.B} f
DEC{.B} f, WREG
(f) 1 f
(f) 1 WREG
W2,, W4
((W2)) 1 W4
DEC.B W3, W3
(W3.lsb) 1 W3.lsb
DEC
(0x1000) 1 0x1000
0x1000
24
D M
Data
Mem
Inst. Reg
23
address
Program Memory,
non-volatile,
non
volatile, up to
4M words (4M x 24)
16
address
16
16 x 16
Working
ki
Reg array
16
16
DOUT
D t
Data
16
16
ALU
16
17 x 17 Multiplier
p
not shown
46
47
A GOTO iinstruction
t ti iis an unconditional
diti l jump.
j
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
V 0.2
48
V 0.2
49
V 0.2
50
ADD forms
ADD Wb, Ws, Wd
Legal:
ADD W0, W1, W2
ADD W0, [W1], [W4]
Ill l
Illegal:
ADD [W0],W1,W2
V 0.2
51
A Simple Program
In this class, will present programs in C form, then translate
(compile) them to PIC24 C assembly language.
C Program equivalent
A uint8 variable is
8 bits (1 byte)
// i = 100
i = i + 1;
// i++, i = 101
j = i;
// j is 101
j = j - 1;
// j--, j is 100
k = j + i;
// k = 201
V 0.2
53
V 0.2
54
C to PIC24 Assembly
55
56
.include "p24Hxxxx.inc"
.global __reset
.bss
;reserve space for variables
i:
.space 1
j:
.space 1
k:
.space 1
.text
;Start of Code section
__reset: ; first instruction located at __reset label
mov #__SP_init,
# S i it W15
15
;;initialize
i iti li
stack
t k pointer
i t
mov #__SPLIM_init,W0
mov W0,SPLIM
;;initialize Stack limit reg.
avalue = 100
This file can be assembled
; i = 100;
mov.b #avalue, W0
; W0 = 100
by the MPLAB
mov.b WREG,i
; i = 100
mptst_byte.s
p _ y
; i = i + 1;
inc.b
i
; j = i
mov.b
i,WREG
mov.b
WREG,j
; j = j 1;
dec b
dec.b
j
; k = j + i
mov.b
i,WREG
add.b
j,WREG
mov.b
WREG,k
done:
goto
done
; i = i + 1
; W0 = i
; j = W0
; j= j 1
; W0 = i
; W0 = W0+j
; k = W0
(WREG is W0)
;loop forever
V 0.2
57
mptst
p _byte.s
y
((cont.))
.include "p24Hxxxx.inc"
.global
g
__reset
.bss
i
i:
j:
k:
;reserve
.space
space
.space
.space
mptst_byte.s (cont.)
.text
__reset: mov #__SP_init, W15
mov #__SPLIM_init,W0
mov W0,SPLIM
W0 SPLIM
avalue = 100
59
; W0 = 100
; i = 100
; i = i + 1
mov.b
mov
b
i,WREG
i WREG
mov.b
WREG,j
; j = j 1;
dec.b
j
; k = j + i
mov.b
i,WREG
add.b
j,WREG
mov.b
WREG,k
; W0 = i
; j = W0
; j= j 1
; W0 = i
; W0 = W0+j
; k = W0
V 0.2
60
mptst
p _byte.s
y
((cont.))
done:
goto
done
;loop forever
.end
A comment
An assembler directive specifying
p
y g the end of
the program in this file.
V 0.2
61
An Alternate Solution
C Program equivalent
#define avalue 100
uint8 i,j,k;
i
i
j
j
k
=
=
=
=
=
avalue;
i + 1;
i;
;
j - 1;
j + i;
//
//
//
//
//
i = 100
i++, i = 101
j is 101
j--, j is 100
k = 201
63
Important!!!!!!!
mov.b #avalue, W0
mov.b WREG,i
inc b
inc.b
i
mov.b
i,WREG
mov.b
WREG,j
d
dec.b
b
j
mov.b
i,WREG
add.b
j,WREG
mov.b
WREG,k
V 0.2
Total
1
1
1
1
1
1
1
1
1
1
1
1
12
65
A uint16 variable is
16 bits (1 byte)
// i = 2047
i = i + 1;
// i++, i = 2048
j = i;
// j is 2048
j = j - 1;
// j--, j is 2047
k = j + i;
// k = 4095
V 0.2
66
.include "p24Hxxxx.inc"
.global __reset
.bss
;reserve space for variables
i:
.space 2
j
j:
.space 2
k:
.space 2
.text
;Start of Code section
__reset: ; first instruction located at __reset label
mov #__SP_init,
# SP i it w15
15
;initialize
i iti li
stack
t k pointer
i t
mov #__SPLIM_init,W0
mov W0,SPLIM
;initialize stack limit reg
avalue = 2048
; i = 2048;
mov #avalue, W0
mov WREG,i
; i = i +
inc
; j = i
mov
mov
; j = j
dec
; k = j +
mov
add
mov
done:
goto
1;
i
Instructions now
perform WORD (16-bit)
operations
p
((the .b
qualifier is removed).
; W0 = 2048
; i = 2048
; i = i + 1
i,WREG
WREG,j
1;
j
i
i,WREG
j,WREG
WREG,k
done
; W0 = i
; j = W0
; j= j 1
; W0 = i
; W0 = W0+j
; k = W0
;loop forever
(WREG is W0)
V 0.2
67
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
=
=
=
=
=
avalue;
i + 1;
i;
;
j - 1;
j + i;
//
//
//
//
//
i = 2047
i++, i = 2048
j is 2048
j--, j is 2047
k = 4095
V 0.2
68
mov #__SP_init,
__ _
W15
mov #__SPLIM_init,W0
mov W0,SPLIM
mov #avalue, W0
mov WREG,i
inc
i
mov
i
i,WREG
WREG
mov
WREG,j
dec
j
mov
i,WREG
add
j,WREG
mov
WREG,k
Total
V 0.2
1
1
1
1
1
1
1
1
1
1
1
1
12
69
70
Review: Units
In this class, units are always used for physical quantity:
Time
Frequency
microseconds (s = 10-6 s)
(ns = 10-9 s)
nanoseconds
71
PIC24H Family
Microchip has an extensive line of PICmicro
microcontrollers, with the PIC24 family introduced
in 2005.
The PIC16 and PIC18 are older versions of the
PIC i family,
PICmicro
f il have
h
been
b
severall previous
i
generations.
Do not assume that because something is done one
way in the PIC24, that it is the most efficient
method for accomplishing that action.
The datasheet for the PIC24HJ32GP202 is found on
the class web site.
V 0.2
72
16F87x
(Fall 2003)
PIC18F242
(Summer 2004)
PIC24H
(Summer 2008)
Instruction
width
idth
14 bits
16 bits
24 bits
Program
memory
8K instr.
8K instructions
~10K instructions
Data
Memory
368 bytes
1536 bytes
2048 bytes
Clock speed
Max 20 MHz, 4
clks=1instr
Max 40 MHz
4 clks=1instr
Max 80 MHz
2 clks=1 instr
Architecture
Accumulator, 88-bit
bit
architecture
General purpose
register, 16-bit
architecture
73
74