#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar hour,hour_h,hour_l; /*????,?????,?????*/
uchar min,min_h,min_l;
uchar sec,sec_h,sec_l;
uchar int_num; /*????????*/
uchar bjcs;
uchar shi;
uchar fen;
sbit key_secadd=P1^6; /*??? p1.4???, ??????*/
sbit key_minadd=P1^5; /*??? p1.5???,??????*/
sbit key_houradd=P1^4; /*??? p1.5???,??????*/
sbit key_intime=P1^7; //?P1.7????
sbit key_quit=P1^7; //P1.7????
sbit bell=P1^0; //???
void delay(uchar time); /*???????*/
void display(); /*???????*/
void inter_init(); /*???????????*/
void time24();
/****????????***/
uchar code duma[]={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf};
uchar code wema[]={0x40,0x80,0x20,0x08,0x10,0x04,0x01,0x02}; /*??hour ?min ?sec*/
//---------------------?????--------------------------------
void delay(uchar time) /*?????*/
{
uchar time_1;
for (;time>0;time--)
for (time_1=100;time_1>0;time_1--);
}
//---------------------???T0??--------------------------
void time0(void) interrupt 1 /*???T0?????*/
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
int_num++;
}
//---------------------?????--------------------------
void inter_init() /*?????????*/
{
EA=1; /*????*/
EX0=1; /*?????0*/
ET0=1; /*?????T0*/
TMOD=0x01; /*????1*/
TCON = 0x00; /*????*/
IP = 0x01; /*??????,T0??*/
TH0=(65535-50000)/256; /*???,??50ms???? */
TL0=(65535-50000)%256;
TR0=1; /*??TR0,?????0*/
}
//---------------------24?????? --------------------------
void time24() /*????*/
{
if(int_num==20) /*??20,??????????*/
{ /* ????2,??????10?,??????*/
int_num=0;
sec++; /*???1*/
if(sec==60) /*?????60,??0*/
{
sec=0;
min++; /*???1*/
if(min==60)
{
min=0;
hour++; /*???0,???1*/
if(hour==24)
{
hour=0; /*???24?0*/
}
}
}
}
}
//-----------???????-------------------------------------
void key(void) //???????
{
if(key_intime==0)
{EA=0; // ???
while(10) //??
{ display();
if(key_quit==0)
{
EA=1;
break;
}
if(key_secadd==0)
{
delay(1);
if(key_secadd==0)
{
sec++;
while(!key_secadd);
if(sec==60)
{
sec=0;
}
}
}
if(key_minadd==0)
{
delay(80);
if(key_minadd==0)
{
min++;
while(!key_minadd);
if(min==60)
{
min=0;
}
}
}
if(key_houradd==0)
{
delay(80);
if(key_houradd==0)
{
hour++;
while(!key_houradd);
if(hour==24)
{
hour=0;
}
}
}
} }
}
//---------------------????????-------------------------------------
void display() /*????????*/
{
sec_h=sec/10; /*????????????*/
sec_l=sec%10; /*????????????*/
min_h=min/10;
min_l=min%10;
hour_h=hour/10;
hour_l=hour%10;
P0=duma[sec_h]; /*?????*/
P2=wema[0];
delay(10);
P0=0xff;
P0=duma[sec_l]; /*?????*/
P2=wema[1];
delay(10);
P0=0xff;
P0=duma[10]; /* “?”????? */
P2=wema[2];
delay(10);
P0=0xff;
P0=duma[min_h]; /*?*/
P2=wema[3];
delay(10);
P0=0xff;
P0=duma[min_l];
P2=wema[4];
delay(10);
P0=0xff;
P0=duma[10]; /* “?”????? */
P2=wema[5];
delay(10);
P0=0xff;
P0=duma[hour_h]; /*?*/
P2=wema[6];
delay(10);
P0=0xff;
P0=duma[hour_l];
P2=wema[7];
delay(10);
P0=0xff;
}
//----------------Õûμ㱨ê±----------------//
void alarm_clock()
{
uint i,j,y,m;
if(hour==00)
{
if(min>=01){m=0;}
if(min==00)
{
if(m<1)
{
do{
m++;display();
for(i=0;i<50;i++)
{bell=~bell;for(j=0;j<24;j++);}
for(y=0;y<50;y++)
{bell=~bell;for(j=0;j<25;j++);}
for(i=0;i<50;i++)
{bell=~bell;for(j=0;j<26;j++);}
for(i=0;i<50;i++)
{bell=~bell;for(j=0;j<27;j++);}
}
while(m<1);
}
}
}
}
//-----------ÄÖÖó----------
void dingshi(void)
{
//if((P1^7==0)&&(time++>40))//??P1_7BU??,????????,???P1_6?P1_7???????
if(P1^7==0)
{
delay(100);
if(P1^7==0)
{
while(!key);
delay(100);
if(P1^7==0)
{
P2=0XFE;
P0=duma[0];//Ãë¸öλ
delay(1);
P2=0XFD;
P0=duma[0];//Ãëê®Î»
delay(1);
P2=0XFB;
P0=duma[10];// -
delay(1);
P2=0XF7;
P0=duma[min_l];//???
delay(1);
P2=0XEF;
P0=duma[min_h];//???
delay(1);
P2=0XDF;
P0=duma[10];//??? -
delay(1);
P2=0XBF;
P0=duma[hour_l];//???
delay(1);
P2=0X7F;
P0=duma[hour_h];//???
delay(1);
}
if(P1^4==0)//???
{
delay(30);
if(P1^4==0)
{
shi++;
if(hour==24)
{
shi=0;
}
}
delay(250);
}
if(P1^5==0)//???
{
delay(30);
if(P1^5==0)
{
fen++;
if(min==60)
{
fen=0;
}
}
delay(250);
}
if((hour==shi)&(min==fen)&(sec==0))//?????,?????
{
for(bjcs=0;bjcs<10;bjcs++)
{
bell=0;
delay(500);
bell=1;
delay(500);
}
}
}}
}
//---------------------Ö÷oˉêy--------------------------------
void main()
{
bell=0;
inter_init(); /*??????*/
hour=23; /*???????,??????????(???)*/
min=59;
sec=50;
while(1)
{
key();
time24(); /*????,??????*/
alarm_clock();//????
display(); /*?????????*/
}
}
按键双击进入调时之后 时钟也变成闹钟设置的时间了 |