如何實(shí)現(xiàn)矩陣鍵盤自由靈活控制數(shù)碼管顯示數(shù)字
#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; // 啟動(dòng)定時(shí)器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; // 停止定時(shí)器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++;
}
評(píng)論