变量被重置。不知道为什么

时间:2015-05-24 19:27:57

标签: c microcontroller avr

我有变量(选项),我在lcd上显示以查看值。当我按下按钮时,它应该将值更改为1.但它以其他方式工作。 (选项)仅在我按住按钮时为1,但是当我释放此按钮时,变量为0.我搜索了所有文件(选项),但它只在一个文件中,所有带有此变量的代码行都显示在此一段代码(有趣的(menuRun()))。

在menu.c中

void menuRun(char * Uline, char * Lline, uint8_t * x, uint8_t * y)
{
    uint8_t button;
    uint16_t value;
    char buffer[20];
    button = whichButton(buffer);
    value = getValue();

    if (menuX==0) /* <---- this is false when i am testing*/
    {
        cursorOn();
        if (menuY==0)
        {
            strncpy(bufferUline,"*",strlen("*"));
            strncpy(bufferLline," ",strlen(" "));
        } else
        {
            strncpy(bufferLline,"*",strlen("*"));
            strncpy(bufferUline," ",strlen(" "));
        }

        if (button == 1)
        {
            menuX = 1;
            cursorOff();
        }else if (button == 3)
        {
            menuY=0;
            strncpy(bufferUline,"*",strlen("*"));
            strncpy(bufferLline," ",strlen(" "));
        }else if (button == 4)
        {
            menuY=1;
            strncpy(bufferLline,"*",strlen("*"));
            strncpy(bufferUline," ",strlen(" "));
        }
    } else /* <---- this is true when i am testing*/
    {
        if (menuY & 1) /* <---- this is false when i am testing*/
        {
            if (option & 1)
            {
                x=6;
                strcpy(bufferLline,"      0000     ");
            }else
            {

            }
        }else /* <---- this is true when i am testing*/
        {
            /* This doesnt matter for now*/
            if (option&1)
            {
                strcpy(bufferUline,"    Nowy kod    ");
                //strcpy(bufferLline,"      xxxx     ");
            }else
            {
                strcpy(bufferUline," Nowa pora dnia ");
                //strcpy(bufferLline,"  xx:xx-xx:xx  ");
            }

            if (button == 2)
            {
                menuX=0;
                menuY=0;
            }
            else if (button == 3)
            {
                /*Here I change variable option, when button pressed*/
                option=1;
            }else if (button == 4)
            {
                //option=0;
            }

        }
    }

    //strcpy(bufferLline,itoa(option,buffer,10));

    //strncpy(bufferLline+5,itoa(menu,buffer,10),strlen(itoa(menu,buffer,10)));


    /* Here I copy value of variable "option" into bufferLline, which is display on lcd*/
    strncpy(bufferLline,itoa(option,buffer,10),strlen(itoa(option,buffer,10)));

    *x=menuX;
    *y=menuY;
    strcpy(Uline,bufferUline);
    strcpy(Lline,bufferLline);
}

在menu.h中

uint8_t menuX, menuY;
uint8_t menuChoice, menu, option;

在main.c中

int main(void)
{
    init();

    char buffer[11];

    correctCode = eeprom_read_word(&correctCodeEem);
    correctCode = 1234;

    counterClick=0;
    menuChoice=1;
    uint8_t x,y;
    while(1)
    {
            if (led & 1)
            {
                menuRun(upperLine,lowerLine,&x,&y);



                led=0;
            }
            if (lcd & 1)
            {
                //char upperBuffer[16], lowerBuffer[16];

                //strcpy(upperBuffer,upperLine);
                //strcpy(lowerBuffer,lowerLine);
                lcdDisplay(upperLine,lowerLine);
                lcdGoTo(x,y);
                lcd=0;
            }
    }

}

标志led和lcd由计时器设置,效果很好。

我不知道为什么(选项)被重置。当我按下按钮一次时,它应该(选项)设置为1。

1 个答案:

答案 0 :(得分:1)

我们不知道函数init(),但我们假设menuXmenuY已在那里正确初始化(否则它们将是随机的)。我们可以看到,至少menuChoice在不同的源代码文件中使用而不是在声明它的地方......这也可能是其他全局变量的情况(这就是声明它们的原因)全局)。

我建议声明这些变量volatile,以便编译器得到一个暗示,这些变量可能会在实际的编译单元之外进行更改,因此在优化时会更加小心,例如

volatile uint8_t menuX, menuY;
volatile uint8_t menuChoice, menu, option;

或者尝试完全关闭编译器优化(-O0

Further info