4.3.10.9 Progmem Attribute
Using both the progmem
attribute and the
const
qualifier allows you to have objects placed in the program
memory. Alternatively, you can use the more portable PROGMEM
macro,
defined by <avr/pgmspace.h>
, which maps to this attribute. For
example.
#include <avr/pgmspace.h>
const unsigned char PROGMEM romChar = 0x55;
Prior to the AVR GCC compiler supporting names address spaces, this was the only way in which objects could be placed in flash, thus it exists today for compatibility with legacy projects or to improve portability of code migrated from other platforms.
Unlike const
-qualified objects in program memory, which
can be read directly, objects defined using the progmem
attribute must
be read using an appropriate library function, e.g.,
pgm_read_byte_near()
. It is up to the programmer to use the correct
library function; however, the code to access objects defined in this way is typically
efficient, since their location is more accurately known by the compiler.