Lecture note 7 AVR Advanced Assembly Language Programming Different Addressing Modes · 2015. 2....
Transcript of Lecture note 7 AVR Advanced Assembly Language Programming Different Addressing Modes · 2015. 2....
03/28/1436
1
AVR Advanced Assembly Language Programming Different Addressing Modes
University of Kashan
Faculty of Electrical and Computer Engineering
Department of Computer Engineering
Hossein Sabaghian-Bidgoli
Fall 2014
Lecture note 7
Assembler Directives
University of Kashan Microprocessors 2
.INCLUDE, .ORG, .EQU, .SET, .DEF
Arithmetic Expression (+-*/%)
Logic Expression (&|^~)
Shift Operators (<<, >>)
03/28/1436
2
An Application of Shift Operators (<<, >>)
Assembler Directives
University of Kashan Microprocessors 3
HIGH() and LOW() Functions
Assembler Directives
University of Kashan Microprocessors 4
03/28/1436
3
Addressing Modes
University of Kashan Microprocessors 5
Data Immediate Register Memory
Data memory Direct Register Indirect (Auto inc, Auto Dec, With displacement)
Program memory Indirect ((Auto inc)
EEPROM
IO Direct
Bit
Program (label) Absolute (long) Relative(Short) Indirect
Addressing Modes
University of Kashan Microprocessors 6
Single Register Addressing
Immediate Addressing
03/28/1436
4
Addressing Modes
University of Kashan Microprocessors 7
Two Register Addressing
Addressing Modes
University of Kashan Microprocessors 8
Data Direct Addressing
Examples: STS 0x1000,R16
03/28/1436
5
Addressing Modes
University of Kashan Microprocessors 9
IO Direct Addressing
University of Kashan Microprocessors 10
03/28/1436
6
University of Kashan Microprocessors 11
Register Indirect
Examples: LD R16, Y
ST Z, R16
University of Kashan Microprocessors 12
Register Indirect
03/28/1436
7
University of Kashan Microprocessors 13
Addressing Modes
University of Kashan Microprocessors 14
Register Indirect with Pre-Decrement
Examples: LD R16, -Z
ST -Z, R16
03/28/1436
8
Addressing Modes
University of Kashan Microprocessors 15
Register Indirect with Post-Increment
Examples: LD R16, Z+
ST Z+, R16
Addressing Modes
University of Kashan Microprocessors 16
Register Indirect With Displacement
Examples: LDD R16, Y+0x10
STD Z+0x20, R16
03/28/1436
9
Addressing Modes
University of Kashan Microprocessors 17
Register Indirect
Look up Table
University of Kashan Microprocessors 18
.DB Define Bytes in program memory
Program memory is word addressable
Assembler align to even address
03/28/1436
10
Look up Table
University of Kashan Microprocessors 19
.DW
Define Words in program memory
Addressing Modes
University of Kashan Microprocessors 20
Program Memory Addressing
Examples: LPM Rn, Z
03/28/1436
11
Addressing Modes
University of Kashan Microprocessors 21
Program Memory Addressing with Post Increment
Examples: LPM Rn, Z+
Addressing Modes
University of Kashan Microprocessors 22
Indirect Program Addressing
Examples: IJMP
ICALL
03/28/1436
12
Addressing Modes
University of Kashan Microprocessors 23
Relative Program Addressing
Examples: RJMP
RCALL
Look up Table
University of Kashan Microprocessors 24
The value of Z
03/28/1436
13
Look up Table
University of Kashan Microprocessors 25
The value of Z
University of Kashan Microprocessors 26
Read a string from flash and send to output PortB
03/28/1436
14
Example 6-17
University of Kashan Microprocessors 27
Read a string from flash memory char by char , Write them to SRAM
Example 6-18
University of Kashan Microprocessors 28
Get a 3-bit number from PortC , Put corresponding Ascii code to PortD
03/28/1436
15
Example 6-19
University of Kashan Microprocessors 29
Get a 4-bit number x from PortB , catch x2 from a look-up table, put to PortD
Example 6-20
University of Kashan Microprocessors 30
Get a 4-bit number x from PortB , put x2+2x+3 to PortC (Use a look-up table)
03/28/1436
16
LED on the Output Ports
University of Kashan Microprocessors 31
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
Incorrect cases Case3 and case4:
Too much current
Failure of Port or LED
Case5: LED is ON if output=1 Appropriate for open collector output (NPN)
Else (if no open collector) failure of Port or LED when 1
High power consumption
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
Output Pin
Vcc=+5V
470
Output Pin
Vcc=+5V
470
Output Pin
Vcc=+5V Output Pin
Output Pin
Vcc=+5V
470
Output Pin
Case2
Case1
Case4
Case3
Case5 Case6
OK
The 7-segment Display (Cont.)
7-segment displays come in 2 configurations:
Common Anode Common Cathode
Connect cathodes to the output Connect Anode to the output
03/28/1436
17
BCD to 7_Seg lookup table
University of Kashan Microprocessors 33
BCD p g f e d c b a
7_seg
hex
0000 0 0 1 1 1 1 1 1 3f
0001 0 0 1 1 0 0 0 0 30
0010 0 1 0 1 1 0 1 1 5b
0011 0 1 0 0 1 1 1 1 4f
0100 0 1 1 0 0 1 1 0 66
0101 0 1 1 0 1 1 0 1 6d
0110 0 1 1 1 1 1 0 1 7d
0111 0 0 0 0 0 1 1 1 07
1000 0 1 1 1 1 1 1 1 7f
1001 0 1 1 0 1 1 1 1 6f
e
f
a
b
c
f
d
f
e
a
b
e
g
d
a
b
c
g
d
b
c
f g
a
c
g
d
a
c
f
e
g
d
a
b
c
a
b
c
f
e
g
d
a
b
c
f g
d
a
b
c
f
e
g
d
tab_7sg: .db 0x3f,0x30,0x5b,0x4f,0x66, 0x6d
.db 0x7d,0x07,0x7f,0x6f
Example: Read Data from SRAM
University of Kashan Microprocessors 34
.include "m16def.inc"
.org 0
LDI R17, High(RAMEND)
OUT SPH, R17
LDI R17, LOW(RAMEND)
OUT SPL, R17
LDI R17, 0b00000110
STS 0x60, R17
LDI R17, 0b01001111
STS 0x61, R17
LDI R17, 0b11101111
STS 0x62, R17
LDI R17, 0b01001111
STS 0x63, R17 ;--->
;--->
LDI R17, 0xff
OUT DDRC, R17
REPEAT:
LDI YL, 0x60
LDI YH, 0x00
LDI R16, 4
UP:
LD R17, Y+
OUT PORTC,R17
CALL DELAY
DEC R16
BRNE UP
jmp REPEAT
03/28/1436
18
Example: Read Data from SRAM
University of Kashan Microprocessors 35
.include "m16def.inc"
.org 0
LDI R17, High(RAMEND)
OUT SPH, R17
LDI R17, LOW(RAMEND)
OUT SPL, R17
LDI R17, 0b00000110
STS 0x60, R17
LDI R17, 0b01001111
STS 0x61, R17
LDI R17, 0b11101111
STS 0x62, R17
LDI R17, 0b01001111
STS 0x63, R17 ;--->
;--->
LDI R17, 0xff
OUT DDRC, R17
REPEAT:
LDI YL, 0x60
LDI YH, 0x00
LDI R16, 4
UP:
LD R17, Y+
OUT PORTC,R17
CALL DELAY
DEC R16
BRNE UP
jmp REPEAT
Example: Read Data from Code Memory
University of Kashan Microprocessors 36
.include "m16def.inc"
.org 0
LDI R17, High(RAMEND)
OUT SPH, R17
LDI R17, LOW(RAMEND)
OUT SPL, R17
LDI R17, 0b00000110
STS 0x60, R17
LDI R17, 0b01001111
STS 0x61, R17
LDI R17, 0b11101111
STS 0x62, R17
LDI R17, 0b01001111
STS 0x63, R17 ;--->
;---> LDI R17, 0xff
OUT DDRC, R17
REPEAT:
LDI ZL, LOW(Data1<<1)
LDI ZH, HIGH(Data1<<1)
LDI R16, 4
UP:
LPM R17, Z+
OUT PORTC,R17
CALL DELAY
DEC R16
BRNE UP
jmp REPEAT
.ORG 0x050
Data1: .DB 0b00000110,0b01001111
.DB 0b01101111,0b01001111
03/28/1436
19
Example: Read Data from Code Memory
University of Kashan Microprocessors 37
.include "m16def.inc"
.org 0
LDI R17, High(RAMEND)
OUT SPH, R17
LDI R17, LOW(RAMEND)
OUT SPL, R17
LDI R17, 0b00000110
STS 0x60, R17
LDI R17, 0b01001111
STS 0x61, R17
LDI R17, 0b11101111
STS 0x62, R17
LDI R17, 0b01001111
STS 0x63, R17 ;--->
;---> LDI R17, 0xff
OUT DDRC, R17
REPEAT:
LDI ZL, LOW(Data1<<1)
LDI ZH, HIGH(Data1<<1)
LDI R16, 4
UP:
LPM R17, Z+
OUT PORTC,R17
CALL DELAY
DEC R16
BRNE UP
jmp REPEAT
.ORG 0x050
Data1: .DB 0x06,0x4f,0x6f,0x4f
Example: Look-up Table
University of Kashan Microprocessors 38
.include "m16def.inc"
.org 0
LDI R17, High(RAMEND)
OUT SPH, R17
LDI R17, LOW(RAMEND)
OUT SPL, R17
LDI R17, 1
STS 0x60, R17
LDI R17, 3
STS 0x61, R17
LDI R17, 9
STS 0x62, R17
LDI R17, 3
STS 0x63, R17 ;--->
;--->
LDI R17, 0xff
OUT DDRC, R17
REPEAT:
LDI YL, 0x60
LDI YH, 0x00
LDI ZH, HIGH(Data1<<1)
LDI R16, 4
UP:
LDI ZL, LOW(Data1<<1)
LD R17, Y+
ADD ZL, R17
LPM R17, Z
OUT PORTC,R17
CALL DELAY
DEC R16
BRNE UP
jmp REPEAT
.ORG 0x050
Data1: .DB 0,0x06,0,0x4f,0,0,
.DB 0,0,0,0x6f,0x4f
03/28/1436
20
Multiplexing of 7_seg
University of Kashan Microprocessors 39
LED Matrix (8×8)
University of Kashan Microprocessors 40
03/28/1436
21
Bit Addressing Modes
University of Kashan Microprocessors 41
How to manipulate bits of GPRs? Using a Mask Selective set
ORI Rd, K ;RdRd or K (K is an 8 bit immediate) SBR Rd, K ;RdRd or K Different name for just one instruction
Selective clear ANI Rd, K ;RdRd and K (K is an 8 bit immediate) CBR Rd, K ;RdRd and K Different name for just one instruction
Examples
LDI R16, 0b01011001 SBR R16, 0b01100100 ;R16=0b01111101 CBR R16, 0b11110000 ;R16=0b01110000
Bit Addressing Modes
University of Kashan Microprocessors 42
How to manipulate a bit of I/O Registers? Use Bit Addressing Set a bit
SBI A, b ;Set bit b in I/O register with address A SBI PortC, 2 ;PortC_Bit21
Clear a bit CBI A, b ;Clear bit b in I/O register with address A CBI PortC, 2 ;PortC_Bit20
How to copy a bit from a GPR to another GPR? Use Bit Addressing Store a bit
BST Rr, b ;Store bit b of Rr in T flag of SREG
Load a bit BLD Rd, b ;Load bit from T to bit b of Rd
Example BST R17, 3 BLD R19, 5 ;Copy R17_3 to R19_5
03/28/1436
22
Bit Addressing Modes
University of Kashan Microprocessors 43
LDS R16, MYREG
4
Bit Addressing Modes
University of Kashan Microprocessors 44
Bit Addressing always is direct adressing
Bit addressing instructions
03/28/1436
23
Bit Addressing Modes
University of Kashan Microprocessors 45
How to check a bit? Use Skip with bit addressing Check a bit of IO register
SBIS A, b SBIS PortC, 2 ;If (PortC_Bit2==1) Skip next instruction
SBIC A, b SBIC PortB, 5 ;If (PortB_Bit5==0) Skip next instruction
Check a bit of GPR SBRS Rn, b
SBRS R20, 3 ;If (R20_Bit3==1) Skip next instruction SBRC Rn, b
SBRC R3, 7 ;If (R3_Bit7==0) Skip next instruction
Use Branch with bit addressing Check a bit of SREG
BRBS s, k BRBS 0, LBL1 ;If (C==1) jump to LBL1 else go to next instruction
BRBC s, k BRBC 4, Neg ;If (S==0) jump to Neg else go to next instruction
Bit Addressing Modes
University of Kashan Microprocessors 46
How to check a bit?
Use Skip with bit addressing Check a bit of IO register Check a bit of GPR
Use Branch with bit addressing Check a bit of SREG (BRBS,BRBC) Use specific instruction
BRBS BRBC
03/28/1436
24
Bit Addressing Modes
University of Kashan Microprocessors 47
How to manipulate a bit of SREG? Use Set and Clear instruction with bit addressing mode BSET s ;Set bit s of SREG BCLR s ;Clear bit s of SREG
Use specific instruction Below instructions
BSET BCLR
University of Kashan Microprocessors 48
SBIS
SBIS PINC, 7
6-25
03/28/1436
25
Bit Addressing Modes
University of Kashan Microprocessors 49
SBIC PINC, 7
R
SBIC
Bit Addressing Modes
University of Kashan Microprocessors 50
How to manipulate a bit of SRAM? Answer: SRAM is not bit addressable Load a byte from SRAM to a GPR Then manipulate or check it
03/28/1436
26
Accessing EEPROM in AVR
University of Kashan Microprocessors 51
SRAM is volatile
Flash is read only at run time
Write data into EEPROM to protect against power cut off
3 IO register EECR EEPROM Control Register
EEDR EEPROM Data Register
EEAR (EEARH and EEARL) EEPROM Address Register
Accessing EEPROM in AVR
University of Kashan Microprocessors 52
ATmega32 1 KB EEPROM 10 bit address (EEAR9:EEAR0)
ATmega16 512 B EEPROM 9 bit address (EEAR8:EEAR0)
ATmega64 2 KB EEPROM 11 bit address (EEAR11:EEAR0)
03/28/1436
27
Accessing EEPROM in AVR
University of Kashan Microprocessors 53
EECR is used to select the operation read or write
EERE: EEPROM Read Enable
EEWE: EEPROM Write Enable
EEMWE: EEPROM Master Write Enable If is set by user will be clear by hardware after 4 clock cycle
Write is started if both EEWE and EEMWE are 1
We can not start a read or write before last write is finished
When write is finished EEWE is cleared by hardware
Check it by polling
Accessing EEPROM in AVR
University of Kashan Microprocessors 54
Write algorithm
Wait until EEWE become 0
Put address to EEAR (EEARH and EEARL)
Put data to EEDR
Set EEMWE to 1
Set EEWE to 1 (within 4 cycle after setting EEMWE)
03/28/1436
28
Accessing EEPROM in AVR
University of Kashan Microprocessors 55
Accessing EEPROM in AVR
University of Kashan Microprocessors 56
Read algorithm
Wait until EEWE become 0
Put address to EEAR (EEARH and EEARL)
Set EERE to 1
Get data from EEDR
03/28/1436
29
Accessing EEPROM in AVR
University of Kashan Microprocessors 57
Accessing EEPROM in AVR
University of Kashan Microprocessors 58
How to initialize EEPROM Use .ESEG before .DB .CSEG is used for code memory
Example .ESEG
.ORG $10
DATA1: DB $95 ; EEPROM memory in address $10
DATA2: DB $19 ; EEPROM memory in address $11
Example DATA1: DB $10 ; Code memory .ESEG
DATA2: DB $20 ; EEPROM memory
DATA3: DB $35 ; EEPROM memory
.CSEG
DATA4: DB $45 ; Code memory
03/28/1436
30
Accessing EEPROM in AVR
University of Kashan Microprocessors 59
University of Kashan Microprocessors 60
03/28/1436
31
MACRO
University of Kashan Microprocessors 61
What is MACRO A group of instructions that is used repeatedly Using MACRO we can write just one copy and invoke it everywhere
needed MACRO reduces code writing Increases the readability of code
Definition .MACRO name
……
.…..
.ENDMACRO
Parameters Can take up to 10 parameters @0 to @9 refer to parameters in the body of MACRO
MACRO
University of Kashan Microprocessors 62
Example of MACRO definition .MACRO LOADIO
LDI R20, @1
OUT @0, R20
.ENDMACRO
Example of MACRO call LOADIO PORTA, 0x20
LOADIO SPL, 0x55
.EQU VAL_1=0xFF
LOADIO DDRC, VAL_1
03/28/1436
32
University of Kashan Microprocessors 63
Switches on Input Ports
University of Kashan Microprocessors 64
Pin should be defined input
DDRX.n 0
Case1: Internal pull-up is connected
PORTX.n 1
Case-2: Internal pull-up is not connected
PORTX.n 0
External pull-up should be connected
Current is 0.5mA on switch close
Good for all cases
Both give a logic 0 on switch close
Input Pin
Internal Pull-up
Push-Button
Input Pin
External Pull-up
Push-Button
Vcc=+5V
10K
03/28/1436
33
Switches on Input Ports
University of Kashan Microprocessors 65
DIP switches usually have 8 switches Both, case-1 and case-2 from previous page is allowed Sequence of instructions to read is:
P0P1P2P3P4P5P6P7
AVR Input Port
SW-DIP8
Vcc=+5V
10K ResPack
P0P1P2P3P4P5P6P7
AVR Input Port
SW-DIP8
LDI R16, 0
OUT DDRX, R16 ;Define port as Input
LDI R16, 0xFF
OUT PORTX, R16 ;Internal Pull-up
IN R20, PINX ;Read Data from Pins
LDI R16, 0
OUT DDRX, R16 ;Define port as Input
LDI R16, 0
OUT PORTX, R16 ;No Internal Pull-up
IN R20, PINX ;Read Data from Pins
Bouncing Problem
University of Kashan Microprocessors 66
Contact:
Push-button switches
Toggle switches
Electromechanical relays
Make and break Contact normally open switch
The effect is called "contact bounce" or, in a switch, "switch bounce”.
If used as edge-triggered input (as INT0), several interrupt is accorded
03/28/1436
34
Bouncing Problem
University of Kashan Microprocessors 67
Hardware Solution
An RC time constant
larger than the switch bounce
Another hardware Solution
Vcc
R
C
OUT
Bouncing Problem
University of Kashan Microprocessors 68
Software Solution
Periodically check
With appropriate time difference (10-20 ms) between check
Wait-and-see technique
When the input drops
An “appropriate” delay is executed (10-20 ms)
Then the value of the line is checked again to make sure the line has stopped bouncing
Read the new state of switch N time
03/28/1436
35
Interfacing a Keypad
University of Kashan Microprocessors 69
16 keys arranged as a 4X4 matrix
If there is no controllable internal pull-up (general case)
Check to see if key pressed Define R as output port Define C as input port Place a 0000 on R Read port C If there is a 0 bit on C then Key has been pressed
Which key is being pressed Define R as output port Define C as input port For i=1 to 4 do
Place a 0 on Ri Read port C If there is a 0 on Cj then Keyij has been pressed Else, try next row
0 1 2 3
5 6 7
D E F
9 A B
C
8
4
C1C2
C3
C4
R1
R2
R3
R4
Output
Input
General uP based system
Interfacing a Keypad
University of Kashan Microprocessors 70
16 keys arranged as a 4X4 matrix
If there is internal pull-up (AVRs)
Both functions are combined
Define C as input port Enable internal pull-up of C Define R as output port Place a 0000 on R Read port C If C=1111 return(null) Else if there is a 0 on Cj then {
Define R as input port Enable internal pull-up of R Define C as output port Place a 0000 on C Read port R If there is a 0 on Ri then return(Keyij) }
0 1 2 3
5 6 7
D E F
9 A B
C
8
4
C1 C2 C3
C4
R1
R2
R3
R4 Outp
ut
Input
Input
Outp
ut
1 2
2
1
03/28/1436
36
Interfacing a Keypad
University of Kashan Microprocessors 71
.include "m32def.inc"
.org 0 LDI R16, 0XFF ;define port D as output OUT DDRD, R16 WAIT_FOR_KEY: LDI R16,0XF0 ;define rows as input and column as output OUT DDRC,R16 LDI R16,0X0F ;pull up rows OUT PORTC,R16 ;and out 0 to columns lines IN R16, PINC ;read rows lines ORI R16, 0XF0 ;clear higher nibble to 1 CPI R16, 0XFF ;if no key pressed go back to up BREQ WAIT_FOR_KEY MOV R17,R16 ;save low nibble of pressed key to R16 LDI R16,0X00 ;set portc IO regs to initial values (0) OUT DDRC,R16 OUT PORTC,R16 LDI R16,0X0F ;define columns as input OUT DDRC,R16 ; and rows as output LDI R16,0XF0 ;pull up columns OUT PORTC,R16 ; and out 0 to rows lines IN R16, PINC ;read columns lines ORI R16, 0X0F AND R17, R16 ;combine low and high nibble of key code OUT PORTD, R17 ;out key code to port D WAIT2: IN R16, PINC ;read columns lines ORI R16, 0X0F CPI R16, 0XFF BRNE WAIT2 ;wait for release RJMP WAIT_FOR_KEY
The End
Questions?
University of Kashan Microprocessors 72