03/28/1436
University of Kashan Faculty of Electrical and Computer Engineering Department of Computer Engineering
Lecture note 7
AVR Advanced Assembly Language Programming Different Addressing Modes Hossein Sabaghian-Bidgoli
[email protected] Fall 2014
Assembler Directives .INCLUDE,
.ORG, .EQU, .SET, .DEF Arithmetic Expression (+-*/%)
Logic
Expression (&|^~)
Shift
Operators ()
2
1
Microprocessors
University of Kashan
03/28/1436
Assembler Directives An
Application of Shift Operators ()
3
Microprocessors
University of Kashan
Assembler Directives HIGH()
4
2
and LOW() Functions
Microprocessors
University of Kashan
03/28/1436
Addressing Modes
Data
Immediate Register Memory
Data memory
Program memory
EEPROM
Indirect ((Auto inc)
IO
Direct Register Indirect (Auto inc, Auto Dec, With displacement)
Direct Bit
Program (label)
Absolute (long) Relative(Short) Indirect
5
Microprocessors
University of Kashan
Addressing Modes Single Register Addressing Immediate
6
3
Addressing
Microprocessors
University of Kashan
03/28/1436
Addressing Modes Two
Register Addressing
7
Microprocessors
University of Kashan
Addressing Modes Data
Direct Addressing
Examples: STS 0x1000,R16
8
4
Microprocessors
University of Kashan
03/28/1436
Addressing Modes IO Direct
5
Addressing
9
Microprocessors
University of Kashan
10
Microprocessors
University of Kashan
03/28/1436
Register Indirect
Examples: LD R16, Y ST Z, R16
11
Microprocessors
University of Kashan
Microprocessors
University of Kashan
Register Indirect
12
6
03/28/1436
13
Microprocessors
University of Kashan
Addressing Modes Register Indirect with
Pre-Decrement
Examples: LD R16, -Z ST -Z, R16 14
7
Microprocessors
University of Kashan
03/28/1436
Addressing Modes Register Indirect with
Post-Increment
Examples: LD R16, Z+ ST Z+, R16 15
Microprocessors
University of Kashan
Addressing Modes Register Indirect With
Displacement
Examples: LDD R16, Y+0x10 STD Z+0x20, R16 16
8
Microprocessors
University of Kashan
03/28/1436
Addressing Modes Register Indirect
17
Microprocessors
University of Kashan
Look up Table .DB
18
9
Define Bytes in program memory Program memory is word addressable Assembler align to even address
Microprocessors
University of Kashan
03/28/1436
Look up Table .DW
Define Words in program memory
19
Microprocessors
University of Kashan
Addressing Modes Program
Memory Addressing
Examples: LPM Rn, Z
20
10
Microprocessors
University of Kashan
03/28/1436
Addressing Modes Program
Memory Addressing with Post Increment
Examples: LPM Rn, Z+
21
Microprocessors
University of Kashan
Addressing Modes Indirect Program
Addressing
Examples: IJMP ICALL
22
11
Microprocessors
University of Kashan
03/28/1436
Addressing Modes Relative Program
Addressing
Examples: RJMP RCALL 23
Microprocessors
University of Kashan
Look up Table The value of Z
24
12
Microprocessors
University of Kashan
03/28/1436
Look up Table The value of
25
Z
Microprocessors
University of Kashan
Read a string from flash and send to output PortB
26
13
Microprocessors
University of Kashan
03/28/1436
Example 6-17 Read a string from flash memory char by char , Write them to SRAM
27
Microprocessors
University of Kashan
Example 6-18 Get a 3-bit number from PortC , Put corresponding Ascii code to PortD
28
14
Microprocessors
University of Kashan
03/28/1436
Example 6-19 Get a 4-bit number x from PortB , catch x2 from a look-up table, put to PortD
29
Microprocessors
University of Kashan
Example 6-20 Get a 4-bit number x from PortB , put x2+2x+3 to PortC (Use a look-up table)
30
15
Microprocessors
University of Kashan
03/28/1436
LED on the Output Ports Vcc=+5V
Correct cases
Case1: LED is ON if output=zero Case2: LED is ON if output=One Most LEDs drop 1.7 volts and need about 10ma Current is (5-1.7)/470
Case1
OK
Output Pin
Case2
Incorrect cases
Too much current Failure of Port or LED
Output Pin
Output Pin
Case4
Case5: LED is ON if output=1
Vcc=+5V
Case3
Case3 and case4:
Output Pin
470
Appropriate for open collector output (NPN) Else (if no open collector) failure of Port or LED when 1 High power consumption Case5
Vcc=+5V
Case6: LED is ON if output=0
Appropriate for open collector output (PNP) Else (if no open collector) failure of Port or LED when 0 High power consumption
31
Microprocessors
470
Output Pin
Vcc=+5V
Case6 Output Pin 470
University of Kashan
The 7-segment Display (Cont.) 7-segment displays come
Common Anode Connect cathodes to the output
16
in 2 configurations:
Common Cathode Connect Anode to the output
03/28/1436
BCD to 7_Seg lookup table a f
b g
e
BCD
pgfedcba 7_seg
he x
0000
001111 11
3f
0001
00110000
30
0010
0101101 1
5b
0011
010011 11
4f
0100
011001 10
66
b
0101
01101101
6d
c
0110
01111101
7d
0111
00000111
07
1000
01111111
7f
1001
01101111
6f
c d
a
a f e
f
d a
b
f
c
e
g
d
g
e
c d
tab_7sg: .db .db
f
b
g
c
d
a
g
b c
d f
c
b
e a
g
a
a b
f
c
e
g
a b
f
g
c d
d
0x3f,0x30,0x5b,0x4f,0x66, 0x6d 0x7d,0x07,0x7f,0x6f
33
Microprocessors
University of Kashan
Example: Read Data from SRAM .include "m16def.inc" .org 0 LDI R17, High(RAMEND) OUT SPH, R17 LDI R17, LOW(RAMEND) OUT SPL, R17
;--->
LDI STS LDI STS LDI STS LDI STS
UP:
34
17
R17, 0x60, R17, 0x61, R17, 0x62, R17, 0x63,
0b00000110 R17 0b01001111 R17 0b11101111 R17 0b01001111 R17 ;--->
LDI OUT
R17, 0xff DDRC, R17
LDI LDI LDI
YL, YH, R16,
LD OUT CALL DEC BRNE jmp
R17, Y+ PORTC,R17 DELAY R16 UP REPEAT
REPEAT:
Microprocessors
0x60 0x00 4
University of Kashan
03/28/1436
Example: Read Data from SRAM .include "m16def.inc" .org 0 LDI R17, High(RAMEND) OUT SPH, R17 LDI R17, LOW(RAMEND) OUT SPL, R17
;--->
LDI STS LDI STS LDI STS LDI STS
UP:
R17, 0x60, R17, 0x61, R17, 0x62, R17, 0x63,
0b00000110 R17 0b01001111 R17 0b11101111 R17 0b01001111 R17 ;--->
35
LDI OUT
R17, 0xff DDRC, R17
LDI LDI LDI
YL, YH, R16,
LD OUT CALL DEC BRNE jmp
R17, Y+ PORTC,R17 DELAY R16 UP REPEAT
REPEAT:
Microprocessors
0x60 0x00 4
University of Kashan
Example: Read Data from Code Memory .include "m16def.inc" .org 0 LDI R17, High(RAMEND) OUT SPH, R17 LDI R17, LOW(RAMEND) OUT SPL, R17 LDI STS LDI STS LDI STS LDI STS
36
18
R17, 0x60, R17, 0x61, R17, 0x62, R17, 0x63,
0b00000110 R17 0b01001111 R17 0b11101111 R17 0b01001111 R17 ;--->
;--->
LDI OUT
R17, 0xff DDRC, R17
LDI LDI LDI
ZL, ZH, R16,
REPEAT: LOW(Data1