功能与前面程序相同,但上个程序采用了线反转扫描方式,该程序采用了行列扫描方式。
/*程序名:矩阵键盘显示*/
/*程序功能:用P1口构成矩阵键盘,P0口作显示,按下按键显示对应按键码*/
/*作者:POLO*/
#include<reg51.h>
typedef unsigned char uchar;
uchar code led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};//共阴极LED字段码
void delay(void)//延时子程序
{
uchar i,j;
for(i=0;i<=25;i++)
for(j=0;j<=200;j++);
}
uchar key()//行列扫描法键盘处理程序
{
uchar temp;
P1=0xf0;
temp="P1"^0xf0;
if(temp!=0x00)
{
delay();//延时消抖
P1=0xf0;
temp="P1"^0xf0;
if(temp!=0x00)
{
P1=0xfe;
temp="P1"&0xf0;
switch(temp)
{
case 0xe0:return 0;break;
case 0xd0:return 1;break;
case 0xb0:return 2;break;
case 0x70:return 3;break;
default:break;
}
P1=0xfd;
temp="P1"&0xf0;
switch(temp)
{
case 0xe0:return 4;break;
case 0xd0:return 5;break;
case 0xb0:return 6;break;
case 0x70:return 7;break;
default:break;
}
P1=0xfb;
temp="P1"&0xf0;
switch(temp)
{
case 0xe0:return 8;break;
case 0xd0:return 9;break;
case 0xb0:return 10;break;
case 0x70:return 11;break;
default:break;
}
P1=0xf7;
temp="P1"&0xf0;
switch(temp)
{
case 0xe0:return 12;break;
case 0xd0:return 13;break;
case 0xb0:return 14;break;
case 0x70:return 15;break;
default:break;
}
}
}
return 16;
}
int main(void)//主程序
{
for(;;)
{
P0=led[key()];
}
}
文章评论(0条评论)
登录后参与讨论