Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Microcontroller
Hsiao-Lung
H i L Chan
Ch
Dept Electrical Engineering
Chang Gung University, Taiwan
chanhl@mail cgu edu tw
chanhl@mail.cgu.edu.tw
Outline
2
Grading
g policy
p y
3
PIC microcontroller vs. Intel MCS-51 (8051)
( )
"Peripheral
Peripheral Interface Controller"
Controller made by Microchip
Technology
8-bit ALU
Intel's original
g in the 1980s.
Several companies offer MCS-51 as IP cores in FPGAs or
ASICs.
68HC MCU and AVR
68HC microcontroller
8-bit microcontroller family introduced by Motorola in 1985.
Now produced by Freescale Semiconductor
CISC (complex instruction set computer) design
AVR
8-bit RISC MCU was sold to Atmel from Nordic VLSI
5
Computer
p hardware organization
g
P ro c e sso r
C o m m o n B u s ( ad d r e s s , d at a, & c o n t r o l )
C o ntro l U nit
D at app at h
Arithm e tic
L o gic U nit M e m o ry
O utput In p u t
P r o g r am D at a
U nits U nits
R e gis te rs S t o r ag
g e S t o r ag
ge
F i g u r e 1 .1 C o m p u t e r O r g an i zat i o n
Microprocessor
p vs. microcontroller (MCU)
( )
Microprocessor
A processor implemented on a very large scale
integration
g (VLSI)
( ) chip
p
Peripheral chips are needed to construct a product
Microcontroller
The processor and peripheral functions implemented on
7
Features of PIC18 MCU
8-bit
8 bit CPU
Memory
Electrical erasable programmable read-only memory (EEPROM)
Flash memory
Static random-access memory (SRAM)
Ti
Timers, including
i l di counters,
t input
i t capture,
t output
t t compare, real-time
l ti
interrupt, and watchdog timer
Pulse-width modulation (PWM)
Parallel I/O ports
SPI, I2C, controller area network (CAN) serial interface
Universal asynchronous receiver transmitter (UART)
10-bit A/D converter
8
Computer
p software
Machine instruction
Assembly language
High-level
High level language
Source code
Object code
9
Source code and object
j code examples
p
10
PIC18 memoryy organization
g
11
PIC18 memoryy access
up to 2 MB of Inside the
c chipp
program memory
4096 registers
Program
Program
P counter
21-bit progam address 12-bit register address Data
Memory (PC)
Memory
Space Space
(a portion
PIC18 (Special
of this function
space is on CPU registers and
the c general
16-bit instruction bus 8-bit data bus
chip) purpose
RAM))
Fi
Figure 1.3
1 3 The
Th PIC18 memory spaces
12
P C < 2 0 :0 >
21
s ta c k le v e l 1
.
.
.
s ta c k le v e l 3 1
L o w P r io r ity I n t e r r u p t V e c t o r 000018h
Up to 128KB (at present time) of
Memory Space
program memory is inside the MCU
chip O n - c h ip a n d e x t e r n a l
p r o gr a m m e m o r y
User M
y x x xx x h
U n im p le m e n te d
p r o gr a m m e m o r y
R ea d '0 '
1FFFFFh
N o te . y c a n b e 0 o r 1 w h e re a s x c a n b e 0 -F
F ig u r e 1 . 5 P I C 1 8 P r o g r a m m e m o r y O r g a n iz a tio n ( r e d r a w w ith p e r m is s io n o f
M ic r o c h ip )
13
PIC18 pipeline
pp
14
15
BSR<3:0>
000h
= 0000 Access RAM 05Fh
B k0
Bank 060h
GPRs
0FFh
100h
= 0001
Bank 1 GPRs
1FFh
200h
= 0010
Bank 2 GPRs
2FFh
300h
= 0011 Access Bank
Bank 3 GPRs 000h
Access RAM low
3FFh 05Fh
400h Access RAM high 060h
SFRs
0FFh
Bank 4
to GPRs
Bank 13
DFFh
E00h
= 1110
Bank 14 GPRs
EFFh
Unused F00h
= 1111 F5Fh
Bank 15 F60h
SFRs
FFFh
16-bit instruction
Note. 1. BSR is the 4-bit bank select register.
Figure 1.4 Data memory map for PIC18 devices (redraw with permission of Microchip)
16
Access bank
PIC18 banks
4096 registers are divided into 16 banks.
Only one bank is active at a time.
B k switching
Bank it hi
When operating on a register in a different bank, bank
switching is needed.
Bank switching incurs overhead and may cause program
errors.
A
Access bank
b k
is created to minimize the problems of bank switching.
the lowest
o 96 bytes
by are
a general-purpose
g a pu po registers;
g ; the
highest 160 bytes, special function registers.
When operands (f) are in the access bank, no bank switching
is needed
needed.
17
Data movement instructions
18
movff
a 32-bit instruction
Copy a file register in one bank to a file register in another
bank without referring to the BSR register
Example
movff 0x100,0x300 ; copy data register 0x100 to data register 0x300
19
Instruction format: Byte-to-byte
y y operations
p
movff fs,, fd
1
15 12 11 0
opcode f (source file register)
15 12 11 0
1111 f (destination file register)
Figure 1.9 Byte to byte move operations (2 words) (redraw with permission of Microchip)
20
movwf
a 16-bit instruction
Copy a working register (WREG) to a file register in the
force access bank or the selected bank
Example
movwf 0x30,A ; copy WREG to a register in access bank
21
Instruction format: byte-oriented
y operations
p
movwf f,a
,
15 10 9 8 7 0
opcode d a f
22
movf
a 16-bit instruction
Copy a file register to the WREG register
Examples
p
movf 0x20,W,A
Copy register 0x20 in access bank to WREG
movff 0x20,W,BANKED
0 20 W BANKED
Copy register 0x20 in the specified by the BSR register to WREG
23
Status register
g
24
movlw/movlb
16-bit instructions
Copy a value to the WREG register/ set bank active
Examples
p
movlw 0x10 ; WREG 0x10
movb 3 ; load 3 into BSR (set bank 3 active)
25
Instruction format: literal operations
p
movlb k
movlw k
15 8 7 0
opcode k
26
swapf
p
a 16-bit instruction
Swap nibbles
Example
p
swapf 0x30,F,A
swap the upper and lower 4 bits of the register 0x30
27
lfstr
a 32
32-bit
bit instruction
Load 12-bit value in the FSR (file select register)
3 FSR (FSR0, FSR1, FSR2), each FSR has low byte and
high byte
Example
p
lfsr FSR1,0xB00 ; place the value 0xB00 in FSR1
28
ADD instruction: addwf
29
ADD instruction: addwfc
30
ADD instruction: addlw
31
Example: write an instruction sequence to increment the
contents of three registers 0x30-0x32 by 3
32
Example: write an instruction sequence to add the contents
of three registers 0x40-0x42 and store the sum at 0x50
33
Examples
p
addwf 0x20,F,A
add the register at 0x20 in access bank with WREG
and store the sum in 0x20.
addwf 0x20,F,BANKED
add the register 0x20 in the bank specified by the BSR
register and
d store the
h sum in 0x20.
34
SUB instruction: subwf
35
ADD instructions: subfwb, subwfb
36
SUB instruction: sublw
37
Example: write an instruction sequence to subtract 9 from
the registers 0x50-0x53
38
PIC18 addressing
g modes
Register direct
Immediate mode
Inherent mode
Indirect mode
Bit-direct
Bit direct addressing mode
39
Register
g direct
Use an 8
8-bit
bit value to specify a data register as operand.
movwf 0x45,A
movwf 0x1A,BANKED
,
movff 0x120,0x220
40
Immediate mode
41
Inherent mode
42
Indirect mode
43
Indirect addressing
g through
g FSR register
g
44
Examples
p
movwf INDF0
copy the contents of WREG to the memory specified by the
FSR0
movwf POSTDEC0
The same operation as the above but the content of FSR0 is
decremented by 1 after the operation.
movwf PREINC0
First increments FSR0 by 1 an then copy the contents of
WREG to the memory specified by the FSR0
clrf PLUSW0
Clear the memory at the address equal to the value of
WREG and that of FSR0
45
Examples
p (cont.)
( )
movf POSTDEC0,W
Copy the memory specified by the FSR0 to the WREG.
The content of FSR0 is incremented byy 1 after the operation.
p
movff POSTINC0,PRODL
Similar operation
p as the above but data is copied
p to the
register with the name PRODL
movff PLUSW2,PRODL
Copy the memory at the address (= WREG + FSR2) to
PRODL
addwf
dd f PREINC1 F
PREINC1,F
FSR1 is incremented by 1
Add the
th content
t t off memory b
by FSR1 with
ith that
th t off WREG.
WREG
The sum leave the memory specified by FSR1 46
Bit-direct addressing
g mode
47
Instruction format of control operations
p
49
Reference
50