Because the first one uses a variable index, forcing the compiler to generate code to fetch the data. The "_PGM" part of the name means that the array is in PROGMEM but you are not calling the function that would fetch the data from PROGMEM so it is fetched from the same address in RAM. The RAM address does not contain the data you want.
The second one uses constants indexing into a constant array so the compiler knows what the result of the fetch should be and doesn't generate code to fetch the data.
To get the first one to work you would need something like:
johnwasser:
Because the first one uses a variable index, forcing the compiler to generate code to fetch the data. The "_PGM" part of the name means that the array is in PROGMEM but you are not calling the function that would fetch the data from PROGMEM so it is fetched from the same address in RAM. The RAM address does not contain the data you want.
The second one uses constants indexing into a constant array so the compiler knows what the result of the fetch should be and doesn't generate code to fetch the data.
To get the first one to work you would need something like:
I assume it's a 'word' array rather than a 'byte' array because the values fetched from RAM would not fit in a byte.
Thank you! It works as expected now. But why does "port_to_mode_PGM" return a 16 bit word, when data direction registers are 8 bit. Should i just cast it to a uint8_t ?
Genwolf:
Thank you! It works as expected now. But why does "port_to_mode_PGM" return a 16 bit word, when data direction registers are 8 bit. Should i just cast it to a uint8_t ?
Because it is returning the ADDRESS of the Data Direction (pinMode()) Register for the port and addresses on the UNO are 16-bit. To use the address, cast it as a "(uint8_t *)" like *((uint8_t *)DDRaddress) = value;