请教一个单片机C语言语法问题!

来源:百度知道 编辑:UC知道 时间:2024/06/22 02:10:58
一个简单的程序,但用keil编译时出现这样的错误提示:
Build target 'Target 1'
assembling STARTUP.A51...
compiling main.c...
MAIN.C(28): error C141: syntax error near 'else'
Target not created
程序如下:
#include"reg52.h"
#include"intrins.h"
#define uint unsigned int
#define uchar unsigned char
uint times=0;
void timer0() interrupt 1
{
TL0=(65526-50000)%256;
TH0=(65536-50000)/256;
times++;
}
void main()
{
uchar temp;
TMOD=0x0;
TL0=(65526-50000)%256;
TH0=(65536-50000)/256;
EA=1;
ET0=1;
TR0=1;
temp=0xfe;
P2=0xfe;
while(1)
{
if(times==20)
temp=_crol_(temp,1);
P2=temp;
else if(times==30)
{
P2=0xff;
times=0;
}
}
}

#incl

while(1)
{
if(times==20)
temp=_crol_(temp,1);
P2=temp; //这里加上大括号
else if(times==30)
{
P2=0xff;
times=0;
}
}
改完后变成

if(times==20)
{
temp=_crol_(temp,1);
P2=temp; //这里加上大括号
}
else if(times==30)
{
P2=0xff;
times=0;
}

程序是不允许

if
{
}
p0=0xf0;
else if
{
}
这种形式的

是这样,else 的上一条语句必须是 if 或者 else

比如
if()
{
}
else
{
}

或者
if()
{
}
else if()
{
}
else
{
}

而你的else上面那条语句是独立的,不是if包含的,加个大括号,或者调整一下语序!!

首先你的工程没有建对,重新建一个工程试试,TMOD=0x0;这个计时器的工作方式定义错了