新聞中心

89C52矩陣鍵盤的識別

作者: 時間:2016-11-24 來源:網(wǎng)絡(luò) 收藏

我所見過的矩陣鍵盤一般有兩種識別方法:掃描法和線反轉(zhuǎn)法。以4X4矩陣鍵盤為例,掃描法是依次將每一行置為0,然后讀取列的狀態(tài),這樣就可以通過四次掃描將整個矩陣鍵盤的按鍵狀況辨別出來,這個方法所需的代碼量比較長;而線反轉(zhuǎn)法則顯得比較簡潔,它通過先將4行全部置0(0xf0),然后讀列的狀態(tài),接著,將列全部置0(0x0f),讀行的狀態(tài),通過(行|列)就能確定整個行列的狀態(tài)了。掃描法比線反轉(zhuǎn)法占用更多的程序儲存空間(Program Space),但占用的更少的數(shù)據(jù)儲存空間(Data Space)。

本文引用地址:http://butianyuan.cn/article/201611/320852.htm


程序如下

掃描法:

#include
#define uchar unsigned char
#define uint unsigned int
uchar num,c;
uchar code SSEG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay1ms(uint z)
{
uint x;
uchar y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

void keyscan()
{
uchar temp;
P2=0xfe;
temp=P2&0xf0;
if(temp!=0xf0)
{
delay1ms(10);
temp=P2&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xee:num=0;break;
case 0xde:num=1;break;
case 0xbe:num=2;break;
case 0x7e:num=3;break;
}
while(temp!=0xf0)//松手檢測
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0xfd;
temp=P2&0xf0;
if(temp!=0xf0)
{
delay1ms(10);
temp=P2&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xed:num=4;break;
case 0xdd:num=5;break;
case 0xbd:num=6;break;
case 0x7d:num=7;break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0xfb;
temp=P2&0xf0;
if(temp!=0xf0)
{
delay1ms(10);
temp=P2&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xeb:num=8;break;
case 0xdb:num=9;break;
case 0xbb:num=10;break;
case 0x7b:num=11;break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0xf7;
temp=P2&0xf0;
if(temp!=0xf0)
{
delay1ms(10);
temp=P2&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xe7:num=12;break;
case 0xd7:num=13;break;
case 0xb7:num=14;break;
case 0x77:num=15;break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
}

void main()
{
num=0xff;
while(1)
{
keyscan();
P1=~SSEG[num];
}

}

線反轉(zhuǎn)法:

#include
#define uchar unsigned char
#define uint unsigned int
uchar num;
uchar code SSEG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay1ms(uint z)
{
uint x;
uchar y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

void keyscan()
{
uchar temp,z;
P2=0xf0;
if(P2!=0xf0)
{
delay1ms(10);
if(P2!=0xf0)
{
temp=P2;
P2=0x0f;
z=temp|P2;
temp=P2&0x0f;
while(temp!=0x0f)//松手檢測
{
temp=P2&0x0f;
}
switch(z)
{
case 0xee: num =0; break;
case 0xde: num =1; break;
case 0xbe: num =2; break;
case 0x7e: num =3; break;
case 0xed: num =4; break;
case 0xdd: num =5; break;
case 0xbd: num =6; break;
case 0x7d: num =7; break;
case 0xeb: num =8; break;
case 0xdb: num =9; break;
case 0xbb: num =10;break;
case 0x7b: num =11;break;
case 0xe7: num =12;break;
case 0xd7: num =13;break;
case 0xb7: num =14;break;
case 0x77: num =15;break;
}
}
}
}

void main()
{
num=0xff;
P2=0xff;
while(1)
{
keyscan();
P1=~SSEG[num];
}
}



關(guān)鍵詞: 89C52矩陣鍵盤識

評論


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

關(guān)閉