这是我以前写的矩阵键盘数码管显示程序,很容易看懂的。
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};//共阴代码
uchar code bite[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //数码管位选代码
uchar key=16;
void show(uchar date,uchar num)
{
P0=tab[date];
P1=bite[num];
}
uchar Tkey()
{
uchar readkey;
uchar x_temp,y_temp;
P2=0x0f;
x_temp=P2&0x0f;
if(x_temp==0x0f) goto keyout;
P2=0xf0;
y_temp=P2&0xf0;
readkey=x_temp|y_temp;
readkey=~readkey;
switch(readkey)
{
case 0x11:key=0; break;
case 0x21:key=1; break;
case 0x41:key=2; break;
case 0x81:key=3; break;
case 0x12:key=4; break;
case 0x22:key=5; break;
case 0x42:key=6; break;
case 0x82:key=7; break;
case 0x14:key=8; break;
case 0x24:key=9; break;
case 0x44:key=10;break;
case 0x84:key=11;break;
case 0x18:key=12;break;
case 0x28:key=13;break;
case 0x48:key=14;break;
case 0x88:key=15;break;
default: key=16;break;
}
keyout:_nop_();
return key;
}
void main()
{
while(1)
{
key=Tkey();
show(key,0);
}
}