#include "reg52.h"
#define THCO 0xee
#define TLCO 0x00
unsigned char code Duan[]={
0x3F,0x06,0x5B,0x4F,
0x66,0x6D,0x7D,0x07,
0x7F,0x6F};
unsigned char Data_Buffer[6]={0,0,0,0,0,0};
unsigned char sec=0,min=58,hour=10;
bit Point_Flash=0;
unsigned char mode_count=1;
sbit DU=P2^6;
sbit WE=P2^7;
sbit mode=P3^4;
sbit add=P3^5;
sbit del=P3^6;
void delay(unsigned int i)
{
unsigned char x,y;
for(x=0;x<i;x++)
for(y=0;y<124;y++);
}
void scankey()
{
if(!mode)
{
delay(10);
while(!mode);
if(mode)
{
delay(10);
mode_count++;
if(mode_count>=4) mode_count=1;
}
}
}
void dealwith()
{
if(mode_count==1)
{
Data_Buffer[5]=hour/10;
Data_Buffer[4]=hour%10;
Data_Buffer[3]=min/10;
Data_Buffer[2]=min%10;
Data_Buffer[1]=sec/10;
Data_Buffer[0]=sec%10;
}
if(mode_count==2)
{
if(!add)
{
delay(10);
while(!add);
if(add)
{
delay(10);
min++;
if(min>=60)min=0;
}
}
if(!del)
{
delay(10);
while(!del);
if(del)
{
delay(10);
min--;
if(min==0)min=59;
}
}
Data_Buffer[5]=mode_count%10;
Data_Buffer[4]=0;
Data_Buffer[3]=0;
Data_Buffer[2]=0;
Data_Buffer[1]=min/10;
Data_Buffer[0]=min%10;
}
if(mode_count==3)
{
if(!add)
{
delay(10);
while(!add);
if(add)
{
delay(10);
hour++;;
if(hour>=24)hour=0;
}
}
if(!del)
{
delay(10);
while(!del);
if(add)
{
delay(10);
hour--;
if(hour==0)hour=23;
}
}
Data_Buffer[5]=mode_count%10;
Data_Buffer[4]=0;
Data_Buffer[3]=0;
Data_Buffer[2]=0;
Data_Buffer[1]=hour/10;
Data_Buffer[0]=hour%10;
}
}
void main()
{
TMOD=0x11;
TH0=THCO;
TL0=TLCO;
TR0=1;
ET0=1;
EA=1;
while(1)
{
scankey();
dealwith();
}
}
void timer0() interrupt 1
{
static unsigned char Bit=0;
static unsigned char count=0;
TH0=THCO;
TL0=TLCO;
count++;
if(count>=200)
{
count=0;
sec++;
if(sec>=60)
{
sec=0;
min++;
if(min>=60)
{
min=0;
hour++;
if(hour>=24)hour=0;
}
}
Point_Flash=1;
}
if(count==100)
Point_Flash=0;
Bit++;
if(Bit>=6)
Bit=0;
P0=Duan[Data_Buffer[Bit]];
if(Point_Flash&&Bit==2)P0|=0x80;
DU=1;
DU=0;
switch(Bit)
{
case 0: P0=0x7f;break;
case 1: P0=0xbf;break;
case 2: P0=0xdf;break;
case 3: P0=0xef;break;
case 4: P0=0xf7;break;
case 5: P0=0xfb;break;
}
WE=1;
WE=0;
}