0% found this document useful (0 votes)
65 views25 pages

AVR Architecture

The document discusses the AVR microcontroller and its architecture. It includes instructions on installing programming tools for AVR devices using Assembly and C languages. It also provides an overview of the AVR CPU architecture, including its ALU, general purpose registers, program counter, and instruction decoder. Simple example programs are given to demonstrate loading values into registers and performing arithmetic operations like addition and subtraction.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
65 views25 pages

AVR Architecture

The document discusses the AVR microcontroller and its architecture. It includes instructions on installing programming tools for AVR devices using Assembly and C languages. It also provides an overview of the AVR CPU architecture, including its ALU, general purpose registers, program counter, and instruction decoder. Simple example programs are given to demonstrate loading values into registers and performing arithmetic operations like addition and subtraction.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

The AVR microcontroller

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

Instruction decoder R30


R31
Instruction Register
registers
Some simple instructions
1. Loading
LDI values
(Load into the general purpose registers
Immediate)
• LDI Rd, k
• Its equivalent in high level languages:
Rd = k
• Example:
R0
• LDI R16,53 ALU R1
R2
• R16 = 53


• 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

LDI R16, 19 ;R16 = 19


LDI R20, 95 ;R20 = 95
ADD R16, R20 ;R16 = R16 + R20
LDI R20, 5 ;R20 = 5
ADD R16, R20 ;R16 = R16 + R20
Some simple instructions
• SUBcalculation
2. Arithmetic Rd,Rs
• Rd = Rd - Rs
• Example:
• SUB R25, R9
• R25 = R25 - R9
• SUB R17,R30 R0
ALU R1
• R17 = R17 - R30 R2


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

Data Address Space


$01 $21 TWSR $17 $37 DDRB $2C $4C TCNT1L
$02 $22 TWAR $18 $38 PORTB $2D $4D TCNT1H
$03 $23 TWDR $19 $39 PINA $2E $4E TCCR1B
$04 $24 ADCL $1A $3A DDRA $2F $4F TCCR1A
$05 $25 ADCH $1B $3B PORTA $30 $50 SFIOR
$06 $26 ADCSRA $1C $3C EECR OCDR
$07 $27 ADMUX $1D $3D EEDR $31 $51 General
OSCCAL
RAM EEPROM Timers
$08 $28 ACSR $1E $3E EEARL $32 $52 Purpose
TCNT0
$09 $29 UBRRL $1F $3F PROGRAM $33
EEARH $53 Registers
TCCR0
$0A $2A UCSRB UBRRCROM $34 $54 MCUCSR
$20 $40
$0B $2B UCSRA UBRRH $35 $55 MCUCR
$0C $2C UDR $21 $41 WDTCR
Program
$36 CPU
$56 TWCRData
$0D $2D SPCR $22 $42 ASSR Bus
$37 $57 SPMCRBus address bus
$0E $2E SPSR $23 $43 OCR2 $38 $58 TIFR data bus
$0F $2F SPDR control bus
$10 $30 PIND
$24 $44 TCNT2 $39 $59 TIMSK Data
$25 $45 TCCR2 $3A $5A GIFR
$11 $31 DDRD $268 bit $46 ICR1L $3B $5B GICR Bus
Data Address
$12 $32 PORTD $27R0 $47 ICR1H $3C $5C OCR0
Space $33
$13 PINC $28R1 $48 OCR1BL $3D $5D SPL
$14
$0000 $34 DDRC $29R2 $49 OCR1BH $3E $5E SPH Interrupt Other
$0001 $35
$15 General
PORTC $2A $4A OCR1AL $3E $5EOSCSREG Ports
Peripherals
Unit

...
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.
...

Example: Write a program that copies the contents of location 0x80


...

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:
...

RAM LDS R21, 0x95 ;R21 =R20,


[0x95]
(SRAM) LDI R20, 55 Example:
Solution: LDI
;R20 = 55 0x53 ;R20 = 0x53
It copies
ADD the contents
R20, R21 of R2 into
;R20
STS R20;
=0x5E,
R20 +R20as 2 is
R21 the=address
;SPH R20 of R2.
LDS R20, 0x80 LDS
STS 0x80, R20 STS;R20R1,
;[0x80] =0x60
0x60,R15
R20
= [0x80] = ; [0x60] = R15
55
STS 0x313, R20 ;[0x313] = R20
STS 0x81, R20 ;[0x81] = R20 = [0x80]
$FFFF
Data Address Space
General RAM EEPROM Timers
Purpose
PROGRAM
Registers
ROM

Program CPU Data


Bus Bus address bus
data bus
control bus
Data
8 bit Bus
Data Address
Example: Write a program that adds the contents of the PINC IO
Space R0
R1
R2
Other
PIND andInterrupt
$0000
$0001 General register to the contents of
OSC stores
Unit
the result
Ports in location 0x90
Peripherals
OUT
IN (IN(OUT toIO
from IOlocation)
location)
...
Purpose
of the SRAM
...

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
...

RAM ADD R20,R21 ;R20 = R20 + R21


(SRAM)
OUTOUT
IN SPH,R12
0x3F,R12
R1, ;OUT
0x3F ;R1 ;SREG 0x3E,R12
= R12
= SREG
STS 0x90,R20 ;[0x90] = R20
IN OUTR15,SREG
0x3E,R15 ;IN;SPHR15,0x3F
= R15
IN R17,0x3E ;R17 = SPH
$FFFF
Status Register (SREG)
SREG: I T H S V N Z C
Carry
Interrupt oVerflow Zero
Temporary Negative
Sign
Data Address
Half carry N+V Space
Example:Show
Example:
Example:
Example: Showthe
Show
Show thestatus
the
the statusof
status
status ofthe
of
of theC,
the
the C,H,
C,
C, H,$0000
H,
H, andZZ
and
and
and Zflags
Z flagsafter
flags
flags afterthe
after
after theaddition
the
the addition
subtraction
subtraction
of
of 0x9C of
of 0x9C
0x23
0x73
0x38 and 0x2F from
from
from 0x9C
0xA5
0x52 in
in the
the following
$0001 General
following
0x64 in the following instructions: instructions:
instructions:
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.

Watch Contents of R16 and check if answer is correct.


Example 2-1

Write the program in AVR Studio to verify that

Note: do not forget to add iat the beginning of the program:


.include "M32DEF.inc"
Example 2- 2

Verify using AVR Studio


Example 2- 3
Example 2- 4
Problems (1)
Problems (2)
Problems (3)
Problems (4)

You might also like