# ATmega128 Extended I/O

This is my fist time working with a "bigger" micro controller, ATmega128. So far I've worked with ATmega328, ATmega8 etc.

Reading through the datasheet, I didn't understand couple of points

• That AVR instruction set can support a maximum of 64 I/O locations. Why is that? Does the AVR instruction set take in 6 bits for an I/O location?

• To accommodate the extended I/O in ATmega128 (160 extra ones), these 160 locations have been mapped to the internal SRAM right after the 64 existing ones. So in effect out of the 4KB SRAM that ATmega128 provides, 160 is taken up by these extended I/O locations. What happens if the ATmega103 compatibility fuse set (programmed by default)? Do I have access to these 160 locations then?

• How exactly do I access these extended locations? The datasheet asks to use LD/LDS/LDD and ST/STS/STD. Any examples to this as I've never used assembly before. I am kind of confused how this wold integrate into my C code workflow? Let's say I need to access DDRF and PORTF. Would I need to know their address(in SRAM) in advance or is there some macro in avr-gcc?

Any code samples or links to read and understand above would be great!

That AVR instruction set can support a maximum of 64 I/O locations. Why is that? Does the AVR instruction set take in 6 bits for an I/O location?

Most AVR instructions put their arguments as part of the machine code word. IN, OUT, SBI, and CBI have no more than 6 bits available for arguments.

To accommodate the extended I/O in ATMega128 (160 extra ones), these 160 locations have been mapped to the internal SRAM right after the 64 existing ones. So in effect out of the 4KB SRAM that ATMega128 provides, 160 is taken up by these extended I/O locations.

Incorrect. All 4kiB is available, 160 addresses further up.

What happens if the ATMega103 compatibility fuse set (programmed by default)? Do I have access to these 160 locations then?

No. You lose the 160 extended registers (and the SRAM moves back to fill them) plus the last 96 bytes of SRAM.

How exactly do I access these extended locations?

With normal memory access instructions.

The datasheet asks to use LD/LDS/LDD and ST/STS/STD. Any examples to this as I've never used assembly before.

LDS R1, $61 ORI R1,$08
STS R1, $61 LDS R1,$62
ANDI R1, $F7 STS R1,$62


I am kind of confused how this wold integrate into my C code worflow? Let's say I need to access DDRF and PORTF. Would I need to know their address(in SRAM) in advance or is there some macro in avr-gcc?

AVR Libc handles all that for you.

DDRF |= _BV(PF3);
PORTF &= ~_BV(PF3);

• So basically I just de-program the ATMega103 compatibility fuse in ATMega128 and then use extended PORTC/G/F as normal. correct? – Ankit Aug 4 '14 at 4:46
• At the C level, yes. – Ignacio Vazquez-Abrams Aug 4 '14 at 4:47