AVR Architecture
AVR Architecture
and embedded
systems
using assembly and c
Installing Programming
• Refer tools
to AVRstudioTutorial.pdf for ATMEL
in folder devices.and
\Lecture Notes\E-Books
Reference for tutorial on how to write, compile, and trace a simple program
ATmega32 in AVR Studio using Assembly language.
• AvrStudio4Setup.exe which is the IDE that allow ATmega32 assembly
program to be built, assembled, and simulated for execution, is in the
folder \Lecture Notes\AVRStudio4\Installer
• Install AvrStudio4Setup.exe first.
• Then install AVRStudio4.18SP3.exe to Upgrade
• Then Install WinAVR-20100110.exe which is in the folder \Lecture
Notes\AVRStudio4\Installer\WinAVR-20100110.
• Refer Overview on the AVR Instructions and Directive.pdf in \Lecture
Notes\E-Books and Reference folder
• Refer page 696 thru 732 of
Microcontroller and Embedded Systems E-book in folder \Lecture
Notes\E-Books and Reference for AVR Instruction Explained.
Topics
• AVR’s CPU
• Its architecture
• Some simple programs
• Data Memory access
• Program memory
• RISC architecture PROGRAM
RAM EEPROM Timers
Flash ROM
Program Data
Bus Bus
CPU
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
AVR’s CPU
• AVR’s CPU
• ALU
• 32 General Purpose
registers (R0 to R31) R0
• PC register ALU R1
R2
• Instruction decoder
…
SREG: I T H S V N Z C
R15
CPU R16
R17
…
PC
…
• LDI R19,132 SREG: I T H S V N Z C
CPU
R15
• LDI R23,0x27 R16
R17
• R23 = 0x27
PC
…
R30
Instruction decoder
R31
Instruction Register
registers
Some simple instructions
• There
2. Arithmetic calculation
are some instructions for doing Arithmetic and
logic operations; such as:
ADD, SUB, MUL, AND, etc.
• ADD Rd,Rs
• Rd = Rd + Rs
• Example: R0
R1
ALU
• ADD R25, R9 R2
…
• R25 = R25 + R9 SREG: I T H S V N Z C
•
CPU
R15
ADD R17,R30 R16
• R17 = R17 + R30 R17
PC
…
R30
Instruction decoder
R31
Instruction Register
registers
A simple program
• Write a program
LDI R16, 19that
;R16 calculates
= 19 19 + 95
LDI R20, 95 ;R20 = 95
ADD R16, R20 ;R16 = R16 + R20
R0
ALU R1
R2
…
SREG: I T H S V N Z C
CPU
R15
R16
R17
PC
…
R30
Instruction decoder
R31
Instruction Register
registers
A simple program
LDI R16, 19 ;R16 = 19
• Write a program that calculates;R20
LDI R20, 95
19 =+9595 + 5
LDI R21, 5 ;R21 = 5
ADD R16, R20 ;R16 = R16 + R20
ADD R16, R21 ;R16 = R16 + R21
…
SREG: I T H S V N Z C
CPU
R15
R16
R17
PC
…
R30
Instruction decoder
R31
Instruction Register
registers
R0 thru R15
• Only registers in the range R16 to R31 can be loaded immediate. We
cannot load a constant into the registers R0 to R15 directly. It would
have to be loaded into a valid register first then copied. To load the
value of 10 into register zero (R0):
Code:
•
LDI R16,10 ;Set R16 to value of 10
MOV R0,A ;Copy contents of R16 to R0
Some simple instructions
• INCcalculation
2. Arithmetic Rd
• Rd = Rd + 1
• Example:
• INC R25
• R25 = R25 + 1
R0
ALU R1
R2
• DEC Rd
…
• Rd = Rd - 1 SREG: I T H S V N Z C
CPU
R15
R16
• Example: R17
• PC
…
DEC R23
• R23 = R23 - 1 Instruction decoder
R30
R31
Instruction Register
registers
Address Name Address Name Address Name
I/O Mem. I/O Mem. I/O Mem.
$00 $20 TWBR $16 $36 PINB $2B $4B OCR1AH
...
Purpose
...
Registers R31
$001F I/O Address I/O
$0020 Example:
TWBR
TWSR
$00 Add contents
$01
of location
Example: 0x90
Store to contents
0x53 into the of location
PINS SPH 0x95
register.
Standard I/O Example:
and store What doesinthe
the result following
location
The
LDS
STS (Load
(Store instruction
0x313.
address of
directSPH do?space)
isdata
from
direct to 0x5E
data space)
Example: Write a program that stores 55 into location 0x80 of RAM.
...
Registers
$005F LDS
of RAM
SREG R20,2
Solution:
SPH $3E
into location 0x81.
LDS
STS Rd, addr ;[addr]=Rd
;Rd = [addr]
$3F
$0060 addr,Rd
General LDS R20, 0x90 Solution:
;R20 = [0x90]
purpose Solution:
Answer:
...
Registers R31
$001F I/O Address I/O
$0020 TWBR
Solution:
$00
OUT IOAddr,Rd
PINS
;[addr]=Rd
Standard I/O
TWSR $01
IN Rd,IOaddress
Using Names of;Rd
IO = [addr]
registers
...
...
Registers
SPH $3E
IN R20,PINC ;R20 = PINC
$005F SREG $3F
$0060 IN R21,PIND ;R21 = PIND
General Example:
Example:
purpose
...
...
LDI R20,
LDI
LDI
LDI R20, 0x9C
R20,
R16, 0x9C
0xA5
0x52 ;R16 = 0x38
0x38 Registers
R0 $001F IO Address
ALU LDI
LDI
LDI R21,
LDI R21,
R21, 0x9C
0x23
0x73
R17, 0x64
0x2FR1 ;R17 = 0x2F$0020 TWBR $00
TWSR $01
R2 Standard R20
IO
SUB R20,
SUB
ADD
ADD R20, R21
R20, R21
R21 ;subtract
;subtract
;add
R16, R17 R21
R21 toR21 from
R20from R20
;add R17 to R16Registers
...
...
…
SPH
SREG: I T H S V N Z C $3E
Solution:
Solution:
Solution:
$005F SREG $3F
CPU
Solution: R15 11
$0060
$520101
$9C
$A5 0010
R16
1001 1100
1010 1100
0101
$380011
$9C 1001 1000 General
- $730111R17
0011 purpose
+-- +$64
$9C
$23 10010100
0010 1100
0011
...
$2F0010
0110 1111 RAM
PC$DF 1101 1111 R20 = $DF
…
$00
$82$1000000
1000 0000
0010
$67 0110 R20
0111R20
1 0000 = $00
= $82
0000R16 = 0x67
R20 = 00(SRAM)
C = 1 because R21 is bigger than R20 and there is a borrow from D8 bit.
CCC===100because
becausethere
because R21 is
R21 is
is not
not
ahas
R30 bigger
bigger
carry than R20
than
beyond R20
the andbit.
and
D7 there is
there is no
no borrow
borrow from
from D8
D8 bit.
bit.
Z
C
Instruction == 00 decoder
because
because the
thereR20is no a value
carry other
beyond than
the D7 zero
bit. after the subtraction.
HZZ == 01 because
because the
the R20
R20R31 is zero
has afterother
a value the D3
subtraction.
than 0 after the subtraction.
H == 11 because
because there
there is
is aa carry
borrow
carry from
from
from the
D4D3
the toto the
D3.
to the D4
D4 bit.
bit.
ZHH == 00 Register
because there
because there isis no
no borrow
borrow from
fromaD4
D4 toto D3.
D3.in it after the addition.
Z == 10 because
Instruction because the
the R20
R16 (the
(the result)
registers result) has
has a value
value 0other than 0 after the addition.
$FFFF
Writing Immediate Value to SRAM
• You cannot copy immediate value directly into SRAM location in AVR.
• This must be done via GPRs
• Example: The following program adds content of location 0x220 to
location 0x221
Exercise
Implement the program that calculates 19 + 95 + 5 on AVR Studio.
Use debug and watch the change of the respective registers as you step
over through the program.