14. 4×4矩陣式鍵盤識(shí)別技術(shù)
如圖4.14.2所示,用AT89S51的并行口P1接4×4矩陣鍵盤,以P1.0-P1.3作輸入線,以P1.4-P1.7作輸出線;在數(shù)碼管上顯示每個(gè)按鍵的“0-F”序號(hào)。對(duì)應(yīng)的按鍵的序號(hào)排列如圖4.14.1所示
(1.把“單片機(jī)系統(tǒng)“區(qū)域中的P3.0-P3.7端口用8芯排線連接到“4X4行列式鍵盤”區(qū)域中的C1-C4R1-R4端口上;
(2.把“單片機(jī)系統(tǒng)”區(qū)域中的P0.0/AD0-P0.7/AD7端口用8芯排線連接到“四路靜態(tài)數(shù)碼顯示模塊”區(qū)域中的任一個(gè)a-h(huán)端口上;要求:P0.0/AD0對(duì)應(yīng)著a,P0.1/AD1對(duì)應(yīng)著b,……,P0.7/AD7對(duì)應(yīng)著h。
4.
(1.4×4矩陣鍵盤識(shí)別處理
(2.每個(gè)按鍵有它的行值和列值,行值和列值的組合就是識(shí)別這個(gè)按鍵的編碼。矩陣的行線和列線分別通過兩并行接口和CPU通信。每個(gè)按鍵的狀態(tài)同樣需變成數(shù)字量“0”和“1”,開關(guān)的一端(列線)通過電阻接VCC,而接地是通過程序輸出數(shù)字“0”實(shí)現(xiàn)的。鍵盤處理程序的任務(wù)是:確定有無鍵按下,判斷哪一個(gè)鍵按下,鍵的功能是什么;還要消除按鍵在閉合或斷開時(shí)的抖動(dòng)。兩個(gè)并行口中,一個(gè)輸出掃描碼,使按鍵逐行動(dòng)態(tài)接地,另一個(gè)并行口輸入按鍵狀態(tài),由行掃描值和回饋信號(hào)共同形成鍵編碼而識(shí)別按鍵,通過軟件查表,查出該鍵的功能。
5.
P3=FFH,P3.0=0
P3=FFH,P3.1=0
P3=FFH,P3.2=0
P3=FFH,P3.3=0
KEYBUFEQU30H
ORG00H
START:MOVKEYBUF,#2
WAIT:
MOVP3,#0FFH
CLRP3.4
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY1
LCALLDELY10MS
MOVA,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A,@A+DPTR
MOVP0,A
DK3A:MOVA,P3
ANLA,#0FH
XRLA,#0FH
JNZDK3A
NOKEY3:
MOVP3,#0FFH
CLRP3.7
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY4
LCALLDELY10MS
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY4
MOVA,P3
ANLA,#0FH
CJNEA,#0EH,NK13
MOVKEYBUF,#12
LJMPDK4
NK13:CJNEA,#0DH,NK14
MOVKEYBUF,#13
LJMPDK4
NK14:CJNEA,#0BH,NK15
MOVKEYBUF,#14
LJMPDK4
NK15:CJNEA,#07H,NK16
MOVKEYBUF,#15
LJMPDK4
NK16:NOP
DK4:
MOVA,KEYBUF
MOVDPTR,#TABLE
MOVCA,@A+DPTR
MOVP0,A
DK4A:MOVA,P3
ANLA,#0FH
XRLA,#0FH
JNZDK4A
NOKEY4:
LJMPWAIT
DELY10MS:
MOVR6,#10
D1:MOVR7,#248
DJNZR7,$
DJNZR6,D1
RET
TABLE:DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H
DB7FH,6FH,77H,7CH,39H,5EH,79H,71H
END
7.
#include
unsignedcharcodetable[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
unsignedchartemp;
unsignedcharkey;
unsignedchari,j;
voidmain(void)
{
while(1)
{
P3=0xff;
P3_4=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=7;
break;
case0x0d:
key=8;
break;
case0x0b:
key=9;
break;
case0x07:
key=10;
break;
}
temp=P3;
P1_0=~P1_0;
P0=table[key];
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P3=0xff;
P3_5=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=4;
break;
case0x0d:
key=5;
break;
case0x0b:
key=6;
break;
case0x07:
key=11;
break;
}
temp=P3;
P1_0=~P1_0;
P0=table[key];
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P3=0xff;
P3_6=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=1;
break;
case0x0d:
key=2;
break;
case0x0b:
key=3;
break;
case0x07:
key=12;
break;
}
temp=P3;
P1_0=~P1_0;
P0=table[key];
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P3=0xff;
P3_7=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=0;
break;
case0x0d:
key=13;
break;
case0x0b:
key=14;
break;
case0x07:
key=15;
break;
}
temp=P3;
P1_0=~P1_0;
P0=table[key];
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
}
}
評(píng)論