用数码管动态显示做的电子钟,调时的时候如何让选中位闪烁呢?自己写了一个不带闪烁的可调时时钟,求大神指点迷津啊!!
自己的代码如下:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar sec,min,hour,kfnum,tt;
sbit key1=P0^0;
sbit keyfunc=P0^1;
void init();
void delay(uchar z);
void display(uchar sec,uchar min,uchar hour);
void keyscan();
void main()
{
init();
while(1)
{
keyscan();
}
}
void init()//初始化程序
{
sec=0;
min=0;
hour=0;
kfnum=0;
tt=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1//定时器0中断程序
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
if(tt==20)
{
tt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
min=0;
sec=0;
}
}
}
}
}
void delay(uchar z)
{
uchar i,j;
for(i=z;i>0;i--)
for(j=128;j>0;j--);
}
void display(uchar sec,uchar min,uchar hour)//显示程序
{
uchar sec1,sec2,min1,min2,hour1,hour2;
sec2=sec/10;
sec1=sec%10;
P1=table[sec1];
P2=0x7f;
delay(5);
P2=0xff;
P1=table[sec2];
P2=0xbf;
delay(5);
P2=0xff;
P1=0x40;//显示短横线
P2=0xdf;
delay(5);
P2=0xff;
min2=min/10;
min1=min%10;
P1=table[min1];
P2=0xef;
delay(5);
P2=0xff;
P1=0x40;//显示短横线
P2=0xfb;
delay(5);
P2=0xff;
P1=table[min2];
P2=0xf7;
delay(5);
P2=0xff;
hour2=hour/10;
hour1=hour%10;
P1=table[hour1];
P2=0xfd;
delay(5);
P2=0xff;
P1=table[hour2];
P2=0xfe;
delay(5);
P2=0xff;
}
void keyscan()//键盘扫描程序
{
if(keyfunc==0)
{
delay(10);
if(keyfunc==0)
{
TR0=0;
kfnum++;
while(keyfunc!=1);
if(kfnum==4)
{
kfnum=0;
TR0=1;
display(sec,min,hour);
}
}
}
if(kfnum!=0)
{
if(key1==0)
{
delay(10);
if(key1==0)
{
display(sec,min,hour);
while(key1!=1);
if(kfnum==1)
{
sec++;
if(sec==60)
sec=0;
}
if(kfnum==2)
{
min++;
if(min==60)
min=0;
}
if(kfnum==3)
{
hour++;
if(hour==24)
hour=0;
}
}
}
}
display(sec,min,hour);
}
此帖出自
小平头技术问答
一周热门 更多>