我在 PIC24f16ka102 和 xc16 编译器中遇到了一个简单而快速的 C 问题。
我想将变量引用传递给我的函数。变量在 eeprom 空间中:
int __attribute__ ((space(eedata))) eeData; // Variable located in EEPROM,declared as a global variable.
通过这个序列,我可以在 eeprom 内存中保存一些数据:
unsigned int offset;
// Set up NVMCON to erase one word of data EEPROM
NVMCON = 0x4004;
// Set up a pointer to the EEPROM location to be erased
TBLPAG = __builtin_tblpage(&eeData2); // Initialize EE Data page pointer
testDebug = TBLPAG;
offset = __builtin_tbloffset(&eeData); // Initizlize lower word of address
__builtin_tblwtl(offset, 0x9876); // Write EEPROM data to write latch
asm volatile ("disi #5"); // Disable Interrupts For 5 Instructions
__builtin_write_NVM(); // Issue Unlock Sequence & Start Write Cycle
while(NVMCONbits.WR == 1);
这样我将值 0x9876 写入 eeprom 的前 16 位。但我需要把它作为 &eeData
我想编写自己的函数:
void eeprom_writeWord(unsigned int __attribute__ ((space(eedata))) addresOfMyEEpromVariable, unsigned int value)
{
unsigned int offset;
// Set up NVMCON to erase one word of data EEPROM
NVMCON = 0x4004;
// Set up a pointer to the EEPROM location to be erased
TBLPAG = __builtin_tblpage(&addresOfMyEEpromVariable); // Initialize EE Data page pointer
offset = __builtin_tbloffset(&addresOfMyEEpromVariable); // Initizlize lower word of address
__builtin_tblwtl(offset, value); // Write EEPROM data to write latch
asm volatile ("disi #5"); // Disable Interrupts For 5 Instructions
__builtin_write_NVM(); // Issue Unlock Sequence & Start Write Cycle
while(NVMCONbits.WR == 1);
}
但是如何将我的地址作为函数参数传递,以便我的函数可以看到它仍然是 eeprom 空间中的地址?它不能只是地址,因为如果这样我就会出错。__builtin 函数需要具有某些属性的地址,它是 eeprom 内存。
如何将带有属性的 eeprom 地址传递给我的函数?请帮忙
编辑:
谢谢你的建议,但我仍然得到同样的错误:
error: Argument to __builtin_tbloffset() is not the address
of an object in a code, psv, or eedata section;
函数 __builtin_tbloffset 需要 eeprom 内存的地址,而不仅仅是某物的地址。如果我使用整个序列而不是函数(我的意思是我的第一篇文章中的序列),它会很好地工作。
现在我按照你所说的那样尝试:
void eeprom_writeWord(unsigned int *addresOfMyEEpromVariable, unsigned int value)
{
//write word
// Set up NVMCON to write one word of data EEPROM
NVMCON = 0x4004;
// Set up a pointer to the EEPROM location to be written
TBLPAG = __builtin_tblpage(*addresOfMyEEpromVariable);
unsigned int offset = __builtin_tbloffset(*addresOfMyEEpromVariable);
// Write Data Value To Holding Latch
__builtin_tblwtl(offset, 0x9999);
// Disable Interrupts For 5 Instructions
asm volatile ("disi #5");
// Issue Unlock Sequence & Start Write Cycle
__builtin_write_NVM();
while(NVMCONbits.WR == 1);
}
甚至没有'*'符号:
void eeprom_writeWord(unsigned int *addresOfMyEEpromVariable, unsigned int value)
{
//write word
// Set up NVMCON to write one word of data EEPROM
NVMCON = 0x4004;
// Set up a pointer to the EEPROM location to be written
TBLPAG = __builtin_tblpage(addresOfMyEEpromVariable);
unsigned int offset = __builtin_tbloffset(addresOfMyEEpromVariable);
// Write Data Value To Holding Latch
__builtin_tblwtl(offset, 0x9999);
// Disable Interrupts For 5 Instructions
asm volatile ("disi #5");
// Issue Unlock Sequence & Start Write Cycle
__builtin_write_NVM();
while(NVMCONbits.WR == 1);
}
结果还是一样。__builtin_tblpage 和其他 __builtin_(...) 函数是内置于 xc16 编译器的函数。