/*程序名:矩阵键盘显示*/
/*程序功能:用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,temp1,temp2;
uchar out;
P1=0xf0;
temp1=P1&0xf0;
P1=0x0f;
temp2=P1&0x0f;
temp="temp1|temp2";
if(temp!=0xff)
{
delay();
P1=0xf0;
temp1=P1&0xf0;
P1=0x0f;
temp2=P1&0x0f;
temp="temp1|temp2";
if(temp!=0xff)
{
switch(temp)
{
case 0xee:out=0;break;
case 0xed:out=1;break;
case 0xeb:out=2;break;
case 0xe7:out=3;break;
case 0xde:out=4;break;
case 0xdd:out=5;break;
case 0xdb:out=6;break;
case 0xd7:out=7;break;
case 0xbe:out=8;break;
case 0xbd:out=9;break;
case 0xbb:out=10;break;
case 0xb7:out=11;break;
case 0x7e:out=12;break;
case 0x7d:out=13;;break;
case 0x7b:out=14;break;
case 0x77:out=15;break;
default:out=16;break;
}
return out;
}
else
{
out="16";
return out;
}
}
else
{
out="16";
return out;
}
}
int main(void)//主程序
{
for(;;)
{
P0=led[key()];
}
}
文章评论(0条评论)
登录后参与讨论