我正在尝试将字符串传递给函数。我已经阅读了很多有关 C18 中 RAM/ROM 字符串的内容,我的代码看起来不错,但它不起作用。我的功能是这样的:
int setBluetoothName (static const rom char *name){
unsigned int n = 0, k = 0;
char string[27] = {0}; // ATNAME + 20 caracteres de nombre + EOF
char command[27] = {0};
_bit state = USART_INT;
// Deshabilito interrupciones del USART
USART_INT_DISABLE;
strcpypgm2ram(&string, &name);
// Numero de caracteres del nombre
for (; string[n] != 0x00 ; ++n);
if(n > 19) return LENGTH_ERROR; // si me pase de 20 letras es muy largo
// Configuro el nombre del Bluetooth
printf("ATNAME%s", &string);
我这样使用它:
setBluetoothName("Brazo");
在 Proteus 中,我看到只有字母“B”被传递,当我将 ROM 字符串复制到 RAM 时,它简直是一团糟(见附图)。 printf() 的输出仅为 ATNAME,不会打印字符串。
我正在使用 C18 v4.40 和 MPLABX v1.41。 非常感谢您的帮助。
最佳答案
尝试:
strcpypgm2ram(string, name);
和
printf("ATNAME%s", string);
当你声明一个数组时
char string[27] = {0};
那么变量string指的是数组第一个元素的地址,当你声明一个参数如
int setBluetoothName (static const rom char *name)
那么name就是字符串所在的地址。
当您在其中添加 & 时,您将获得包含数据地址的变量的地址。
关于c - 为什么 ROM 字符串没有正确传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616962/