1
votes

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 ?

1

1 Answers

2
votes

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.