我正在尝试通过运行一系列地址(例如 0x0 到 0xfef)来测试我正在使用的设备的 RAM。
我尝试了很多方法,但没有任何效果。这是我想要做的一个例子:
unsigned char temp;
unsigned char* addr = 0x0; // create ptr to point to address in mem
while(addr != 0xfef) // while not at end of mem
{
temp = *addr; // save value at current addr
*addr = 0xAA; // set value at addr to 0xAA
if(*addr != 0xAA) // if value did not write properly, do not run software
while(1);
*addr = temp; // restore value at addr to original value
++addr; // move on to next addr
}
此代码中唯一起作用的操作是将 addr 处的值设置为 0xAA。 while 和 if 语句都会给出错误,“比较需要错误 [1128] 兼容标量操作数”。
我还尝试使用所需的地址值创建指针,但尝试将指针设置为另一个指针会产生错误,“赋值时错误 [1131] 类型不匹配”。
提前谢谢您。
编辑2018-04-06:
更多信息: 我用的是PIC18F66K80。 我们可以使用 SFR 来执行此功能:
FSR0 = 0x00;
while(FSR0 != 0xfef)
{
temp = INDF0;
INDF0 = 0xAA;
if(INDF0 != 0xAA)
while(1);
POSTINC0 = temp;
};
C 代码现在运行,并修改内存,事实证明我在内存中查找了错误的位置...
但是,它似乎在 addr = 0xDAA 处失败。我会注意到指针addr的地址是0xD08,而temp的地址是0xD07。
最佳答案
您可能会遇到类型冲突,其中常量的类型是 int ,而指针或取消引用的指针的类型具有不同的大小或符号。将两者转换到同一事物应该可以工作:
while(addr != (unsigned char*)0xfef)
{
temp = *addr;
*addr = 0xAA;
if(*addr != (unsigned char)0xAA)
while(1);
*addr = temp;
++addr;
}
关于c - 如何更改连续地址的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49682418/