在AVR GCC中如何使用C语言在程序空间申请常量数组?就像在KEIL中使用CODE或者PCODE的功能。

本人想在MEGA48的程序空间定义常量数组,但是我试过code和pcode都不可以,编译不过,哪位大侠用过?请指点,谢谢

GCC中把大数组存入flash区的方法大全

AVRGCC中将变量定义在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)。括号中是地址值。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-12-02
winavr目录下如C:\WinAVR-20080610\avr\include\avr下有很多头文件,你说的定义可能在eeprom.h文件中,找到相应的宏定义,自己建立新的宏定义,重新定义成code,pcode以方便自己的习惯。GCC的类似命令就是小脚老太裹脚布又臭(丑)又长。
第2个回答  2009-12-06
1 包含头文件 #include <avr/pgmspace.h>
2 定义常量数组 prog_uint8_t Data[]={0x7F,0x7F,0x7F,0x7F,0x80,0x80,0x7F};
3 读取
char * pData=Data;
unsigned char Temp=pgm_read_byte(pData++);
相似回答
大家正在搜