keil uVision4界面:
#include<reg51.h>
unsigned char key;
unsigned char seg1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阳极数码管0至9
unsigned char seg2[]={0x92,0xa4,0xc0,0xf9,0xb0,0xf9,0x99};//共阳极数码管5、2、0、1、3、1、4
unsigned char seg3[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //8只流水灯上往下
unsigned char seg4[]={0xff,0xe7,0xc3,0x81,0x00,0xff,0x7e,0x3c,0x18,0x00}; //8只流水灯中心到两端,再两端到中心
void delay(int x) //延时函数
{
int i,j;
for(i=x;i>0;i--)
for(j=120;j>0;j--);
}
void duo() //判断哪个按键
{
P2=0xff;
if((P2&0x0f)!=0x0f)
{
delay(10);
if(P2==0xfe)
key=1;
if(P2==0xfd)
key=2;
if(P2==0xfb)
key=3;
if(P2==0xf7)
key=4;
}
}
void shu1() //0至9
{
int a;
for(a=0;a<10;a++)
{
P0=seg1[a];
delay(500);
}
}
void shu2() //5201314
{
int a;
for(a=0;a<7;a++)
{
P0=seg2[a];
delay(500);
}
}
void shu3() //上往下
{
int a;
for(a=0;a<8;a++)
{
P1=seg3[a];
delay(800);
}
}
void shu4() //中心到两端,再两端到中心
{
int a;
for(a=0;a<10;a++)
{
P1=seg4[a];
delay(800);
}
}
void main() //主函数
{
key=0;
while(1)
{
duo();
switch(key)
{
case 1:shu3();break;//按下K1,8个LED实现流水点亮
case 2:shu4();break;//按下K2,8个LED实现中心到两端,再两端到中心
case 3:shu1();break;//按下K3,数码管显示0至9
case 4:shu2();break;//按下K4,数码管显示5201314
}
}
}
proteus仿真界面:
![](https://i-blog.csdnimg.cn/blog_migrate/ca790a6d351e725a3963c0ea408b6331.png)