上學期寫的一個時鐘程序 可能很多可以優化的 不過你可以參考一下
#include<reg51.h>
unsigned char shows1[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char shows2[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
unsigned char i,j,num,temp;
unsigned int hour,minute,second,tens,units,second1,minute1,hour1;
void delay(unsigned int n)
{
for(i=n;i>0;i--)
for(j=0;j<110;j++);
}
void showtime(int x)
{
tens=x/10;
units=x%10;
}
void debughour()
{
if(P3==0xbf)
{
delay(1);
while(P3==0xbf);
hour++;
if(hour==24)
hour=0;
showtime(hour);
P0=shows1[tens];
P2=0x01;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x02;
delay(1);
P2=0x00;
}
if(P3==0xdf)
{
delay(1);
while(P3==0xdf);
hour--;
if(hour==-1)
hour=23;
showtime(hour);
P0=shows1[tens];
P2=0x01;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x02;
delay(1);
P2=0x00;
}
if(P3==0x7f)
{
delay(1);
while(P3==0x7f);
temp++;
}
}
void debugminute()
{
if(P3==0xbf)
{
delay(1);
while(P3==0xbf);
minute++;
if(minute==60)
minute=0;
showtime(minute);
P0=shows1[tens];
P2=0x04;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x08;
delay(1);
P2=0x00;
}
if(P3==0xdf)
{
delay(1);
while(P3==0xdf);
minute--;
if(minute==-1)
minute=59;
showtime(minute);
P0=shows1[tens];
P2=0x04;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x08;
delay(1);
P2=0x00;
}
if(P3==0x7f)
{
delay(1);
while(P3==0x7f);
temp++;
}
}
void keyscan1()
{
while(temp<=2)
{
if(P3==0x7f)
{
delay(1);
while(P3==0x7f);
temp++;
}
TR0=0;
showtime(second);
P0=shows1[tens];
P2=0x10;
delay(1);
P2=0x00;
P0=shows1[units];
P2=0x20;
delay(1);
P2=0x00;
showtime(minute);
P0=shows1[tens];
P2=0x04;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x08;
delay(1);
P2=0x00;
showtime(hour);
P0=shows1[tens];
P2=0x01;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x02;
delay(1);
P2=0x00;
if(temp==1)
debughour();
if(temp==2)
debugminute();
if(temp==3)
{
TR0=1;
temp=0;
break;
}
}
}
void keyscan2()
{
while(temp<=1)
{
if(P3==0xef)
{
delay(1);
while(P3==0xef);
temp++;
}
if(num==18)
{
num=0;
P1=~P1;
second1++;
if(second1==60)
{
second1=0;
minute1++;
if(minute1==60)
{
minute1=0;
hour1++;
if(hour1==100)
hour1=0;
}
}
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
hour=0;
}
}
}
showtime(second1);
P0=shows1[tens];
P2=0x10;
delay(1);
P2=0x00;
P0=shows1[units];
P2=0x20;
delay(1);
P2=0x00;
showtime(minute1);
P0=shows1[tens];
P2=0x04;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x08;
delay(1);
P2=0x00;
showtime(hour1);
P0=shows1[tens];
P2=0x01;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x02;
delay(1);
P2=0x00;
if(temp==2)
{
temp=0;
hour1=0;
minute1=0;
second1=0;
break;
}
}
}
void runtime()
{
if(num==18)
{
num=0;
P1=~P1;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
hour=0;
}
}
}
}
main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P3=0xff;
temp=0;
hour=3;
minute=58;
while(1)
{
runtime();
showtime(second);
P0=shows1[tens];
P2=0x10;
delay(1);
P2=0x00;
P0=shows1[units];
P2=0x20;
delay(1);
P2=0x00;
showtime(minute);
P0=shows1[tens];
P2=0x04;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x08;
delay(1);
P2=0x00;
showtime(hour);
P0=shows1[tens];
P2=0x01;
delay(1);
P2=0x00;
P0=shows2[units];
P2=0x02;
delay(1);
P2=0x00;
if(P3==0x7f)
keyscan1();
if(P3==0xef)
keyscan2();
}
}
void timer0() interrupt 1 using 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
}
|