|
![](static/image/common/ico_lz.png)
楼主 |
发表于 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 */
}; |
|