1个回答
展开全部
这个是简单的数码管时钟显示如果有需要,我写过比较完善的1602显示时钟.显示控制年月日等等.#include<reg51.h>
sbit Begin=P2^0;
sbit Hour=P2^1;
sbit Mus=P2^2;
sbit End=P2^3;
unsigned char code Tab[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
unsigned char code num[]={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char Time[]={0,0,16,0,0,16,0,0};
unsigned char a;
unsigned int x,m,th;
void init()
{
TMOD=0x01;
TH0=(65535/50000)/256;
TL0=(65535/50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
unsigned char keyboard()
{
if(Begin==0){
delay(5);
if(Begin==0)
return 1;
}
if(Hour==0){
delay(30);
if(Hour==0)
return 2;
}
if(Mus==0)
return 3;
if(End==0)
return 4;
}
void display()
{
unsigned char i; for(i=0;i<8;i++)
{
P3=num[i];
P0=Tab[Time[i]];
delay(5);
}
}
void main()
{
unsigned char Key;
init();
Key=keyboard();
if(Key==1)
{
while(1){
Key=keyboard();
display();
if(Key==2)
x+=3600;
if(Key==3)
x+=1;
if(Key==4)
return;
}
}}
void holes() interrupt 1 using 2
{
TH0=(65535/50000)/256;
TL0=(65535/50000)%256;
a++;
if(a>=20)
{
x++;
m=x;
th=m/3600; //设置小时
Time[0]=th/10;
Time[1]=th%10;
m=m%3600; Time[2]=16; th=m/60; //设置分钟
Time[3]=th/10;
Time[4]=th%10;
m=m%60; Time[5]=16; th=m; //设置秒
Time[6]=th/10;
Time[7]=th%10; a=0;
}
}
sbit Begin=P2^0;
sbit Hour=P2^1;
sbit Mus=P2^2;
sbit End=P2^3;
unsigned char code Tab[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
unsigned char code num[]={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char Time[]={0,0,16,0,0,16,0,0};
unsigned char a;
unsigned int x,m,th;
void init()
{
TMOD=0x01;
TH0=(65535/50000)/256;
TL0=(65535/50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
unsigned char keyboard()
{
if(Begin==0){
delay(5);
if(Begin==0)
return 1;
}
if(Hour==0){
delay(30);
if(Hour==0)
return 2;
}
if(Mus==0)
return 3;
if(End==0)
return 4;
}
void display()
{
unsigned char i; for(i=0;i<8;i++)
{
P3=num[i];
P0=Tab[Time[i]];
delay(5);
}
}
void main()
{
unsigned char Key;
init();
Key=keyboard();
if(Key==1)
{
while(1){
Key=keyboard();
display();
if(Key==2)
x+=3600;
if(Key==3)
x+=1;
if(Key==4)
return;
}
}}
void holes() interrupt 1 using 2
{
TH0=(65535/50000)/256;
TL0=(65535/50000)%256;
a++;
if(a>=20)
{
x++;
m=x;
th=m/3600; //设置小时
Time[0]=th/10;
Time[1]=th%10;
m=m%3600; Time[2]=16; th=m/60; //设置分钟
Time[3]=th/10;
Time[4]=th%10;
m=m%60; Time[5]=16; th=m; //设置秒
Time[6]=th/10;
Time[7]=th%10; a=0;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询