我试图通过运行一系列地址来测试我正在处理的设备的RAM,比如说0x0到0xfef。
我尝试了很多东西,但没有任何效果。这是我想要做的一个例子:
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语句都给出了错误," Error [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。
答案 0 :(得分:1)
您可能会遇到类型冲突,其常量类型为int
,指针类型或取消引用指针的大小或符号不同。将两者都投射到相同的东西应该有效:
while(addr != (unsigned char*)0xfef)
{
temp = *addr;
*addr = 0xAA;
if(*addr != (unsigned char)0xAA)
while(1);
*addr = temp;
++addr;
}
答案 1 :(得分:0)
感谢您的帮助。事实证明我在寻找错误的记忆,这就是为什么我没有看到任何变化。
在发现代码之后遇到的另一个问题是使用特殊功能寄存器时出现的问题和问题。指针和变量被覆盖,导致测试失败(或进入while循环)。
我通过使用两个指针和两个临时值来修复此问题,并检查会破坏代码正常功能的特定条件:
unsigned char temp, temp2;
unsigned char * addr, * addr2;
addr = (unsigned char*) 0x0; // set addr to start of ram
//continue until at end of usable memory
while(addr != (unsigned char*) 0xfef)
{
//if not testing address of 'addr' pointer, continue testing byte by byte
if((int) addr != (int) &addr)
{
//if 'addr' is pointing to location in ram where 'temp' is being stored,
//use 'temp2' to hold the old value instead, since it has a dif address
if(&temp != addr)
temp = *addr; //copy value out of address
else
temp2 = *addr; //copy value out of address
*addr = 0xAA; //set RAM value at 'addr' to 0xAA
//if value at 'addr' equals 0xAA then continue test
if(*addr != 0xAA)
//if value is not equal to 0xAA, then hang forever
while(1);
//if 'addr' is pointing to location in ram where 'temp' is being stored,
//pull old value from 'temp2' since that is where it is stored
if(&temp != addr)
*addr = temp; //copy old value back to address
else
*addr = temp2; //copy old value back to address
++addr; //move on to the next byte in memory
}
//if testing the address of the 'addr' pointer, then perform same test
//differently by setting both bytes of the pointer to 0xAA, and then
//validating that both bytes are what they should be, before returning the
//'addr' pointer back to its original value.
else
{
addr2 = addr; //save the position of the ram test
//set RAM value at addr to 0xAAAA to test both bytes of ptr
addr = (char*) 0xAAAA;
//check to ensure bytes in memory changed like they should have
if(addr != (char*) 0xAAAA )
//if both bytes are not equal to 0xAAAA, then hang forever
while(1);
addr = addr2 + 2; //return to correct position in memory to continue test
//move two bytes forward since two bytes were tested
}
}