搜索
bottom↓
回复: 3

CVAVR结构体定义在FLASH问题!

[复制链接]

出0入0汤圆

发表于 2010-8-21 22:14:47 | 显示全部楼层 |阅读模式
放弃GCC开始研究CVAVR。。遇到一点问题。。熟悉CVAVR的朋友看看。。我画的这两句话有矛盾吗??第一句话,我的理解是,不能用一个指针变量去操作FLASH或者EEPROM中的结构体,但是第二句话明显和我的想法有矛盾!!

(原文件名:截图00.jpg)

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2010-8-22 17:22:13 | 显示全部楼层
为什么不看看原文是怎么写的?CVCVR的HELP中就有。

中文的东西只能做初步的参考,尤其是现在的东西

出0入0汤圆

 楼主| 发表于 2010-8-22 21:31:49 | 显示全部楼层
回复【1楼】machao  
-----------------------------------------------------------------------

谢谢马老师。。我看了。。help中确实没有第一句话。。

Structures Previous Top Next  

--------------------------------------------------------------------------------

Structures are user-defined collections of named members.
The structure members can be any of the supported data types, arrays of these data types or pointers to them.
Structures are defined using the struct reserved keyword.
The syntax is:


[<memory attribute>] struct [<structure tag-name>] {
    [<type> <variable-name>[,<variable-name>, ...]];
    [<type> [<bitfield-id>]:<width>[,[<bitfield-id>]:<width>, ...]];
    ...
    } [<structure variables>];


Example:


/* Global structure located in RAM */
struct ram_structure {
             char a,b;
             int  c;
             char d[30],e[10];
             char *pp;
             } sr;


/* Global constant structure located in FLASH */
flash struct flash_structure {
             int  a;
             char b[30], c[10];
             } sf;


/* Global structure located in EEPROM */
eeprom struct eeprom_structure {
             char a;
             int  b;
             char c[15];
             } se;


void main(void) {
/* Local structure */
struct local_structure {
             char a;
             int  b;
             long c;
             } sl;


/* ............. */


}


The space allocated to the structure in memory is equal to sum of the sizes of all the members.


The same generic structure type can be declared in any memory type: RAM, FLASH or EEPROM:


/* Generic structure type */
struct my_structure {
             char a,b;
             int  c;
             char d[30],e[10];
             char *pp;
             };


/* Global structure located in RAM */
struct my_structure sr;


/* Global pointer located in RAM to the RAM located structure */
struct my_structure *ptrsr = &sr;


/* Global pointer located in FLASH to the RAM located structure */
struct my_structure * flash ptrfsr = &sr;


/* Global pointer located in EEPROM to the RAM located structure */
struct my_structure * eeprom ptresr = &sr;


/* Global constant structure located in FLASH */
flash struct my_structure sf = {0,0,0,{0},{0},0};


/* Global pointer located in RAM to the FLASH located structure */
flash struct my_structure *ptrsf = &sf;


/* Global pointer located in FLASH to the FLASH located structure */
flash struct my_structure * flash ptrfsf = &sf;


/* Global pointer located in EEPROM to the FLASH located structure */
flash struct my_structure * eeprom ptresf = &sf;


/* Global constant structure located in EEPROM */
eeprom struct my_structure se;


/* Global pointer located in RAM to the EEPROM located structure */
eeprom struct my_structure *ptrse = &se;


/* Global pointer located in FLASH to the EEPROM located structure */
eeprom struct my_structure * flash ptrfse = &se;


/* Global pointer located in EEPROM to the EEPROM located structure */
eeprom struct my_structure * eeprom ptrese = &se;


void main(void) {
/* Local structure */
struct my_structure sl;
/* Local pointer to the RAM located global structure */
struct my_structure *ptrlsr = &sr;
/* Local pointer to the FLASH located global structure */
flash struct my_structure *ptrlsf = &sf;
/* Local pointer to the EEPROM located global structure */
eeprom struct my_structure *ptrlse = &se;

/* ............. */


}


Structures can be grouped in arrays.
Example how to initialize and access an global structure array stored in EEPROM:


/* Global structure array located in EEPROM */
eeprom struct eeprom_structure {
             char a;
             int  b;
             char c[15];
             } se[2]={{'a',25,"Hello"},
                     {'b',50,"world"}};


void main(void) {
char k1,k2,k3,k4;
int i1, i2;


/* define a pointer to the structure */
struct eeprom_structure eeprom *ep;


/* direct access to structure members */
k1=se[0].a;
i1=se[0].b;
k2=se[0].c[2];
k3=se[1].a;
i2=se[1].b;
k4=se[1].c[2];


/* same access to structure members using a pointer */
ep=&se; /* initialize the pointer with the structure address */
k1=ep->a;
i1=ep->b;
k2=ep->c[2];
++ep;   /* increment the pointer */
k3=ep->a;
i2=ep->b;
k4=ep->c[2];
}


Because some AVR devices have a small amount of RAM, in order to keep the size of the Data Stack small, it is recommended not to pass structures as function parameters and use pointers for this purpose.
Example:


struct alpha {
             int a,b, c;
             } s={2,3};
/* define the function */
struct alpha *sum_struct(struct alpha *sp) {
/* member c=member a + member b */
sp->c=sp->a + sp->b;
/* return a pointer to the structure */
return sp;
}
void main(void) {
int i;
/* s->c=s->a + s->b */
/* i=s->c */
i=sum_struct(&s)->c;
}


Structure members can be also declared as bit fields, having a width from 1 to 32.
Bit fields are allocated in the order of declaration starting from the least significant bit.
Example:


/* this structure will occupy 1 byte in RAM
   as the bit field data type is unsigned char */
struct alpha1 {
              unsigned char a:1; /* bit 0 */
              unsigned char b:4; /* bits 1..4 */
              unsigned char c:3; /* bits 5..7 */
              };


/* this structure will occupy 2 bytes in RAM
   as the bit field data type is unsigned int */
struct alpha2 {
              unsigned int a:2; /* bits 0..1 */
              unsigned int b:8; /* bits 2..9 */
              unsigned int c:4; /* bits 10..13 */
                                /* bits 14..15 are not used */
              };


/* this structure will occupy 4 bytes in RAM
   as the bit field data type is unsigned long */
struct alpha3 {
              unsigned long a:10; /* bits 0..9 */
              unsigned long b:8;  /* bits 10..17 */
              unsigned long c:6;  /* bits 18..23 */
                                  /* bits 24..31 are not used */
              };
头像被屏蔽

出0入0汤圆

发表于 2012-9-2 14:29:03 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-15 10:15

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表