用4个按键来控制数码管显示的内容
#include<reg51.h>
#define duan P0 //段选
#define wei P2 //位选
unsigned char code wei1[8] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位选控制 查表的方法控制
unsigned char code duan1[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码
unsigned char ge,shi,bai,a,b;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
void keys(); //按键 函数
void s(unsigned char xms); //延时函数
void DigDisplay(); //动态显示函数
void init(); //初始化函数
void main(void)
{
init();
while(1)
{
DigDisplay();
keys();
}
}
void DigDisplay()
{
unsigned char i;
unsigned int j;
bai=a/100;
shi=a%100/10;
ge=a%10;
i=0;
wei = wei1[i]; //发送位选
duan = duan1[bai]; //发送段码
j = 10; //扫描间隔时间设定
while(j--);
duan = 0x00; //消隐
i++;
wei = wei1[i]; //发送位选
duan = duan1[shi]; //发送段码
j = 10; //扫描间隔时间设定
while(j--);
duan = 0x00; //消隐
i++;
wei = wei1[i]; //发送位选
duan = duan1[ge]; //发送段码
j = 10; //扫描间隔时间设定
while(j--);
duan = 0x00; //消隐
}
void init()
{
key1=1;
key2=1;
key3=1;
key4=1;
TMOD=0X01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
}
void s(unsigned char xms)
{
unsigned char x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
void times() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
b++;
if(b==20)
{
b=0;
a++;
if(a==256)
{
a=0;
}
}
}
void keys()
{
if(key1==0)
{
s(10);
if(key1==0)
{
a++;
TR0=0;
if(a==256)
a=0;
while(!key1)
DigDisplay();
}
}
if(key2==0)
{
s(10);
if(key2==0)
{
TR0=0;
if(a==0)
a=256;
a--;
while(!key2)
DigDisplay();
}
}
if(key3==0)
{
s(10);
if(key3==0)
{
TR0=0;
a=0;
while(!key3)
DigDisplay();
}
}
if(key4==0)
{
s(10);
if(key4==0)
{
while(!key4)
DigDisplay();
TR0=~TR0;
}
}
}
|