简体   繁体   English

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

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

I'm a student and I've written out this code (the task involves controlling an automatic door) I thought I had everything sorted but when I try to build it it gives me a few errors.我是一名学生,我已经写出了这段代码(任务涉及控制自动门)我以为我已经把所有东西都整理好了,但是当我尝试构建它时,它给了我一些错误。 If someone could point me in the right direction to fixing them that would be great!如果有人能指出我修复它们的正确方向,那就太好了!

#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;
        }
    }
}

The errors it is giving are:它给出的错误是:

Build target 'Target 1'构建目标“目标 1”

compiling main.c...编译 main.c...

main.c(10): error C25: syntax error near 'enum' main.c(10): 错误 C25: 'enum' 附近的语法错误

main.c(11): error C53: redefinition of 'state' main.c(11):错误 C53:重新定义“状态”

main.c(13): error C25: syntax error near 'while' main.c(13): 错误 C25: 'while' 附近的语法错误

main.c(13): error C25: syntax error near '1' main.c(13): 错误 C25: '1' 附近的语法错误

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

Target not created.目标未创建。

It seems you are compiling in C89 mode, where you can't have variable declarations anywhere (it was added in the C99 standard).看来您是在 C89 模式下编译的,在这种模式下,您不能在任何地方进行变量声明(它已添加到 C99 标准中)。 Move the state declaration to the top of the function.state声明移到函数的顶部。

Alternatively, tell the compiler to use the C99 rules.或者,告诉编译器使用 C99 规则。 For GCC this can be done by adding the -std=c99 option to the command line.对于 GCC,这可以通过在命令行中添加-std=c99选项来完成。


Right now your code looks like this (comments added by me):现在你的代码看起来像这样(我添加的评论):

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 */

    ...
}

You need to put the variable declarations first in the function:您需要先将变量声明放在函数中:

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 */

    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM