cazhcs 发表于 2009-3-30 11:04:46

CVAVR中指针调用出错

程序中,数组定义:const uchar data[]={...}
      函数定义:void pit(const uchar *ucp){}
      调用:pit(data);
在ICC里可编译通过,
但在CVAVR中编译时提示出错是怎么回事。
请帮忙,谢谢了。

cazhcs 发表于 2009-3-30 14:46:05

是不是问题太简单了,但我刚从ICC转向CV,在书店也找不到CVAVR的书.
所以,拜托那位一下了.

kbdcj2000 发表于 2009-3-31 11:51:31

这个问题我也遇到过。后来把数组名赋值给一个指针来调用的。没有深入看cvavr手册。可能跟cvavr的规定有关吧。

cazhcs 发表于 2009-3-31 12:21:07

谢谢你,我再试试.

cazhcs 发表于 2009-4-1 09:32:06

不行呀,在函数调用时应如何赋值呀??

cazhcs 发表于 2009-4-1 09:32:33

那位高手帮帮忙吧.

shaozh 发表于 2009-4-1 09:56:21

改一下你的data名字!比如dddd试试!

cazhcs 发表于 2009-4-1 14:39:29

对不起,原程序中数组名是:HZ_data[].
缩写了.不关名称的问题.
在编译时,提示:pit(data); 出错.原因:与声明的类型不一致.

Appcat 发表于 2009-4-1 14:49:44

const在CV中好像是等价flash关键字
吧函数改下试试:

函数定义:void pit(flash unsigned char *ucp){}

cazhcs 发表于 2009-4-1 17:21:55

好的,先试试,不过也先谢了.

cazhcs 发表于 2009-4-2 12:26:38

试过了,8楼的办法也不行呀。

cazhcs 发表于 2009-4-6 12:35:54

请马老师帮帮忙,好吗?

kbdcj2000 发表于 2009-4-7 13:55:49

前几天看c的书。数组名是常数,如果你的函数里面有++,--的运算可能有问题。常数++,--运算没有意义。

cazhcs 发表于 2009-4-8 12:26:23

如果用重定义类型: typedel unsindeg char uchar 这样就可以通过编译了.
而用: #defind uchar unsindeg char 预处理就不能通过编译,
现在是可以通过编译了,但我不明白这是为什么?

machao 发表于 2009-4-12 19:54:39

奇怪!?为什么放者现成的文挡,就是不看呢?

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 wordaddress 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={"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={"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;

/* and initialize the array */

pp=m1;

pp=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);

}

cazhcs 发表于 2009-4-13 12:23:01

谢谢马老师.
页: [1]
查看完整版本: CVAVR中指针调用出错