1.
如图4.14.2所示,用AT89S51的并行口P1接4×4矩阵键盘,以P1.0-P1.3作输入线,以P1.4-P1.7作输出线;在数码管上显现每个按键的“0-F”序号。对应的按键的序号摆放如图4.14.1所示
(1.把“单片机体系“区域中的P3.0-P3.7端口用8芯排线连接到“4X4行列式键盘”区域中的C1-C4R1-R4端口上;
(2.把“单片机体系”区域中的P0.0/AD0-P0.7/AD7端口用8芯排线连接到“四路静态数码显现模块”区域中的任一个a-h端口上;要求:P0.0/AD0对应着a,P0.1/AD1对应着b,……,P0.7/AD7对应着h。
4.
(1.4×4矩阵键盘辨认处理
(2.每个按键有它的行值和列值,行值和列值的组合便是辨认这个按键的编码。矩阵的行线和列线别离经过两并行接口和CPU通讯。每个按键的状况相同需变成数字量“0”和“1”,开关的一端(列线)经过电阻接VCC,而接地是经过程序输出数字“0”完成的。键盘处理程序的使命是:确认有无键按下,判别哪一个键按下,键的功用是什么;还要消除按键在闭合或断开时的颤动。两个并行口中,一个输出扫描码,使按键逐行动态接地,另一个并行口输入按键状况,由行扫描值和回馈信号一起构成键编码而辨认按键,经过软件查表,查出该键的功用。
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,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY1
MOVA,P3
ANLA,#0FH
CJNEA,#0EH,NK1
MOVKEYBUF,#0
LJMPDK1
NK1:CJNEA,#0DH,NK2
MOVKEYBUF,#1
LJMPDK1
NK2:CJNEA,#0BH,NK3
MOVKEYBUF,#2
LJMPDK1
NK3:CJNEA,#07H,NK4
MOVKEYBUF,#3
LJMPDK1
NK4:NOP
DK1:
MOVA,KEYBUF
MOVDPTR,#TABLE
MOVCA,@A+DPTR
MOVP0,A
DK1A:MOVA,P3
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,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;
}
}
}
}
}