矩阵键盘 能完成 多个按键 衔接少数I/O口, 矩阵键盘,按下一个键 即可 使 数码管显现相应数字
#include
unsigned char code table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x10};
unsigned char t=0;
unsigned char i=0;
void Delay(unsigned char i)
{
unsigned char j;
for(; i>0; i–)
for(j=110; j>0; j–);
}
void DisplayLED(unsigned char n)
{
P0=table[n];
}
void Keycan() //判别 矩阵键盘是否被按下
{
unsigned char tmp, key;
P2=0xfe; //第四列
tmp=P2;
tmp=tmp&0xf0;
if(tmp!=0xf0)
{
Delay(10); //
tmp=P2;
tmp=tmp&0xf0;
if(tmp!=0xf0)
{
tmp=P2;
switch(tmp)
{
case 0xee: TR0=1; // 发动定时器T0
break;
case 0xde: key=8; //
break;
case 0xbe: key=4; /
break;
}
while(tmp!=0xf0)
{
tmp=P2;
tmp=tmp&0xf0;
}
if(tmp!=0xee)
{
DisplayLED(key);
}
}
}
P2=0xfd; //第三列
tmp=P2;
tmp=tmp&0xf0;
if(tmp!=0xf0)
{
Delay(10); //
tmp=P2;
tmp=tmp&0xf0;
if(tmp!=0xf0)
{
tmp=P2;
switch(tmp)
{
case 0xed: TR0=0, key=i; // 中止定时器T0
break;
case 0xdd: key=7; //
break;
case 0xbd: key=3; //
break;
}
while(tmp!=0xf0)
{
tmp=P2;
tmp=tmp&0xf0;
}
DisplayLED(key);
}
}
P2=0xfb; //第二列
tmp=P2;
tmp=tmp&0xf0;
if(tmp!=0xf0)
{
Delay(10); //
tmp=P2;
tmp=tmp&0xf0;
if(tmp!=0xf0)
{
tmp=P2;
switch(tmp)
{
case 0xeb: key=0 ; //
break;
case 0xdb: key=6; //
break;
case 0xbb: key=2; //
break;
}
while(tmp!=0xf0)
{
tmp=P2;
tmp=tmp&0xf0;
}
DisplayLED(key);
}
}
P2=0xf7; //榜首列
tmp=P2;
tmp=tmp&0xf0;
if(tmp!=0xf0)
{
Delay(10); //
tmp=P2;
tmp=tmp&0xf0;
if(tmp!=0xf0)
{
tmp=P2;
switch(tmp)
{
case 0xe7: key=9 ; //
break;
case 0xd7: key=5; /
break;
case 0xb7: key=1; //
break;
}
while(tmp!=0xf0)
{
tmp=P2;
tmp=tmp&0xf0;
}
DisplayLED(key);
}
}
}
void main()
{
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
while(1)
{
Keycan();
if(t==20)
{
t=0;
P0=table[i];
Delay(1000);
i++;
if(i==10)
{
i=0;
}
}
}
}
void TimerLED() interrupt 1 using 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t++;
}