gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:37:31 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com