新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 第10課 鍵盤檢測(cè)與應(yīng)用

第10課 鍵盤檢測(cè)與應(yīng)用

作者: 時(shí)間:2016-12-01 來(lái)源:網(wǎng)絡(luò) 收藏
例1 按鍵檢測(cè)程序
//功能:當(dāng)按下S8鍵時(shí),L1燈發(fā)光,松手后,L1燈熄滅。相應(yīng)程序如例1.
#include
#define uint unsigned int
#define uchar unsigned char
sbit D1=P1^0;
sbit S1=P3^4;
void main()
{
P3=0xff;
while(1)
{
if(S1==0)
delay(20);//消除按下時(shí)的抖動(dòng)
if(S1==0)
{
D1=0;
while(!S1);//松手檢測(cè)
delay(20);//消除松手時(shí)的抖動(dòng)
}
else
D1=1;
}
}
例2 按鍵檢測(cè)程序
//功能:當(dāng)每按下S8鍵時(shí),數(shù)碼管自動(dòng)加1,當(dāng)加到F時(shí)又從零開始。
#include
#define uint unsigned int
#define uchar unsigned char
sbit duan=P2^6;
sbit wei=P2^7;
sbit S1=P3^4;
uchar temp;
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z )
{
uint x ,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
P3=0xff;
wei=1;
P0=0xfe;
wei=0;
temp=0;
while(1)
{
if(S1==0)
{
delay(20);//消除按下時(shí)的抖動(dòng)
if(S1==0)//
{
temp++;
if(temp==16)temp=0;
}
while(!S1);//松手檢測(cè),防止temp一次多加
delay(20);//消除松手時(shí)的抖動(dòng)
while(!S1);
}
duan=1;
P0=table[temp];
duan=0;
}
}
上一頁(yè) 1 2 3 下一頁(yè)

關(guān)鍵詞: 鍵盤檢測(cè)單片

評(píng)論


技術(shù)專區(qū)

關(guān)閉