【问题标题】:How can I change memory at consecutive addresses?如何更改连续地址的内存?
【发布时间】:2018-04-05 22:21:18
【问题描述】:

我正在尝试通过运行一系列地址来测试我正在使用的设备的 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] compatible scalar operands required for comparison”。

我也尝试使用我想要的地址值创建指针,但尝试将指针设置为另一个指针会产生错误,“错误 [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。

【问题讨论】:

  • while(addr != 0xfef) -- 除非sizeof(int) = 1,否则永远不会评估为真。尝试将 addr 声明为指向 unsigned char 的指针。并将if(addr != 0xAA) 更改为if(*addr != 0xAA)
  • 我猜你的意思是 while (*addr != 0xAA)...
  • 我确实是这个意思,实际上我最初将 addr 作为 char 指针,但在尝试使代码正常工作时更改了它。感谢您的位置。
  • 只是一个问题,在正常情况下,并非每个 RAM 单元都可以访问。你检查过你的数据表吗?您是否尝试访问正常的位置?另外,你的设备是什么?也许这些信息会让人们更好地帮助你。

标签: c pointers memory comparison mplab


【解决方案1】:

您可能会遇到类型冲突,其中常量的类型是int,而指针的类型或取消引用的指针的大小或符号不同。将两者都投射到同一个东西上应该可以工作:

while(addr != (unsigned char*)0xfef) 
{
    temp = *addr; 
    *addr = 0xAA; 
    if(*addr != (unsigned char)0xAA) 
        while(1);
    *addr = temp; 
    ++addr; 
}

【讨论】:

  • 感谢您的建议。 if(*addr != (unsigned char)0xAA) 实际上会导致有符号到无符号的警告,可能是因为启用了整数提升。
  • @SamsquanchIsReal 是的,这是因为常量的默认类型是有符号整数。如果您也想摆脱警告,您可以通过使用变量而不是常量来进行比较:unsigned char my_val = (unsigned char)0xAA;,然后:if(*addr != my_val)
【解决方案2】:

感谢您的帮助。原来我看错了记忆,这就是为什么我没有看到任何变化。

在发现代码正常工作后我遇到的另一个问题是使用特殊功能寄存器时不存在的问题。指针和变量被覆盖,导致测试失败(或进入 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
  }
}

【讨论】:

    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多