Week+02+-+Chap+1+Introduction+and+Chap+2+AVR+Architecture.ppt

Share Embed Donate


Short Description

Download Week+02+-+Chap+1+Introduction+and+Chap+2+AVR+Architecture.ppt...

Description

www.MicroDigital Ed.com BIHEuniversity

Introduction to AVR Chapter 1

The AVR AVR microcontro m icrocontroller ller and embedded systems using assembly and c

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Topics www.MicroDigital Ed.com BIHEuniversity

• Microcontrollers vs. Microprocessors • Most common microcontrollers •  AVR Features •  AVR members

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Topics www.MicroDigital Ed.com BIHEuniversity

• Microcontrollers vs. Microprocessors • Most common microcontrollers •  AVR Features •  AVR members

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

General Purpose Microprocessors vs. Microcontrollers www.MicroDigital Ed.com BIHEuniversity

• General Purpose Microprocessors Data BUS

General Purpose Micro processor

RAM

ROM

Timer

Serial COM Port

IO Port

Address BUS Control BUS

• Microcontrollers CPU

RAM

Serial Timer Port  AVR Microcontroller and Embedd ed System Using Assembly and C

ROM

I/O

© 2011 Pearson Higher Education,

Most common microcontrollers www.MicroDigital Ed.com BIHEuniversity

• 8-bit microcontrollers  – AVR  – PIC  – HCS12  – 8051

• 32-bit microcontrollers  – ARM  – PIC32

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

 AVR internal architecture www.MicroDigital Ed.com BIHEuniversity

40 PIN DIP

PROGRAM ROM

Program Bus

(XCK/T0) PB0

1

(T1) PB1

2

40

PA0 (ADC0)

39

(INT2/AIN0) PB2

3

PA1 (ADC1)

38

PA2 (ADC2)

(OC0/AIN1) PB3

4

(SS) PB4

5

37

PA3 (ADC3)

36

(MOSI) PB5

PA4 (ADC4)

6

35

PA5 (ADC5)

(MISO) PB6

7

34

PA6 (ADC6)

(SCK) PB7

8

33

PA7 (ADC7)

RESET

9

32

 AREF

CPU

RAM MEGA32

Bus 10

VCC

EEPROM

31

 AGND

GND

11

30

 AVCC

XTAL2

12

29

PC7 (TOSC2)

XTAL1

13

28

PC6 (TOSC1)

(RXD) PD0

14

27

PC5 (TDI)

(TXD) PD1

15

26

PC4 (TDO)

(INT0) PD2

16

25

PC3 (TMS)

(INT1) PD3

17

24

PC2 (TCK)

(OC1B) PD4

18

23

PC1 (SDA)

22

PC0 (SCL)

21

PD7 (OC2)

(OC1A) PD5 OSC (ICP) PD6

Interrupt 19 Unit 20

Ports

Timers

Other  Peripherals

I/O PINS  AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

 AVR different groups www.MicroDigital Ed.com BIHEuniversity

• Classic AVR  – e.g. AT90S2313, AT90S4433

• Mega  – e.g. ATmega8, ATmega32, ATmega128

• Tiny  – e.g. ATtiny13, ATtiny25

• Special Purpose AVR  – e.g. AT90PWM216,AT90USB1287

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Let’s get familiar with the AVR part numbers www.MicroDigital Ed.com BIHEuniversity

 ATmega128  Atmel

 ATtiny44  Atmel Tiny group

Flash =4K

 AVR Microcontroller and Embedd ed System Using Assembly and C

group

Flash =128K

 AT90S4433  Atmel

Classic group

Flash =4K

© 2011 Pearson Higher Education,

 AVR Architecture (pp 55 - 75) Chapter 2

The AVR microcontroller and embedded systems using assembly and c

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Installing Programming tools for ATMEL devices. 



Refer to AVRstudioTutorial.pdf in folder \Lecture Notes\E-Books and 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 Ebook in folder \Lecture Notes\E-Books and Reference for AVR Instruction Explained.

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Topics 

 AVR’s CPU 









Its architecture Some simple programs

Data Memory access Program memory RISC architecture

RAM

EEPROM

Interrupt Unit

Ports

Timers

PROGRAM Flash ROM

Program Bus

CPU

OSC

Data Bus

Other  Peripherals

I/O PINS

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

 AVR’s CPU 

 AVR’s CPU 







 ALU 32 General Purpose registers (R0 to R31) PC register Instruction decoder

R0 R1

ALU

R2



SREG:

I

T

H

S V

N Z

C

CPU PC Instruction decoder

R15 R16 R17



R30 R31

Instruction Register

 AVR Microcontroller and Embedd ed System Using Assembly and C

registers © 2011 Pearson Higher Education,

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

R0 R1 R2

ALU



SREG: I T H S V N Z C

CPU PC Instruction decoder Instruction Register

 AVR Microcontroller and Embedd ed System Using Assembly and C

R15 R16 R17



R30 R31 registers

© 2011 Pearson Higher Education,

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

R0 R1 R2

ALU



SREG: I T H S V N Z C

CPU PC Instruction decoder Instruction Register

 AVR Microcontroller and Embedd ed System Using Assembly and C

R15 R16 R17



R30 R31 registers

© 2011 Pearson Higher Education,

 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 R1 R2

ALU



SREG: I T H S V N Z C

CPU PC Instruction decoder Instruction Register

 AVR Microcontroller and Embedd ed System Using Assembly and C

R15 R16 R17



R30 R31 registers

© 2011 Pearson Higher Education,

 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

R16, R20

;R16 = R16 + R20

R20, 5

;R20 = 5

R16, R20

;R16 = R16 + R20

 ADD LDI  ADD

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

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 R1 R2

ALU



SREG: I T H S V N Z C

CPU PC Instruction decoder Instruction Register

 AVR Microcontroller and Embedd ed System Using Assembly and C

R15 R16 R17



R30 R31 registers

© 2011 Pearson Higher Education,

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 MOV R0,A

;Set R16 to value of 10 ;Copy contents of R16 to R0

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Some simple instructions 2. Arithmetic calculation 

INC Rd 



Rd = Rd + 1

Example: 

INC R25 

R25 = R25 + 1

R0 R1 R2

ALU 

DEC Rd 



Rd = Rd - 1

Example: 

DEC R23 

R23 = R23 - 1

 AVR Microcontroller and Embedd ed System Using Assembly and C



SREG: I T H S V N Z C

CPU PC Instruction decoder Instruction Register

R15 R16 R17



R30 R31 registers

© 2011 Pearson Higher Education,

Address I/O

$00 $01 $02 $03 $04 $05 $06 $07 $08 $09 $0A $0B $0C $0D $0E $0F $10 $11 Data Address $12 Space $13 $14 $0000 $0001 $15      .      .      .

Name

Mem.

$20 TWBR $21 TWSR $22 TWAR $23 TWDR $24 ADCL $25 ADCH $26 ADCSRA $27 ADMUX $28 ACSR $29 UBRRL $2A UCSRB $2B UCSRA $2C UDR $2D SPCR $2E SPSR $2F SPDR $30 PIND $31 DDRD $32 PORTD $33 PINC $34 DDRC $35 PORTC General

Purpose Registers

$001F

     .      .

Standard I/O Registers

$005F $0060      .      .      .

$16 $17 $18 $19 $1A $1B $1C $1D $1E $1F

Address

Name

Mem.

$36 $37 $38 $39 $3A $3B $3C $3D $3E $3F

$20

$40

$21 $41 $22 $42 $23 $43 $24 $44 $25 $45 8 bit $46 $26 $27R0 $47 $28R1 $48 $29R2 $49 $2A $4A      .      .      .

PINB DDRB PORTB PINA DDRA PORTA EECR EEDR EEARL PROGRAM EEARH

I/O

Mem.

$2B $2C $2D $2E $2F $30

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

$31

$32 $33 UBRRCROM $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 $56CPU TWCRData $57 SPMCRBus $58 TIFR $59 TIMSK Data $5A GIFR Bus $5B GICR $5C OCR0 $5D SPL $5E SPH Interrupt OSC $5E SREG Unit

General purpose RAM (SRAM)

EEPROM

Timers

addressbus databus controlbus

Other  Peripherals

Ports

R31

I/O

I/O Address

$0020      .

Data Address Space

Address I/O

E xTWBR a m p l e$00 : A d d c o n t e n t sExamp o f l o c le: a t ioStore n 0 x 90x53 0 t o cinto o n t ethe n t s SPH o f l o cregister. ation 0x95 PINS $01 TWSR E x a . . . m ple h at d o lt e sinthThe foSladdress l(L o(Store w ndg di in r tu fctrto io daot?aspace) and sto re: W the resu location odirec f tcSPH isnd0x5E Le D ot ia0x313. rsse m data pna 0 ce ) 0 of RAM. STS to E x a m p l e : W r i t e a p r o g r a m t h a s t o r e 5 5 i n t o l o cnat st iso xc8 Exa m p l e : W r i t e a p r o g r a m t h a t c o p i e s t h e c o n t e o f l o ation 0x80 SPH $3E LDS Solution: o f SREG R A M $3F i nR20,2 t o l o c a t i o n 0LDS x 8 1 . Rd, addr ;Rd = [addr]

STS addr,Rd 0x90 Solution: ;R20

;[addr]=Rd

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

0x313, R20 0x81, R20

;[0x313] = R20 ;[0x81] = R20 = [0x80]

$FFFF

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Data Address Space

PROGRAM ROM

Program Bus

General Purpose Registers

CPU

RAM

EEPROM

Data Bus

addressbus databus controlbus

Data Bus

8 bit

Data Address Space $0000 $0001      .      .      .

R0

E x a m p l e : W r i t e a p r o g r a m t h a t a d d s t h e c o n t e n t s o f t h e P I NC IO r e g i s t e r t o t h e c o n t e n t s oOSC f P IN D a n dInterrupt sUnit t o r e s t h e r ePorts s u l t i n l o c Peripherals a t Other  io n 0 x 9 0 OUT (OUT IOlocation) location ) IN (IN fromtoIO of the SRAM  R1 R2

General Purpose Registers

     .      .      .

R31

$001F

     .      .

Standard I/O Registers

$0060      .      .

General purpose RAM (SRAM)

$00 $01

 . . . SPH SREG

$005F

     .

I/O PINS

I/O Address

Solution:

TWBR TWSR

$0020      .

Timers

$3E $3F

IN R20,PINC IN

R21,PIND

 ADD R20,R21 STS 0x90,R20

OUT IN Rd,IOaddress U sIOAddr,Rd i n g N a m e s o f;Rd I;[addr]=Rd O r=eg[addr] isters ;R20 = PINC ;R21 = PIND Example: Example: ;R20 = R20 + R21 OUT ;OUT 0x3E,R12 OUT 0x3F,R12 ;SREG = R12 IN SPH,R12 R1, 0x3F ;R1 = SREG ;[0x90] = R20 IN R15,SREG

;IN

R15,0x3F

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

$FFFF

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Status Register (SREG) SREG:

I

T

H

S

Interrupt Temporary Half carry

N

V

Z

oVerflow Sign N+V

C Zero

Carry

Negative Data Address Space

Example: le: Show Show th status of thee C, C,H, H,$0000 and ZZ flags after after the the add Examp le: Show ttth he status o the C, H, and flags after the Examp ee status and itionn Examp le: Show he status oofff th the C, H, and ZZ flags flags after the additio $0001 General ond n o0 ofxf 6 0x m 952C ign tithnhesetfro fuolcllo i nsst tr ruucct ti o i onnss: : b0bxtxtrr93aaC iion 3C olo 7i n 3 ftffrhrrooeom m os 4 t iw owinni n : in osfu fu0 8ccttaan 20x F29 f o00l0lxxoxA5w i ni n sgg Purpose      .      .

LDI R20, LDI R20, LDI R20, LDI R16, 0x38 R0 R1 LDI R21, LDI R21, R21, ALU LDI LDI R17, 0x2F R2 … SUB R20, SUB R20, R20,  ADD  ADD R16, R17 SREG: I T H S V N Z C Solution:

Soo S S oolllluu uuttttiiiioo oonn nn:::: S

CPU

     .

0x9C 0xA5 0x52 0x9C ;R16 = 0x38 Registers $001F

TWBR 0x9C 0x23 0x73 0x64 ;R17 = $0020 0x2F TWSR Standard IO  . . R21 ;subtract R21 from R20 R20 R21 ;subtract R21 from  . R21 ;add R21 to R20 ;add R17 to Registers R16

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

$52 $9C $A 5 $38 $9C - $23 $73 $9C 1001 1100 0010 0011 … 0100 $2F 1111 +-- +$64 0110 $DF 1101 1111 PC $00 0000 0111 0000 $82 1000 0010 $67R21 1is0000 0110 $100 C = 1 because bigger0000 than R20

IO Address $00 $01

     .      .      .

$005F

SPH SREG

$3E $3F

$0060

General purpose R20 ==RAM $DF R20 $00 R20 = $82 (SRAM) R16 0x 67 R20 = 00 and there is a bor row from D8 bit. C === 100because because there R21 is isisnot not igger than R20 R20 andbit. there is is no no bor borrow row from from D8 D8 bit. bit. C because R21 bbigger than and there C ahas carry b eyond thethan D7 R30 Z == 00 decoder because the R20 a value other zero after the sub traction. C because there is no carry b eyond the D7 bit. Instruction because the the R20 R20ahas is zero after theerD3 sub traction. ZZ == 01 because a value than 0 after the sub traction . H fro m oth the the D4 bit. R31 H == 11 because because there there is a carry bor row from D4 toto D3. is carry fro m th e D3 to the D4 bit. H===100because ausseethe is(the w fhas frroom maD D D033in bbeeccau tthheeR20 rree is nnoo bbresult) oorrrroow 44 ttoo D .. it after the additio n. ZH value Instruction Z = 0 Register because the R16 (the result) has a value oth er than 0 after th e addition . registers      .      .      .

$FFFF  AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

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

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

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.

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

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"  AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Example 2- 2

 Verify using AVR Studio

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Example 2- 3

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Example 2- 4

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Problems (1)

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Problems (2)

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

Problems (3)

 AVR Microcontroller and Embedd ed System Using Assembly and C

© 2011 Pearson Higher Education,

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF