#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
delay(uint t);
sbit P17=P1^7; //设置发光管的开关
main()
{
P17=0;
uchar k=0; //设置流水灯的方向标志位
uchar leds=0x01;
P0=~leds;
while(1)
{
delay(1000);
if(k==0&&leds==0x80)
{
k=1;
}
else if(k==1&&leds==0x01)
{
k=0;
}
if(k==0)
{
leds=leds<<1;
P0=~leds;
}
else
{
leds=leds>>1;
P0=~leds;
}
}
}
delay(uint t)
{
uint i,j;
for(i=0;1<t;i++);
for(j=0;j<223;j++);
}
就这个简单流水灯,为什么编译老是说有K,leds没有被定义,编译无法通过,试着调整摆放P17的位置,却能编译成功,这到底为什么啊?谁能给个解释啊?
#include <at89x51.h>
#define uchar unsigned char
#define uint unsigned int
uchar k,leds;
delay(uint t);
sbit P17=P1^7; //设置发光管的开关
main()
{
P17=0;
k=0; //设置流水灯的方向标志位
leds=0x01;
P0=~leds;
while(1)
{
delay(1000);
if((k==0)&&(leds==0x80))
{
k=1;
}
else if((k==1)&&(leds==0x01))
{
k=0;
}
if(k==0)
{
leds=leds<<1;
P0=~leds;
}
else
{
leds=leds>>1;
P0=~leds;
}
}
}
delay(uint t)
{
uint i,j;
for(i=0;1<t;i++);
for(j=0;j<223;j++);
}
上面的〈at89x51.h>是我随便改的,和错误没关系。
把定义放在main前面时,定义的量在全局有效
在函数中,变量的定义必须放在执行语句前面!
把定义部分应该放在主函数MAIN之前吧,全局变量
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
delay(uint t); //什么意思??
sbit P17=P1^7;
uchar k,leds;
main()
{
P17=0;
leds=0x01;
P0=~leds;
while(1)
}
P17=0;赋值语句 unchar leds;变量定义
变量定义放在后面肯定不能通过编译。
main里面的
P17=0;是赋值语句
肯定要放在
uchar k=0; //设置流水灯的方向标志位
uchar leds=0x01;
之后了,这里没有什么全局变量不全局变量的问题
“为什么编译老是说有K,leds没有被定义”
我也遇到过这样的问题,不过现在不会这样的了。
定义变量和常量一定要在要定义,再写执行语句,否则不可能编译通过
!
把:
P17=0;
uchar k=0; //设置流水灯的方向标志位
uchar leds=0x01;
改为
uchar k=0; //设置流水灯的方向标志位
uchar leds=0x01;
P17=0;
应该可以的了。
欢迎光临 (http://www.51hei.com/bbs/) | Powered by Discuz! X3.1 |