#include<reg52.h>
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
void delayms(unsigned int x);
void matrixkeycan();
void display(unsigned char num);
unsigned char code table[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,0x00
};//共阳级数码管字符表
void main()
{
led1=0;
led2=0;
led3=0;
led4=0;//打开4个数码管
while(1)
{
matrixkeycan();
}
}
void delayms(unsigned int x)//延时程序
{
unsigned i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void display(unsigned char num)
{
P0=table[num];
}//显示程序
void matrixkeycan()
{
unsigned char temp,key;
P1=0x3e;//确定行,111110,使行低电平
temp=P1;
temp=temp&0x38;//与111000与运算,如果按键按下,111000会变化,否则不变
if(temp!=0x38)
{
delayms(10);//去抖动
temp=P1;
temp=temp&0x38;
if(temp!=0x38)//按键按下
{
temp=P1;
switch(temp)
{
case 0x1e:
key=0;
break;
case 0x2e:
key=1;
break;
case 0x36:
key=2;
break;//分别确定3列
}
while(temp!=0x38)
{
temp=P1;
temp=temp&0x38;
}
}
display(key);
}
P1=0x3d;
temp=P1;
temp=temp&0x38;
if(temp!=0x38)
{
temp=P1;
switch(temp)
{
case 0x1d:
key=3;
break;
case 0x2d:
key=4;
break;
case 0x35:
key=5;
break;
}
while(temp!=0x38)
{
temp=P1;
temp=temp&0x38;
}
display(key);
}
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
P1=0x3b;
temp=P1;
temp=temp&0x38;
if(temp!=0x38)
{
delayms(10);
temp=P1;
temp=temp&0x38;
if(temp!=0x38)
{
temp=P1;
switch(temp)
{
case 0x33:
key=8;
break;
case 0x2b:
key=7;
break;
case 0x1b:
key=6;
break;
}
}
while(temp!=0x38)
{
temp=P1;
temp=temp&0x38;
}
display(key);
}
文章评论(0条评论)
登录后参与讨论