Microchip C18 - 奇怪的代码行为(可能与扩展模式/非扩展模式相关)

时间:2012-01-28 17:40:12

标签: c microchip pic18

我对PIC18F67J60的Microchip C18编译器有这个奇怪的问题。

我创建了一个非常简单的函数,它应该在一个更大的String中返回一个Sub-String的索引。

我不知道什么是错的,但是行为似乎与启用或不启用扩展模式有关。

在MPLAB.X中启用扩展模式后,我得到:

  • memcmppgm2ram函数始终返回零。

在MPLAB.X中禁用扩展模式后,我得到:

  • 迭代器变量i的值计为:0, 1, 3, 7, 15, 21

我在想一些堆栈问题或其他什么,因为这真的很奇怪。 完整的代码如下所示。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char bigString[] = "this is a big string";

unsigned char findSubStr(char *str, const rom char *subStr, unsigned char n, unsigned char m)
{
    unsigned char i;

    for (i=0; i < n-m; i++)
    {
        if(0 == memcmppgm2ram(&str[i], (const far rom void*)subStr, m))
            return i;
    }
    return n; // not found
}

void main(void)
{
    char n;

    n = findSubStr(bigString, (const rom void*)"big", sizeof(bigString), 3); 
}

1 个答案:

答案 0 :(得分:0)

memcmppgm2ram()期望指向数据存储器(ram)的指针作为其第一个参数。您正在将指针传递给字符串文字,该字符串文字位于程序存储器(ROM)中。

您可以改为使用memcmppgm(),或使用memcpypgm2ram()strcpypgm2ram()将其他字符串复制到ram。

不幸的是我无法对此进行测试,因为我目前无法访问此编译器。