Lecture 05 – AVR Addressing Modes

Ryan Robucci

Program and Data addressing modes in AVR

Table of Contents

Introduction

Type 1: Register Direct (Single Reg.)

Example Decription
INC R0 ++i;
DEC R5 --j;
LSL R9 Shift R9, X<<1;
REGISTER FILE 15 31

Type 2: Register Direct (Two Reg.)

Example Description
ADD R1, R3 i = i+j; //i+=j;
SUB R5, R7 i = i-j;
REGISTER FILE 15 31

Type 3: Immediate Mode

SUBI R4, 8    ;Subtract Constant from Register   
ADIW R26, 5   ;Add Immediate to Word  
              ; R27:R26 ← R27:R26 + 5  --  

like

i-=8;  
a+=29;  

Type 4: I/O Direct

uO MEMORY 15 63

C

unsigned char i = PINB;  
unsigned char k = 45;  
PORTC = k;

accomplished using

IN R10, PINB if the input to PORTB is 0x03 and all the pins are configured as an inputs, R10 becomes 0x03

OUT PORTC, R1 the contents of R1 are used to set the output register of PORTC

Table with Register Addresses, and Corresponding Memory Address:

Type 5: Data Direct

STS K, Rs

LDS RD, K

Note writing a constant to memory requires writing it to a register first

20 19 31 16 LSBs 15 16 Rr/Rd Data Space $0000 $FFFF

Type 6: Data Indirect

Data Space Y OR Z - REGISTER 10

Look at Page 378 of data sheet

I/O Ports using Indirect

The I/O ports can also be accessed using SRAM access commands, e.g. ST and LD.
Add 0x20 to the port number (the first 32 addresses are the registers!)

Example:

.DEF SomeRegister = R16 

  LDI ZH,HIGH(PORTB+32) 
  LDI ZL,LOW(PORTB+32) 
  LD SomeRegister,Z 
Data Memory oxoooo General purpose Real Registers Static RAM Registers (32) I/O Registers (64) Extended I/O Registers (160) Internal SRAM (1024) 8 bits Ox001F ox0020 Ox005F ox0060 OxOOFF ox0100 Ox4FF

Extended I/O

For I/O registers located in extended I/O map,
I/O register direct commands like "IN", "OUT", "SBIS", "SBIC", "CBI", and "SBI" cannot be used.
Use direct (memory) and indirect (memory) instructions that allow access to extended I/O, typically "LDS" and "STS" combined with "SBRS", "SBRC", "SBR", and "CBR" if modifying a single pin

Data Memory oxoooo General purpose Real Registers Static RAM Registers (32) I/O Registers (64) Extended I/O Registers (160) Internal SRAM (1024) 8 bits Ox001F ox0020 Ox005F ox0060 OxOOFF ox0100 Ox4FF

Type 7: Direct program addressing

CALL k ;

Type 8: Implicit Addressing

CLC

RET

Type 9: Indirect Program Addressing

In these types of instructions, the Z register is used to point to

the program memory. (Up to 64 Kbytes of program memory)

IJMP ;Indirect Jump to (Z) PC ← Z

ICALL ;Indirect Call to (Z) PC ← Z,STACK = PC + 1

PROGRAM MEMORY 15 Z.REGISTER

Type 10: Relative program addressing

PROGRAM MEMORY $000 15 12 11

RCALL k ;Relative Subroutine Call PC ← PC + k + 1 , STACK=PC+1
RJMP k ;Relative Jump PC ← PC + k + 1