2

Assume I want to write the following without using DDRD or PORTD:

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRD = 0xFF;
    PORTD = 0xFF; 
}

I found this link which includes a list of mapped registers to memory addresses: https://github.com/DarkSector/AVR/blob/master/asm/include/m328Pdef.inc

So I replaced one of the keywords with a pointer:

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRD = 0xFF;
    unsigned char *portd = 0x0b;
    *portd = 0xFF;
}

But it's not working (my LED isn't turning on with the second code). Why is that?

Juraj
  • 18,037
  • 4
  • 29
  • 49
Joes
  • 21
  • 1
  • 2
  • It might get optimized out, as it's not volatile. Or it's using wrong instruction to store data so it needs different address. – KIIV Sep 23 '18 at 05:29
  • 1
    what in this question is about Arduino? – Juraj Sep 23 '18 at 08:34
  • If you'd use unsigned char *portd = &PORTD; would your pointer work for port manipulation? That would lead to #2. 2. From here:
  • unsigned char * myportA = (unsigned char *) 0x003B; Of course, adjusted to your microcontroller. If that works for you, I lack of knowledge why would you need to cast it, but if passing the port name by reference works (#1) and direct assign using the hex doesn't, it has something to do with that. 3. Could declaring the pointer variable volatile help, in case something else modifies it?

    – dBm Sep 23 '18 at 05:42