我剛學習單片機
遇到了幾個小問題想不明白
請各位大蝦指教
請知道的大手
把回答發上來供新手學習
鍵控流水燈程序
#include "reg51.h"
#include "intrins.h"
#define uchar unsigned char
void mDelay(unsigned int DelayTime)
{ unsigned int j=0;
for(;DelayTime>0;DelayTime--)
{ for(j=0;j<125;j++)
{;} }}
uchar Key()
{ uchar KeyV;
uchar tmp;
P3=P3|0x3c; // 四個鍵所接位置
KeyV=P3; 一
if((KeyV|0xc3)==0xff) //無鍵按下
return(0);
mDelay(10); //延時,去鍵抖
KeyV=P3;
if((KeyV|0xc3)==0xff)
return(0);
else
{ for(;;){ tmp=P3; 二
if((tmp|0xc3)==0xff)
break;}
return(KeyV);}}
void main()
{ unsigned char OutData=0xfe;
bit UpDown=0;
bit Start=0;
uchar KValue;
for(;;)
{ KValue=Key();
switch (KValue)
{ case 0xfb: //P3.2=0,Start
{ Start=1;
break; }
case 0xf7: //P3.3=0,Stop
{ Start=0;
break; }
case 0xef: //P3.4=0 Up
{ UpDown=1;
break; }
case 0xdf: //P3.5=0 Down
{ UpDown=0;
break; }
}
if(Start)
{ if(UpDown)
OutData=_crol_(OutData,1);
else
OutData=_cror_(OutData,1); P1=OutData;
}
else
P1=0xff; //否則燈全滅
mDelay(1000);
}
}
問題一處
1 為何
P3=P3|0x3c; // 四個鍵所接位置
KeyV=P3;
if((KeyV|0xc3)==0xff) //無鍵按下
用這個來確認鍵是否被按下
2是否在
KeyV=P3; 后
程序會掃描端口
若不是 什么時候掃描端口
3 mDelay(10); //延時,去鍵抖
是什么意思
問題二處
for(;;)不加行不行?不行去掉會怎么樣?
歡迎光臨 (http://m.zg4o1577.cn/bbs/) | Powered by Discuz! X3.1 |