c - 为什么我的开关盒不工作?/如何修复这些构建错误?

标签 c build enums switch-statement

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

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

它给出的错误是:

构建目标“目标 1”

正在编译main.c...

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

main.c(11): 错误 C53: 重新定义“状态”

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

    ...
}

关于c - 为什么我的开关盒不工作?/如何修复这些构建错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32425499/

相关文章:

swift - 枚举 if-else 比较

c - 一些 pthread 代码......为什么它运行缓慢?

c - 未定义的函数引用和 ld 返回 1 退出状态错误。没有语法错误。

c - 用 C 读取 PGM 图像

c - 从文件中解析 http 响应

java - 将同一个包中的类构建到单独的 jar 中

c# - 如何检查至少一个枚举值是否等于 if 条件中的变量

build - Hudson 定期构建

android:exported 需要为元素 <service#org.apache.cordova.firebase.FirebasePluginMessagingService> 明确指定

json - 使用 JSON 序列化/反序列化 Dart 的新枚举的最佳方法是什么?