(1)flash常量:
#include//须添加的头文件
const prog_uchar FlashConst = 3; //界说uchar型的常量n界说在flash里(flash常量)
unsigned char RamVar; //界说无符号整型变量(Ram变量)
RamVar = pgm_read_byte(&FlashConst); //读取flash常量到ram变量
(2)flash一维数据:
#include
const prog_uchar s[5] = { 1, 2, 3, 4, 5 };
unsigned char RamVar; //界说无符号整型变量(Ram变量)
RamVar = pgm_read_byte( &s[1] ); //读取s[1]的值到RamVar, or RamVar = pgm_read_byte( s+1 );
(3)flash多维数据:
#include
const prog_uchar s[4][16] = { {14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7 },
{ 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8 },
{ 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0 },
{15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13 }
};
unsigned char RamVar[4],[16]; //界说无符号整型变量(Ram变量)
register char i, j;
for(i=0; i<4; ++) { for(j=0; j<16; j++) { RamVar[i][j] = pgm_read_byte( &s[i][j] ); //读取数组s的值到RamVar }//end for 2 }//end for 1 (4)扩展部分 avr对ram和flash是独立编址的,ram是按8位编址,而flash却按16位编址,读ram和读flash的汇编指令也是不同的。
相似flash数据类型还有:prog_void 、prog_char 、prog_int8_t、prog_uint8_t、prog_int16_t、prog_uint16_t、prog_int32_t、prog_uint32_t等。
读取指令pgm_read_xxx宏界说其实便是一段包含了flash读取指令的内联汇编代码。函数原型为:pgm_read_byte(address_short)、pgm_read_word(address_short)、pgm_read_dword(address_short)、pgm_read_float(address_short)。括号中是地址值。