Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we add an Offset when addressing DDRx ,PORTx in avr?

I recently started programming my Arduino (ATmega328p) using AVR and gained sufficient knowledge on using avr lib,instead of arduino IDE.I started with a blink led example.

Then i tried the same eg but this time however i didnot include io.h or any other lib, and instead used pointer to PORTs Like DDRB or PORTB etc.

According to atmega328p datasheet, DDRB is at 0x04 and PORTB at 0x05.

However setting them high for my blinking led eg didnot work. Instead i had to add an offset of 0x20 to both addresses, after it worked perfectly. i.e.,DDRB=0x24 and PORTB=0x25 Where is this offset(0x20) comming from ?

like image 986
darth Avatar asked Jan 28 '26 01:01

darth


1 Answers

If you look at the memory map on p.19 of the datasheet, you see that the first 32 byte addresses are the registers, and the I/O registers start after these. That is why you need to add a 32 (0x20) offset to get the SRAM address.

like image 85
uncleO Avatar answered Jan 31 '26 14:01

uncleO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!