为什么我的 switch case 不工作?/我如何修复这些构建错误?

Why isn't my switch case working?/ How can I fix these build errors?

我是一名学生,我已经写出这段代码(任务涉及控制自动门)我以为我已经把所有东西都整理好了,但是当我尝试构建它时它给了我一些错误。如果有人能指出正确的方向来修复它们,那就太好了!

#include <reg167.h>
#define SensorsActive P2 & 0x0010 == 0x0010 || P2 & 0x0020 == 0x0020

/* Main program - Automatic doors */

int main(void)
{
    DP2 = 0x0003;
    /* sets bits 0,1 as outputs and the rest as inputs */ 
    ODP2 = 0x0000;
    /* selects output push/pull driver */
    enum {closed, opening, open, closing} state;
    /* define FSM states */
    state = 1;
    /* set starting state as door closed */

    for (;;) {
        switch (state) {
        case closed: /* Door closed */
            if (SensorsActive) {
            /* If motion sensors are activated switch to opening state */
                state = opening;                                        
            }
            break;      
        case opening: /* Door opening */
            P2 = P2 & ~0x0001;
            /* Sets direction bit to clockwise (opening) */
            P2 |= 0x0002;    /* Turns on motor control bit */
            if(P2&0x0004==0x0004)//door open limit switches triggered switch to open
                state = open;
            break;
        case open: /* Door open */
            timer();
            while (T3OTL != 1) ;
            if(SensorsActive) {
            /* If sensors are active break so that open case will be repeated and timer reset... */                                                         /* ...Else switch state to closing */
                    break;
            }
            else state = closing;
            break;
        case closing: /* Door closing */
            P2 |= 0x0001;// Sets direction bit to counterclockwise (closing) 
            P2 |= 0x0002;/* Turns on motor control bit */
            if(SensorsActive)/* If sensors are active start opening doors */ 
                state = opening;
            if(P2 & 0x0008 == 0x0008) {
            //If closed limit switches are reached switch state to closed
                    state = closed;
            }
            break;
        }
    }
}

它给出的错误是:

构建目标'Target 1'

正在编译main.c...

main.c(10):错误 C25:'enum'

附近的语法错误

main.c(11): 错误 C53: 'state'

的重新定义

main.c(13):错误 C25:'while'

附近的语法错误

main.c(13):错误 C25:“1”附近的语法错误

main.c(16): 错误 C103: 'state': 非法函数定义(缺少 ';' ?)

未创建目标。

看来你是在C89模式下编译,你不能在任何地方声明变量(它是在C99标准中添加的)。将 state 声明移动到函数的顶部。

或者,告诉编译器使用 C99 规则。对于 GCC,这可以通过在命令行中添加 -std=c99 选项来完成。


现在你的代码看起来像这样(我添加的评论):

void main(void) {
    DP2 = 0x0003;  /* Assignment, okay */
    ODP2 = 0x0000; /* Another assignment, okay */
    enum {closed, opening, open, closing} state;  /* Variable declaration, NOT okay */
    state = 1; /* Assignment, okay */

    ...
}

您需要在函数中声明变量:

void main(void) {
    enum {closed, opening, open, closing} state;  /* Variable declaration, okay here */

    DP2 = 0x0003;  /* Assignment, okay */
    ODP2 = 0x0000; /* Another assignment, okay */
    state = 1; /* Assignment, okay */

    ...
}