You are on page 1of 22

Bab 3 bagian 1: Arsitektur Mikrokontroler AVR

Mikroprosesor dan Antarmuka Oleh: Amin Suharjono

Topics
AVRs CPU
- Its architecture - Some simple programs

Data Memory access Program memory RISC architecture


RAM
PROGRAM Flash ROM

EEPROM

Timers

Program Bus

CPU

Data Bus

OSC

Interrupt Unit

Ports

Other Peripherals

I/O PINS

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

AVRs CPU
AVRs CPU
- ALU - 32 General Purpose registers (R0 to R31) - PC register - Instruction decoder - Status Register (SREG)
R0

ALU
SREG:

R1
R2

T H S V

N Z

CPU
PC
Instruction decoder
Instruction Register

R15 R16 R17

R30 R31
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 LDI R19,132 LDI R23,0x27 - R23 = 0x27

ALU
SREG: I T H S V N Z C

R0 R1 R2

CPU
PC
Instruction decoder
Instruction Register

R15 R16 R17

R30 R31
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
Rd = Rd + Rs Example: ADD R25, R9 - R25 = R25 + R9 ADD R17,R30 - R17 = R17 + R30

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

A simple program
Write a program that calculates 19 + 95
LDI R16, 19 LDI R20, 95 ADD R16, R20 ;R16 = 19 ;R20 = 95 ;R16 = R16 + R20
R0 R1 R2

ALU
SREG: I T H S V N Z C

CPU
PC
Instruction decoder
Instruction Register

R15 R16 R17

R30 R31
registers

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

A simple program
Write a program that calculates 19 + 95 + 5
LDI LDI LDI ADD ADD R16, 19 R20, 95 R21, 5 R16, R20 R16, R21 ;R16 = 19 ;R20 = 95 ;R21 = 5 ;R16 = R16 + R20 ;R16 = R16 + R21

LDI LDI ADD

R16, 19 R20, 95 R16, R20

;R16 = 19 ;R20 = 95 ;R16 = R16 + R20

LDI
ADD

R20, 5
R16, R20

;R20 = 5
;R16 = R16 + R20

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

Some simple instructions


2. Arithmetic calculation
SUB Rd,Rs
-

Rd = Rd - Rs
SUB R25, R9 - R25 = R25 - R9 SUB R17,R30 - R17 = R17 - R30

Example:

ALU
SREG: I T H S V N Z C

R0 R1 R2

CPU
PC
Instruction decoder
Instruction Register

R15 R16 R17

R30 R31
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
INC R25 - R25 = R25 + 1
R0 R1 R2

Example:

DEC Rd
Rd = Rd - 1 DEC R23 - R23 = R23 - 1

ALU
SREG: I T H S V N Z C

Example:

CPU
PC
Instruction decoder
Instruction Register

R15 R16 R17

R30 R31
registers

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

Data Address Space


General Purpose Registers RAM EEPROM Timers

PROGRAM ROM

Program Bus

CPU

Data Bus

8 bit

Data Bus
Interrupt Unit

address bus data bus control bus

Data Address Space


$0000 $0001
...

R0 R1 R2

General Purpose Registers

R31 $00 Add contents TWBR Example: of location 0x90 to contents of location 0x95 Example: Store 0x53 into PINS the SPH register. $01 TWSR Example: does the following instruction do? and store What the result in location 0x313. The address of SPH isdata 0x5E LDS (Load direct from data space) STS (Store direct to space) Example: Write a program that stores 55 into location 0x80 of RAM. Example: Write a program that copies the contents of location 0x80 SPH $3E LDS R20,2 SREG $3F Solution: of RAM into location 0x81. LDS addr ;[addr]=Rd ;Rd = [addr] STS Rd, addr,Rd
...

$001F $0020

...

OSC

Ports

Other Peripherals

I/O Address

I/O

Standard I/O Registers

$005F $0060

...

General purpose RAM (SRAM)

LDS R20, 0x90 Solution: ;R20 = [0x90] Solution: Answer: LDS R20, R21, 55 0x95 [0x95] LDI =;R21 R20,= 0x53 ;R20 = 0x53 Example: LDI ;R20 55 Solution: It copies contents of R2 ;R20 into R20; as is the address of R2. ADD the R20, R21 = R20 +2 R21 STS 0x5E, R20 ;SPH = R20 STS 0x80, R20 ;[0x80] = R20 = 55 LDS R20, 0x80 LDS ;R20 = [0x80] 0x60 STS R1, 0x60,R15 ; [0x60] = R15 STS STS 0x313, R20 0x81, R20 ;[0x313] = R20 ;[0x81] = R20 = [0x80]

$FFFF

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

...

Example 2-1

Solution:

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

Example 2- 2

Solution:

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

Purpose Registers

$001F $0020
TWBR TWSR

...

...

I/O Mem. $00 $20 TWBR $01 $21 TWSR $02 $22 TWAR $03 $23 TWDR $04 $24 ADCL $05 $25 ADCH $06 $26 ADCSRA $07 $27 ADMUX $08 $28 ACSR $09 $29 UBRRL $0A $2A UCSRB $0B $2B UCSRA $0C $2C UDR $0D $2D SPCR $0E $2E SPSR $0F $2F SPDR $10 $30 PIND $11 $31 DDRD Data Address $12 $32 PORTD Space $13 $33 PINC $0000 $34 $14 DDRC $0001 $35 General $15 PORTC

Data Address Space (I/O Register)


Name
I/O $16 $17 $18 $19 $1A $1B $1C $1D $1E $1F $20 $21 $22 $23 $24 $25 8 bit $26 $27 R0 R1 $28 R2 $29 $2A Mem. $36 $37 $38 $39 $3A $3B $3C $3D $3E $3F $40 $41 $42 $43 $44 $45 $46 $47 $48 $49 $4A

Address

Address

Name

Address

Example: Write a program that adds the contents of the PINC IO Other Ports in location 0x90 register to the contents of PIND and stores the result Peripherals OUT (OUT toIO IOlocation) location) IN (IN from of the SRAM R31

PINB DDRB PORTB PINA DDRA PORTA EECR $31 EEDR EEARL $32 PROGRAM EEARH $33 ROM UBRRC $34 UBRRH $35 Program WDTCR $36 Bus ASSR $37 OCR2 $38 TCNT2 $39 TCCR2 $3A ICR1L $3B ICR1H $3C OCR1BL $3D OCR1BH $3E OCR1AL $3E

I/O $2B $2C $2D $2E $2F $30

Mem. $4B $4C $4D $4E $4F $50

Name

OCR1AH TCNT1L TCNT1H TCCR1B TCCR1A SFIOR OCDR RAM $51 General OSCCAL Purpose $52 TCNT0 $53 TCCR0 Registers $54 MCUCSR $55 MCUCR $56 TWCRData $57 SPMCRBus $58 TIFR $59 TIMSK $5A GIFR $5B GICR $5C OCR0 $5D SPL $5E SPH Interrupt OSC $5E SREG Unit

EEPROM

Timers

CPU

Data Bus

address bus data bus control bus

Solution:
$00 $01 $3E $3F

I/O Address

Standard I/O Registers

...

...

$005F $0060

SPH SREG

IN R20,PINC

OUT IOAddr,Rd ;[addr]=Rd IN Rd,IOaddress ;Rd = [addr] Using Names of IO registers


;R20 = PINC

I/O PINS

General purpose RAM (SRAM)

IN R21,PIND Example: ;R21 = PIND Example: ADD R20,R21 ;R20 SPH,R12 = R20 + R21;OUT 0x3E,R12 OUT OUT 0x3F,R12 ;SREG = R12 IN R1, 0x3F ;R1 = SREG STS 0x90,R20 ;[0x90] = R20 IN R15,SREG ;IN R15,0x3F

...

OUT 0x3E,R15;R17 ;SPH = R15 IN R17,0x3E = SPH

$FFFF

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

Status Register (SREG)


SREG: Interrupt Temporary Half carry I T H

C
Zero Carry

oVerflow

Sign N+V

Negative
Data Address Space

Example:Show Showthe thestatus status of ofthe theC, C,H, H, andZ Zflags flagsafter afterthe theaddition Example: Show the status of the C, H, and Z flags after the $0000 Example: and Example: Show the status of the C, H, and Z flags after the addition $0001 General subtraction of 0x9C from 0x9C in the following instructions: subtraction of 0x23 from 0xA5 in the following instructions: 0x73 from 0x52 of 0x64 in the following instructions: of 0x9C 0x38 and 0x2F Purpose LDI 0x38 R20, 0x9C 0x9C LDI R20, 0xA5 0x52 R20, LDI LDI R16, ;R16 = 0x38 Registers

ALU

LDI ADD

...

SREG: I T H S V N Z C Solution:

Solution: Solution: Solution:

Z = 0 because the R20 a value otherthe than zero after the subtraction. C = 0 because there is has no carry beyond D7 Instruction Z= =1 1 decoder because there the R20 R20 is zero after the D3 subtraction. Z = 0 because the has a value other than 0 bit. after the subtraction. H because is a carry from the to the D4 bit.
registers
$FFFF

C= =1 0because becausethere R21 is is not bigger than R20 R20 and there is is no no borrow borrow from from D8 D8 bit. bit. C = 0 because R21 bigger than and there C isnot a carry beyond the D7 bit. R30 H = 1 because there isR31 a borrow from D4 toto D3. carry from the D3 the D4 bit. H = 0 because there is no borrow from D4 to D3. H = 0 because there is no borrow from D4 to D3. Z = 1 because the R20 (the result) has a value in it after the addition. Instruction Register Z = 0 because the R16 (the result) has a value 0 other than 0 after the addition.

$0060 $52 General $9C $A5 $38 $9C purpose $73 --+$64 $9C 10010100 1100 $23 0010 0011 $2F 1111 + 0110 RAM $DF 1101 1111 R20 = $DF PC $00 0000 0000 R20 = $00 $82 1000 0010 R20 = 00 $82 (SRAM) $67 0110 0111 R16 0x67 $100 1 0000 0000 R20 = C = 1 because R21 is bigger than R20 and there is a borrow from D8 bit.
...

CPU

R15 1 1 0101 0010 R16 1001 1100 1100 1010 0101 0011 1000 1001 R17 0011 0111

$005F

SPH SREG

...

R0 $001F R1 0x64 LDI 0x2F R21, 0x9C LDI R21, 0x23 0x73 LDI R21, R17, ;R17 = $0020 0x2F R2 Standard IO

...

IO Address
$00 $01

TWBR TWSR

SUB R17 R20, R21 R21 ;subtract R21 from R20 R20 SUB R20, R21 ;subtract R21 from ADD R20, ;add R21 to R20 R16, ;add R17 to Registers R16
$3E $3F

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

22

You might also like