You are on page 1of 23

Bab 3: Arsitektur

Mikrokontroler AVR
Mikroprosesor dan Antarmuka
Oleh: Amin Suharjono
Topics
 AVR’s CPU
- Its architecture
- Some simple programs
 Data Memory access
 Program memory
 RISC architecture
RAM EEPROM Timers

PROGRAM
Flash ROM

Program Data
Bus Bus
CPU

Interrupt Other
OSC Ports
Unit Peripherals

I/O
PINS

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


AVR’s CPU
 AVR’s CPU
- ALU
- 32 General Purpose registers R0
(R0 to R31) R1
ALU
- PC register R2
- Instruction decoder


- Status Register SREG: I T H S V N Z C
R15

CPU R16
R17


PC

Instruction decoder R30


R31
Instruction Register
registers

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Some simple instructions
1. Loading values into the general purpose registers
LDI (Load Immediate)
 LDI Rd, k
- Its equivalent in high level languages:
Rd = k
 Example:
- LDI R16,53
- R16 = 53 R0
- LDI R19,132 ALU R1
R2
- LDI R23,0x27


- R23 = 0x27 SREG: I T H S V N Z C

CPU
R15
R16
R17
PC


R30
Instruction decoder
R31
Instruction Register
registers

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Some simple instructions
2. Arithmetic calculation
 There are some instructions for doing Arithmetic and logic operations;
such as:
ADD, SUB, MUL, AND, etc.
 ADD Rd,Rs
R0
- Rd = Rd + Rs R1
ALU
- Example: R2
ADD R25, R9


-
SREG: I T H S V N Z C
- R25 = R25 + R9
CPU
R15
- ADD R17,R30 R16
R17
- R17 = R17 + R30
PC


R30
Instruction decoder
R31
Instruction Register
registers

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


A simple program
 Write a program that calculates 19 + 95

LDI R16, 19 ;R16 = 19


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

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


A simple program
 Write a program that calculates 19 + 95 + 5

LDI R16, 19 ;R16 = 19


LDI R20, 95 ;R20 = 95
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

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Some simple instructions
2. Arithmetic calculation
 SUB Rd,Rs
- Rd = Rd - Rs
 Example:
- SUB R25, R9
- R25 = R25 - R9
- SUB R17,R30
- R17 = R17 - R30
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

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


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,R16        ;Copy contents of R16 to R0

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Some simple instructions
2. Arithmetic calculation
 INC Rd
- Rd = Rd + 1
 Example:
- INC R25
- R25 = R25 + 1

 DEC Rd R0
ALU R1
- Rd = Rd - 1 R2
 Example:


SREG: I T H S V N Z C
- DEC R23
CPU
R15
- R23 = R23 - 1 R16
R17
PC


R30
Instruction decoder
R31
Instruction Register
registers

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Data Address Space
Address Name Address Name Address Name
I/O Mem. I/O Mem. I/O Mem.
$00 $20 TWBR $16 $36 PINB $2B $4B OCR1AH
$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 the contents of;R20
ADD R20, R21
R2 into R20;R21
=0x5E,
as 2 is the address of R2.
R20 +R20
STS
R20 LDS ;SPH = R20
STS
LDS 0x80,
R20, 0x80 ;R20R1,
STS
;[0x80] 0x60
0x60,R15
= =[0x80]
R20 = 55; [0x60] = R15
STS 0x313, R20 ;[0x313] = R20
STS 0x81, R20 ;[0x81] = R20 = [0x80]
$FFFF

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


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

IN R20,PINC ;R20 = PINC


...

Registers
SPH $3E
$005F SREG $3F
$0060 IN R21,PIND ;R21 = PIND
General Example:
Example:
purpose
ADD R20,R21 ;R20 = R20 + R21
...

RAM
(SRAM)
OUTOUT 0x3F,R12
IN SPH,R12
R1, 0x3F ;R1;SREG = R12
= SREG
;OUT 0x3E,R12
STS 0x90,R20 ;[0x90] = R20
IN OUT 0x3E,R15;R17
IN R17,0x3E
R15,SREG ;SPH
;IN = R15
= SPH
R15,0x3F

$FFFF

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


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
R16, R17;subtract
;subtract
;add 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
isnot
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
thereR20
is no a value
carry other
beyond than
the D7 zero
bit. after the subtraction.
HZZ == 01 because
because the
the R20
R20R31is 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 is
is 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

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


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

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Example 2-1

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Example 2- 2

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Example 2- 3

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Example 2- 4

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Problems (1)

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Problems (2)

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Problems (3)

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Problems (4)

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id


Terima Kasih

Amin Suharjono

Mikroprosesor dan Antarmuka – amin.suharjono@polines.ac.id 23

You might also like