GCCAVR(AVR Studio)数据存储在程序存储器(flash)空间的定义

   

1)flash常量:

         #include <avr\pgmspace.h>//须增加的头文件

         const prog_uchar FlashConst = 3;         //定义uchar型的常量n定义在flash里(flash常量)

         unsigned char RamVar;                          //定义无符号整型变量(Ram变量)

         RamVar = pgm_read_byte(&FlashConst); //读取flash常量到ram变量

       

(2)flash一维数据:

         #include <avr\pgmspace.h>

         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 <avr\pgmspace.h>

         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)。括号中是地址值。

 

 

博客内容来源:

http://hi.baidu.com/%B7%DC%C7%E0%D0%A1%C7%BF/blog/item/a4bdb3c4c3aeebd3d000607c.html

 

你可能感兴趣的:(GCCAVR(AVR Studio)数据存储在程序存储器(flash)空间的定义)