#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://m.zg4o1577.cn/bbs/) | Powered by Discuz! X3.1 |