Professional Documents
Culture Documents
1
AVR Micro-Controller
2
AVR Micro-Controller
3
AVR Micro-Controller
4
AVR Micro-Controller
5
AVR Micro-Controller
9
AVR Block Diagram
10
Addressing modes
Way of accessing memory
Register Addressing (32 GPR, 64 I/O)
Data Addressing (SRAM)
Program Addressing
11
Register Addressing
Three modes
Register Direct, Single Register (R0-R31)
Register Direct, Two Register (R0-R31)
I/O Direct Addressing (64 I/O
Registers)
12
Register Direct, Single Register
Register File
0
15 4 0
OP d
d
Example:
INC R16
CLR R22
31
13
Register Direct, 2 Register
Register File
0
15 5 4 0
OP r d
Example:
ADD R16, R17 31
CP R22, R5
MOV R0, R1
14
I/O Direct
I/O Memory
0
15 5 4 0
OP n p
Example:
IN R16, PIND
OUT PORTC, R16
63
15
Data Addressing
There are five modes
Direct Data Addressing
Data Indirect with Displacement (Y or Z)
Data Indirect (X, Y, or Z)
Data Indirect with pre-decrement (X, Y,
or Z).
Data Indirect with post-decrement (X, Y,
or Z) 16
Data Direct
Data space
$0000
31 19 16
OP p
16 LSBs
15 0
Example:
STS 0x1000, R16
$FFFF
17
Data Indirect
Data space
$0000
X, Y, or Z Register
15 0
Example:
LD R16, Y
ST Z, R16
$FFFF
18
Data Indirect w/ Displacement
Data space
$0000
Y or Z Register
15 0
Example:
LDD R16, Y+0x10
STD Z+0x20, R16
15 10 5 0 $FFFF
OP n a
19
Data Indirect: Pre-Decrement
Data space
$0000
X, Y or Z Register
15 0
Example:
LD R16, -Z
ST -Z, R16
-1
$FFFF
20
Data Indirect: Post-Increment
Data space
$0000
X, Y or Z Register
15 0
Example:
LD R16, Z+
ST Z+, R16
1
$FFFF
21
Program Addressing
There are five modes
Program Memory Constant Addressing
Program Memory Addressing with post-
increment
Direct Program Addressing
Indirect Program Addressing
Relative Program Addressing
22
Program Memory constant
Addressing
Program Memory
$0000
Z Register
15 0
LSB
Example:
LDI ZL, 0x55
LDI ZH, 0x02; Z = 0x0255
LPM R17, Z
SPM Z, R17 $FFFF
23
Program Memory Addressing
with post-increment
Program Memory
$0000
Z Register
15 0
LSB
1 +
Example:
LDI ZL, 0x55
$FFFF
LDI ZH, 0x02; Z = 0x0255
LPM R16, Z+
LPM R17, Z 24
Direct Program Memory
Addressing
31 16 Program Memory
OP 6 LSB $0000
16 LSB
15 0
21 0
PC
Example:
JMP Address1
RJMP Address2
CALL Function1
RCALL Function2 $FFFF
25
Relative Program Memory
Addressing
Program Memory
$0000
PC
15 0
+
15 12 11 0
OP k
Example: $FFFF
RJMP
RCALL
26
AVR Instruction Set
Load/store architecture
At most two operands in each
instruction
Most instructions are two byte long
Some instructions are four byte
27
AVR Instruction Set
Four categories
Arithmetic and logic instructions
Program Control instructions
Data transfer instructions
Bit and bit test instructions
28
General-purpose Register
AVR
32 general registers
Named r0, r1,…,r31
Broken into two parts with 16 register
each
Each register is assigned a memory
address in SRAM space
29
General-purpose Register
AVR
Register r0 and r26 through r31 have
additional function
• R0 is used in the instruction LPM (load
program memory)
• Register x (r27:r26), y (r29:28) &
z(r31:r30) used as a pointer register
30
Cont…
Address
0x00 r0
0x01 r1
.
.
.
0x1A r26 x register low byte
0x1B r27 x register high byte
0x1C r28 y register low byte
0x1D r29 y register high byte
0x1E r30 z register low byte
0x1F r31 z register high byte
31
The Status Register in AVR
32
The Status Register in AVR
33
The Status Register in AVR
I T H S V N Z C
Bit 7 6 5 4 3 2 1 0
34
The Status Register in AVR
35
The Status Register in AVR
36
The Status Register in AVR
38
The Status Register in AVR
39
Data Transfer Instruction
40
Data Transfer Instruction
41
Arithmetic & Logic
Instructions
add, adc, inc
sub, sbc, dec
mul, muls, mulsu
and, or, eor
clr, cbr, cp, cpc, cpi, tst
com, neg
42
Add without carry
43
Add with carry
44
Increment
Example:
sub r13, r12; subtract r12 from r13
47
Subtract with carry
Example:
sub r0, r1 ; subtract r1 from r0
sbc r2, r3 ; subtract with carry
48
Subtract a constant
Example:
subi r16, 5 ; subtract 5 from r16
49
Subtract a constant
Example:
suci r16, 5 ; subtract 5 from r16 minus carry
50
Decrement
Syntax: dec Rd
Operand: Rd {r0, r1,…., r31}
Operation: Rd Rd - 1
Flags affected: S, V, N, C
Example:
ldi r17,$10 ; Load 10 to r17
loop: add r1,r2 ; add r1 and r2
dec r17 ; decrement r17
brne loop ; branch to loop if not equal
nop
51
Subtract constant to 16-bit
register
SBIW – Subtract a constant to a 16
bit register
Example:
SBIW R31:R30, 63
54
Signed & unsigned
Multiplication
Syntax: mulsu Rd, Rr
Operand: Rd, Rr {r0, r1,…., r31}
Operation: r1,r0 Rd * Rr
Flags affected: Z, C
Example:
muls r6, r7
55
Bitwise AND
Example:
ldi r2, 0b00110101
ldi r16, 1
and r2, r16
56
Bitwise AND with constant
Example:
andi r16, 0xF0
57
Bitwise OR
Syntax: or Rd, Rr
Operand: Rd, Rr {r0, r1,…., r31}
Operation: Rd Rd + Rr
Flags affected: S, V, N, Z
Example:
ldi r2, 0b00110101
ldi r16, 1
or r2, r16
58
Bitwise OR with constant
Example:
ori r18, 0xF0
59
Bitwise XOR
Example:
eor r4, r4
eor r0, r1
60
1’s Complement
Example:
ldi r0, 0x4A
com r0
61
2’s Complement
Example:
ldi r0, 0x4A
neg r0
62
Bit Manipulation
Example:
ldi r16, $AA ldi r16, $AA
andi r16, $F0 cbr r16, $0F
63
Bit Manipulation
64
Bit Manipulation
clr – loads a $00 to a register and does it
by performing eor to itself
- Does modify flags
tst – Checks if a register is zero or
negative
- The register is not changed but flags
are modified.
65
Branch Instruction
RJMP
Relative jump to an address within +/-
2k.
Unconditional jump
JMP
Absolute jump to an address within 4M
(words/addresses) of program memory.
66
Branch Instruction
IJMP
Indirect jump to an address pointed by
the Z.
Maximum jump = 64k
67
Branch Instruction
RCALL
Relative call to an address within +/- 2k
CALL
Absolute call to an address within 4M of
program memory
ICALL
Indirect call to an address pointed by the Z.
68
Branch Instruction
RET
Return from subroutine
RETI
Return from interrupt
69
Skip Instruction
SBRC
Skip the next instruction if a bit in a
register is 0
Example:
sbcr r0, 4
rcall task
nop
70
Skip Instruction
SBRS
Skip the next instruction if a bit in a
register is 1
Example:
sbcs r0, 4
rcall task
nop
71
Skip Instruction
SBIC/SBIS
Similar to SBCR/SBCS but checks a bit
from an I/O register
Example:
sbic pinc, 1
mul r0, r1
72
Comparison Instruction
CP
Performs comparison of two register
Content doesn’t change
Example:
ldi r16, 100
ldi r17, 200
cp r16, r17
73
Comparison Instruction
CPC
Similar to CP but carry is added
Example:
cpc r16, r17 ; r16 – (r17 + C)
CPI
Comparison of register with constant
Example:
cpi r16, 255 74
Comparison Instruction
CPSE
Compare and skip instruction
Example:
return:
inc r13
cpse r0, r13
rjmp return
sub r0, r13
75
Branch Instruction
BREQ
Jump to the specified relative address if
Z=1
Example:
cp r10, r20
breq they_re_equal
nop
they_re_equal:
nop 76
Branch Instruction
BRNE
Jump to the specified relative address if
Z=0
Example:
cp r10, r20
brne different
nop
different:
nop 77
Bit & bit test Instruction
78
Bit & bit test Instruction
79
AVR assembly directives
80
AVR assembly directives
.org $0046
defining the memory address of the next
instruction
M_LOOP:
Label (e.g. for jumps)
81