|
发表于 2008-7-14 14:54:10
|
显示全部楼层
下面是CVAVR中关于指针使用的说明:
Pointers
Due to the Harvard architecture of the AVR microcontroller, with separate address spaces for data (SRAM), program (FLASH) and EEPROM memory, the compiler implements three types of pointers.
The syntax for pointer declaration is:
[<type storage modifier>] type * [<pointer storage modifier>]
[* [<pointer storage modifier>] ...] pointer_name;
or
type [<type storage modifier>] * [<pointer storage modifier>]
[* [<pointer storage modifier>] ...] pointer_name;
where type can be any data type.
Variables placed in SRAM are accessed using normal pointers.
For accessing constants placed in FLASH memory, the flash type storage modifier is used.
For accessing variables placed in EEPROM, the eeprom type storage modifier is used.
Although the pointers may point to different memory areas, they are by default stored in SRAM.
Example:
/* Pointer to a char string placed in SRAM */
char *ptr_to_ram="This string is placed in SRAM";
/* Pointer to a char string placed in FLASH */
flash char *ptr_to_flash1="This string is placed in FLASH";
char flash *ptr_to_flash2="This string is also placed in FLASH";
/* Pointer to a char string placed in EEPROM */
eeprom char *ptr_to_eeprom1="This string is placed in EEPROM";
char eeprom *ptr_to_eeprom2="This string is also placed in EEPROM";
In order to store the pointer itself in other memory areas, like FLASH or EEPROM, the flash or eeprom pointer storage modifiers must be used as in the examples below:
/* Pointer stored in FLASH to a char string placed in SRAM */
char * flash flash_ptr_to_ram="This string is placed in SRAM";
/* Pointer stored in FLASH to a char string placed in FLASH */
flash char * flash flash_ptr_to_flash="This string is placed in FLASH";
/* Pointer stored in FLASH to a char string placed in EEPROM */
eeprom char * flash eeprom_ptr_to_eeprom="This string is placed in EEPROM";
/* Pointer stored in EEPROM to a char string placed in SRAM */
char * eeprom eeprom_ptr_to_ram="This string is placed in SRAM";
/* Pointer stored in EEPROM to a char string placed in FLASH */
flash char * eeprom eeprom_ptr_to_flash="This string is placed in FLASH";
/* Pointer stored in EEPROM to a char string placed in EEPROM */
eeprom char * eeprom eeprom_ptr_to_eeprom="This string is placed in EEPROM";
In order to improve the code efficiency several memory models are implemented.
The TINY memory model uses 8 bits for storing pointers to the variables placed in SRAM. In this memory model you can only have access to the first 256 bytes of SRAM.
The SMALL memory model uses 16 bits for storing pointers the variables placed in SRAM. In this memory model you can have access to 65536 bytes of SRAM.
In both TINY and SMALL memory models pointers to the FLASH memory area use 16 bits.
Because in these memory models pointers to the FLASH memory are 16 bits wide, the total size of the constant arrays and literal char strings is limited to 64K.
However the total size of the program can be the full amount of FLASH.
In order to remove the above mentioned limitation, there are available two additional memory models: MEDIUM and LARGE.
The MEDIUM memory model is similar to the SMALL memory model, except it uses pointers to constants in FLASH that are 32 bits wide. The pointers to functions are however 16 bit wide because they hold the word address of the function, so 16 bits are enough to address a function located in all 128kbytes of FLASH.
The MEDIUM memory model can be used only for chips with 128kbytes of FLASH.
The LARGE memory model is similar to the SMALL memory model, except it uses pointers to the FLASH memory area that are 32 bits wide.
The LARGE memory model can be used for chips with 256kbytes or more of FLASH.
In all memory models pointers to the EEPROM memory area are 16 bit wide.
Pointers can be grouped in arrays, which can have up to 8 dimensions.
Example:
/* Declare and initialize a global array of pointers to strings
placed in SRAM */
char *strings[3]={"One","Two","Three"};
/* Declare and initialize a global array of pointers to strings
placed in FLASH
The pointer array itself is also stored in FLASH */
flash char * flash messages[3]={"Message 1","Message 2","Message 3"};
/* Declare some strings in EEPROM */
eeprom char m1[]="aaaa";
eeprom char m2[]="bbbb";
void main(void) {
/* Declare a local array of pointers to the strings placed in EEPROM
You must note that although the strings are located in EEPROM,
the pointer array itself is located in SRAM */
char eeprom *pp[2];
/* and initialize the array */
pp[0]=m1;
pp[1]=m2;
}
Pointers to functions always access the FLASH memory area. There is no need to use the flash keyword for these types of pointers.
Example:
/* Declare a function */
int sum(int a, int b) {
return a+b;
}
/* Declare and initialize a global pointer to the function sum */
int (*sum_ptr) (int a, int b)=sum;
void main(void) {
int i;
/* Call the function sum using the pointer */
i=(*sum_ptr) (1,2);
} |
|