第10課 鍵盤檢測與應用
例1 按鍵檢測程序
//功能:當按下S8鍵時,L1燈發光,松手后,L1燈熄滅。相應程序如例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);//消除按下時的抖動
if(S1==0)
{
D1=0;
while(!S1);//松手檢測
delay(20);//消除松手時的抖動
}
else
D1=1;
}
}
例2 按鍵檢測程序
//功能:當每按下S8鍵時,數碼管自動加1,當加到F時又從零開始。
#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);//消除按下時的抖動
if(S1==0)//
{
temp++;
if(temp==16)temp=0;
}
while(!S1);//松手檢測,防止temp一次多加
delay(20);//消除松手時的抖動
評論