8个独立按键分别按下,数码管会显示出相对应的数字,代码如下
#include<reg51.h>
int a;
unsigned char code table[]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8,
0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};//共阳极
/*unsigned char code table[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71}; //共阴极*/
void delay(int sex)
{
int x,y;
for(x=sex;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
while(1)
{
if(P1!=0xff)
{
delay(5);//防止抖动
if(P1!=0xff)//P1所以接口都是不是1111 1111
{
switch(P1)
{
case 0xfe:a=1;break; //1111 1110
case 0xfd:a=2;break; //1111 1101
case 0xfb:a=3;break; //1111 1011
case 0xf7:a=4;break; //1111 0111
case 0xef:a=5;break; //1110 1111
case 0xdf:a=6;break; //1101 1111
case 0xbf:a=7;break; //1011 1111
case 0x7f:a=8;break; //0111 1111
}
}
}
while(P1!=0xff);
P3=table[a];
}
}
proteus8.6仿真效果图
|