如果要将数据(如常量,字符串,等等)放在FLASH里,用户需要指明数据类型__ATTR_PROGMEM__。为了方便使用,AVR-GCC定义了一些更直观的符号,如下表所示。
typedef void PROGMEM prog_void
typedef char PROGMEM prog_char
typedef unsigned char PROGMEM prog_uchar
typedef int8_t PROGMEM prog_int8_t
typedef uint8_t PROGMEM prog_uint8_t
typedef int16_t PROGMEM prog_int16_t
typedef uint16_t PROGMEM prog_uint16_t
typedef int32_t PROGMEM prog_int32_t
typedef uint32_t PROGMEM prog_uint32_t
typedef int64_t PROGMEM prog_int64_t
typedef uint64_t PROGMEM prog_uint64_t
宏定义:定义FLASH空间的指针类型
#define PROGMEM __ATTR_PROGMEM__
#define PSTR(s) ((const PROGMEM char *)(s))
#define PGM_P const prog_char *
#define PGM_VOID_P const prog_void *宏函数:执行LPM/ELPM指令从FLASH里取数
#define pgm_read_byte_near(address_short) __LPM((uint16_t)(address_short))
#define pgm_read_word_near(address_short) __LPM_word((uint16_t)(address_short))
#define pgm_read_dword_near(address_short) __LPM_dword((uint16_t)(address_short))
#define pgm_read_byte_far(address_long) __ELPM((uint32_t)(address_long))
#define pgm_read_word_far(address_long) __ELPM_word((uint32_t)(address_long))
#define pgm_read_dword_far(address_long) __ELPM_dword((uint32_t)(address_long))
#define pgm_read_byte(address_short) pgm_read_byte_near(address_short)
#define pgm_read_word(address_short) pgm_read_word_near(address_short)
#define pgm_read_dword(address_short) pgm_read_dword_near(address_short)
库函数
4. strchr_P
用法:iPGM_P strchr_P( PGM_P s, int val);
说明:功能与strchr()类似。第二个参数指向程序存储器内的字符串。
5.strcmp_P
用法:int strcmp(char const *, PGM_P);
说明:功能与strcmp()类似。第二个参数指向程序存储器内的字符串。
6.strcpy_P
用法:char* strcpy_P(char *, PGM_P);
说明:功能与strcpy()类似。第二个参数指向程序存储器内的字符串。
7.strcat_P
用法:char * strcat_P(char *, PGM_P);
说明:功能与strcat()类似。第二个参数指向程序存储器内的字符串。
8.strlen_P
用法:size_t strlen_P(PGM_P);
说明:功能与strlen()类似。第二个参数指向程序存储器内的字符串。
9.strncmp_P
用法:size_t strncmp_P(char const*, PGM_P, size_t);
说明:功能与strncmp()类似。第二个参数指向程序存储器内的字符串。
10.strncpy_P
用法:size_t strncpy_P(char*, PGM_P, size_t);
说明:功能与strncpy()类似。第二个参数指向程序存储器内的字符串。