Professional Documents
Culture Documents
Topics
AVRs CPU
- Its architecture - Some simple programs
EEPROM
Timers
Program Bus
CPU
Data Bus
OSC
Interrupt Unit
Ports
Other Peripherals
I/O PINS
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
R30 R31
registers
Example:
-
ALU
SREG: I T H S V N Z C
R0 R1 R2
CPU
PC
Instruction decoder
Instruction Register
R30 R31
registers
ADD Rd,Rs
Rd = Rd + Rs Example: ADD R25, R9 - R25 = R25 + R9 ADD R17,R30 - R17 = R17 + R30
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
R30 R31
registers
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
ADD
R20, 5
R16, R20
;R20 = 5
;R16 = R16 + R20
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
R30 R31
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,R16 ;Copy contents of R16 to R0
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
R30 R31
registers
PROGRAM ROM
Program Bus
CPU
Data Bus
8 bit
Data Bus
Interrupt Unit
R0 R1 R2
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
$005F $0060
...
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
...
Example 2-1
Solution:
Example 2- 2
Solution:
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
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
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
Solution:
$00 $01 $3E $3F
I/O Address
...
...
$005F $0060
SPH SREG
IN R20,PINC
I/O PINS
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
...
$FFFF
Example 2- 3
Example 2- 4
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:
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
Problems (1)
Problems (2)
Problems (3)
Problems (4)
Terima Kasih
Amin Suharjono
22