#include<STC15F2K60S2.h>
#define uchar unsigned char
#define uint unsigned int
uchar code wela[8]={0xfe,0xfd,0xfc,0xfb,0xef,0xdf,0xcf,0xbf};
uchar code dula[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xfb,0x80,0x90,0xbf};
void init();//初始化定时器
void delay(uint z);
void miao();
void fen();
void shi();
void fuhao();
void display();//显示函数
void keyscan();
sbit key1=P3^4; //控制秒分时的闪烁
sbit key2=P3^1; //控制时钟的启动暂停
sbit key3=P3^6; //设置时钟的值
sbit key4=P3^7; //计时和倒计时
uchar hour,second,minute,t0,i,t1,key1_num;
uchar s_flag;//闪烁标志位
uchar count_flag; //计时倒计时标志位
void main()
{
init();
while(1)
{
keyscan();
switch(key1_num)
{
case 0:
{
display();
break;
}
case 1:
{
AUXR|=0x00;
if(s_flag==1)
{
P2=wela[5];
P0=0xff;
delay(1);
P2=wela[4];
P0=0xff;
delay(1);
}
if(s_flag==0)
{
miao();
}
fuhao();
fen();
shi();
break;
}
case 2:
{
AUXR|=0x00;
if(s_flag==1)
{
P2=wela[3];
P0=0xff;
delay(1);
P2=wela[2];
P0=0xff;
delay(1);
}
if(s_flag==0)
{
fen();
}
fuhao();
miao();
shi();
break;
}
case 3:
{
AUXR|=0x00;
if(s_flag==1)
{
P2=wela[1];
P0=0xff;
delay(1);
P2=wela[0];
P0=0xff;
delay(1);
}
if(s_flag==0)
{
shi();
}
fuhao();
miao();
fen();
break;
}
}
}
}
void miao()
{
P2=wela[7];
P0=dula[second%10];
delay(1);
P2=wela[6];
P0=dula[second/10];
delay(1);
}
void fen()
{
P2=wela[4];
P0=dula[minute%10];
delay(1);
P2=wela[3];
P0=dula[minute/10];
delay(1);
}
void shi()
{
P2=wela[1];
P0=dula[hour%10];
delay(1);
P2=wela[0];
P0=dula[hour/10];
delay(1);
}
void fuhao()
{
P2=wela[5];
P0=dula[10];
delay(1);
P2=wela[2];
P0=dula[10];
delay(1);
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Timer2() interrupt 12
{
if(count_flag==0)
{
t0++;
if(t0==10)
{
t0=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
hour=0;
}
}
}
}
if(count_flag==1)
{
t0++;
if(t0==10)
{
t0=0;
second--;
if(second==255)
{
second=59;
minute--;
if(minute==255)
{
minute=59;
hour--;
if(hour==255)
hour=23;
}
}
}
}
}
void T0_time() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
t1++;
if(t1==5)
{
t1=0;
s_flag=!s_flag;
}
}
void init()
{
TMOD=0x01;
AUXR&=0xfb;
T2H=0x3c;
T2L=0xb0;
TH0=0x3c;
TL0=0xb0;
EA=1;
IE2|=0x04;
AUXR|=0x10;
ET0=1;
TR0=1;
}
void keyscan()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
key1_num++;
if(key1_num==4)
key1_num=0;
}
while(!key1)
display();
}
if(key2==0)
{
delay(10);
if(key2==0)
{
AUXR|=0x00;
}
while(!key2)
display();
}
if(key3==0)
{
delay(10);
if(key3==0)
{
if(key1_num==1)
{
second++;
if(second==60)
second=0;
}
if(key1_num==2)
{
minute++;
if(minute==60)
minute=0;
}
if(key1_num==3)
{
hour++;
if(hour==24)
{
hour=0;
}
}
while(!key3)
display();
}
}
if(key4==0)
{
delay(10);
if(key4==0)
{
count_flag++;
if(count_flag==2)
count_flag=0;
}
while(!key4)
display();
}
}
void display()
{
miao();
fen();
shi();
fuhao();
|