51单片机Keil C51变量定义后仍然报错’x’ undefined identifier

发布于 2022-03-12  43 次阅读


一、起因与源代码

有一段代码:

/**
 * @brief 显示指定数字
 *
 * @param numbers char[] / *char
 */
void showLargeNumber(const char *numbers)
{
    unsigned short int length = strlen(numbers);
    //八位数字显示
    if (length > 8)
    {
        length = 8;
    }
    // unsigned short int pointIndex = 10;
    enum SHOW_DP showDP = False;
    int i;

    //通过不断扫描显示多个数字
    for (i = 0; i < length; i++)
    {

        if (*numbers == '.')
        {
            showDP = True;
        }
        else
        {
            showDP = False;
        }
        showSingleNumber(i, charNumberToValue(*numbers), showDP);
        //短暂延时
        delay_us(100);
        //指针后移
        numbers++;
    }
}

语法都是正常的,但在Keil编译中一直出现如下错误:

compiling main.c...
compiling nixietube.c...
nixietube.c(126): error C141: syntax error near 'enum'
nixietube.c(126): error C202: 'SHOW_DP': undefined identifier
nixietube.c(127): error C141: syntax error near 'int'
nixietube.c(127): error C202: 'i': undefined identifier
nixietube.c(130): error C202: 'i': undefined identifier
nixietube.c(135): error C202: 'showDP': undefined identifier
nixietube.c(139): error C202: 'showDP': undefined identifier
nixietube.c(141): error C202: 'i': undefined identifier
Target not created.

二、解决

将变量声明放在函数的最开始位置预先定义...

/**
 * @brief 显示指定数字
 *
 * @param numbers char[] / *char
 */
void showLargeNumber(const char *numbers)
{
    unsigned short int length = strlen(numbers);
    // unsigned short int pointIndex = 10;
    enum SHOW_DP showDP = False;
    int i;
    //八位数字显示
    if (length > 8)
    {
        length = 8;
    }


    //通过不断扫描显示多个数字
    for (i = 0; i < length; i++)
    {

        if (*numbers == '.')
        {
            showDP = True;
        }
        else
        {
            showDP = False;
        }
        showSingleNumber(i, charNumberToValue(*numbers), showDP);
        //短暂延时
        delay_us(100);
        //指针后移
        numbers++;
    }
}

解决。

Program Size: data=46.0 xdata=0 code=940
creating hex file from ".\Objects\template"...
".\Objects\template" - 0 Error(s), 2 Warning(s).
Build Time Elapsed:  00:00:01

垃圾Keil C51,毁我青春,Debug半天结果竟然是因为IDE的原因。


网站的建设与维护者,孤独的守望者