一个独立按键控制数码管动态显示程序亮灭c语言

2024-12-02 22:52:38
推荐回答(1个)
回答1:

有点类似,你看看吧:
#include
#include //反转法控制按键
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar a,b,c,d,keya,keyb;
uint e;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(uint x) //延时
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void shumaguan(uchar a) //数码管
{
wela=1;
P0=0xc0;
wela=0;
dula=1;
P0=table[a]; //a在键盘函数引用
dula=0;
delay(10);
}
void liushuideng() //流水灯
{
b=0xc9;
while(e>=0&&e<=80)
{
P1=b;
delay(50);
b=_crol_(b,1);
e++;
if(e==80)
b=0xff;
}
}
uchar key() //有返回值的按键函数
{
P3=0x0f; //定行
P3=P3&0x0f; //判断哪一行有被按下的键
if(P3!=0x0f)
{
delay(10);
P3=0x0f;
P3=P3&0x0f;
if(P3!=0x0f)
{
c=P3&0x0f;
P3=0xf0;
d=P3&0xf0; //判断列 ,行反转
return(c+d);
}
}
return 0xff;
}
void main()
{
while(1)
{
keyb=key();
switch(keyb)
{
case 0xee:keya=0;while(key()!=0xff);shumaguan(keya);break; //判断按键
case 0xed:keya=1;while(key()!=0xff);shumaguan(keya);break;
case 0xeb:keya=2;while(key()!=0xff);shumaguan(keya);break;
case 0xe7:keya=3;while(key()!=0xff);shumaguan(keya);break;
case 0xde:keya=4;while(key()!=0xff);shumaguan(keya);break;
case 0xdd:keya=5;while(key()!=0xff);shumaguan(keya);break;
case 0xdb:keya=6;while(key()!=0xff);shumaguan(keya);break;
case 0xd7:keya=7;while(key()!=0xff);shumaguan(keya);break;
case 0xbe:keya=8;while(key()!=0xff);shumaguan(keya);break;
case 0xbd:keya=9;while(key()!=0xff);shumaguan(keya);break;
case 0xbb:keya=10;while(key()!=0xff);shumaguan(keya);break;
case 0xb7:keya=11;while(key()!=0xff);shumaguan(keya);break;
case 0x7e:keya=12;while(key()!=0xff);shumaguan(keya);break;
case 0x7d:keya=13;while(key()!=0xff);shumaguan(keya);break;
case 0x7b:keya=14;while(key()!=0xff);shumaguan(keya);break;
case 0x77:keya=15;while(key()!=0xff);shumaguan(keya);delay(1000);liushuideng();e=0;break;
default:break;
}
}
}