#include<REGX51.h>
#define uchar unsigned char
#define uint unsigned int
uint count,i,sec,shumaguainitflag;
uint miao,shimiao,fen,shifen;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f} ; //数码管显示1-9
unsigned char code sectab[]={0xbf,0x86,0xDb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; //第二位数码管现显示1-9
unsigned char code dangwei[]={0xbf,0x86,0xdb,0xcf};
int dw=0; unsigned char a,b,c,d;
sbit s1=P2^0; //第一位数码管
sbit s2=P2^1; //第二位数码管
sbit s3=P2^2; //第三位数码管
sbit s4=P2^3; //第四位数码管
sbit k1=P3^1; //测试按键
sbit k2=P3^7; //复位按键
sbit k3=P3^3; //加档按键
sbit k4=P3^5; //减档按键
sbit k5=P3^6; //启动按键
sbit k6=P2^4; //增加一秒
sbit k7=P2^5; //增加十秒
sbit k8=P2^6; //增加一分钟
sbit k9=P2^7; //增加十分钟
void delay(unsigned int xms); //调用延时函数
void Nixie(unsigned char Location,Number) //显示函数
{
switch(Location)
{
case 1:s1=0;s2=1;s3=1;s4=1;break;
case 2:s1=1;s2=0;s3=1;s4=1;break;
case 3:s1=1;s2=1;s3=0;s4=1;break;
case 4:s1=1;s2=1;s3=1;s4=0;break;
}
if(Location==2){P0=sectab[Number];}
else {P0=table[Number]; }
delay(1);
P0=0x00;
}
void dangwei_init() //档位函数
{
if(k3==0)
{while(!k3);dw++;}if(dw==4){dw=3;}
if(k4==0)
{while(!k4);dw--;}if(dw<0){dw=0;}
P1=dangwei[dw];
}
void daiji_Init() //待机函数
{
Nixie(1,0);
Nixie(2,0);
Nixie(3,0);
Nixie(4,0);
}
void jiance_init() //检测函数
{
while(1)
{
P0=0xff;
P2_0=0;
delay(1);
P0=0xff;
P2_1=0;
delay(1);
P0=0xff;
P2_2=0;
delay(1);
P0=0xff;
P2_3=0;
delay(1);
P3_0=0;
delay(1000);
P0=0x00;
P2_0=1;
delay(1);
P0=0x00;
P2_1=1;
delay(1);
P0=0x00;
P2_2=1;
delay(1);
P0=0x00;
P2_3=1;
delay(1);
P3_0=1;
delay(1000);
if(k2!=1){break;}
}
}
void Timer0Init() //定时器初始化函数
{
TMOD=0x01; //0000 0001
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main() //主函数
{
while(1)
{
daiji_Init();
if(k1==0){jiance_init();}
dangwei_init();
if(dw==1)
{
while(1){dangwei_init();Nixie(1,1);Nixie(2,0);Nixie(3,0);Nixie(4,0);if(k5==0|dw==2|dw==3|dw==0|k2==0){break;}}
if(k2!=1){break;}
Timer0Init();sec=600;
if(k5==0)
{
while(sec)
{
if(count<1000&&shumaguainitflag==0)
{
miao = table[0];
shimiao = table[0];
fen = table[0];
shifen = table[0];
}
if(count>=1000)
{
count = 0;
sec--;if(a+b*10>60&&c>0){a+b*10==60;}
shumaguainitflag=1;
miao = table[sec%3600%60%10];
shimiao = table[sec%3600%60/10];
fen = sectab[sec%3600/60%10];
shifen = table[sec/600];
}
}P3_0=0;delay(2000);P3_0=1;
break;
}
}
if(dw==2)
{
while(1){dangwei_init();Nixie(1,2);Nixie(2,0);Nixie(3,0);Nixie(4,0);if(k5==0|dw==1|dw==3|dw==0|k2==0){break;}}
if(k2!=1){break;}
Timer0Init();sec=1200;
if(k5==0)
{
while(sec)
{
if(count<1000&&shumaguainitflag==0)
{
miao = table[0];
shimiao = table[0];
fen = table[0];
shifen = table[0];
}
if(count>=1000)
{
count = 0;
sec--;if(a+b*10>60&&c>0){a+b*10==60;}
shumaguainitflag=1;
miao = table[sec%3600%60%10];
shimiao = table[sec%3600%60/10];
fen = sectab[sec%3600/60%10];
shifen = table[sec/600];
}
}P3_0=0;delay(2000);P3_0=1;
break;
}
}
if(dw==3)
{
while(1){dangwei_init();Nixie(1,3);Nixie(2,0);Nixie(3,0);Nixie(4,0);if(k5==0|dw==1|dw==2|dw==0|k2==0){break;}}
if(k2!=1){break;}
Timer0Init();sec=1800;
if(k5==0)
{
while(sec)
{
if(count<1000&&shumaguainitflag==0)
{
miao = table[0];
shimiao = table[0];
fen = table[0];
shifen = table[0];
}
if(count>=1000)
{
count = 0;
sec--;if(a+b*10>60&&c>0){a+b*10==60;}
shumaguainitflag=1;
miao = table[sec%3600%60%10];
shimiao = table[sec%3600%60/10];
fen = sectab[sec%3600/60%10];
shifen = table[sec/600];
}
}P3_0=0;delay(2000);P3_0=1;
break;
}
}
a=b=c=d=0;
if(P3_2==0)
{
Nixie(1,0);Nixie(2,0);Nixie(3,0);Nixie(4,0);Timer0Init();
while(1)
{
if(k6==0){while(!k6);a++;if(a==10){a=0;}} Nixie(4,a);
if(k7==0){while(!k7);b++;if(b==7){b=0;}}if(a+b*10>60){b=0;} Nixie(3,b);
if(k8==0){while(!k8);c++;if(c==10){c=0;}} Nixie(2,c);
if(k9==0){while(!k9);d++;if(d==10){a=b=c=d=0;}} Nixie(1,d);
if(k2!=1){break;}
if(k5==0)
{
sec=a+b*10+c*60+d*600;
while(sec)
{
if(count<1000&&shumaguainitflag==0)
{
miao = table[0];
shimiao = table[0];
fen = table[0];
shifen = table[0];
}
if(count>=1000)
{
count = 0;
sec--;if(a+b*10>60&&c>0){a+b*10==60;}
shumaguainitflag=1;
miao = table[sec%3600%60%10];
shimiao = table[sec%3600%60/10];
fen = sectab[sec%3600/60%10];
shifen = table[sec/600];
}
}P3_0=0;delay(2000);P3_0=1;
break;
}
}
}
}
}
void Timer0() interrupt 1 //中断函数
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
count++;
P2=0xff;
switch(i)
{
case 0: P0 = miao;P2 = 0xf7;i++; break;
case 1: P0 = shimiao;P2 = 0xfb;i++;break;
case 2: P0 = fen;P2 = 0xfd;i++;break;
case 3: P0 = shifen;P2 = 0xfe;i=0;break;
default : break;
}
}