51单片机,4*4矩阵键盘,八位数码管,仿真实例可以参考一下。
#include<reg51.h>
#define uchar unsigned char
uchar temp;
int key1,key,disbuf;// 此表为 LED 的字模 0 1 2 3 4 5 6 7 8 9 a b c d e f
unsigned char code LED7Code[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned char ledx[8];
bit s0,s1;
void delay(uchar z)
{
uchar i,j;
for(i=0;i<120;i++)
for(j=0;j<z;j++);
}
void scan() //要是按键了,扫描键盘编码值
{
P1=0xF0;
delay(1);
temp=P1;
switch(temp)
{
case 0xe0: key1=0;
break;
case 0xd0: key1=1;
break;
case 0xb0: key1=2;
break;
case 0x70: key1=3;
break;
}
P1=0x0f;
delay(1);
temp=P1;
switch(temp)
{
case 0x0E: key=key1+0;
break;
case 0x0D: key=key1+4;
break;
case 0x0B: key=key1+8;
break;
case 0x07: key=key1+12;
break;
default : key=-1;
}
if((key1+1)&&(key+1)) disbuf=key;
}
void ejjc() //判断是否按键
{
P1=0xF0;
if(P1!=0xF0) { scan();s0=1;}
else { s0=0; s1=1;}
}
void main()
{
uchar i;
while(1)
{
ejjc();
if(s0==1 && s1==1)
{
s0=0;s1=0;
for(i=0;i<8;i++)
{ ledx[i]=ledx[i+1]; ledx[8]=disbuf; }
}
P0=0xff;
P2=LED7Code[ledx[0]];
P0=0xfe;
delay(5);
P0=0xff;
P2=LED7Code[ledx[1]];
P0=0xfd;
delay(5);
P0=0xff;
P2=LED7Code[ledx[2]];
P0=0xfb;
delay(5);
P0=0xff;
P2=LED7Code[ledx[3]];
P0=0xf7;
delay(5);
P0=0xff;
P2=LED7Code[ledx[4]];
P0=0xef;
delay(5);
P0=0xff;
P2=LED7Code[ledx[5]];
P0=0xdf;
delay(5);
P0=0xff;
P2=LED7Code[ledx[6]];
P0=0xbf;
delay(5);
P0=0xff;
P2=LED7Code[ledx[7]];
P0=0x7f;
delay(5);
}
}
追问谢谢大佬,要是共阳数码管怎么改程序?