#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit ds1302_RST =P2^0;
sbit ds1302_IO =P2^1;
sbit ds1302_SCLK=P2^2;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit A1=P3^0;
sbit A2=P3^1;
sbit A3=P3^2;
sbit A4=P3^3;
sbit A5=P3^4;
sbit A6=P3^5;
sbit key1=P3^6;
sbit key2=P3^7;
uchar now_time[3],wei,d[3]={0,0,0};
uchar code s[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
void delay(uint x)
{
uchar i;
for(x;x>0;x--)
for(i=0;i<100;i++);
}
void disp()
{
P1=s[now_time[2]/16];
A1=d[2];
delay(5);
A1=1;
P1=s[now_time[2]%16];
A2=d[2];
delay(5);
A2=1;
P1=s[now_time[1]/16];
A3=d[1];
delay(5);
A3=1;
P1=s[now_time[1]%16];
A4=d[1];
delay(5);
A4=1;
P1=s[now_time[0]/16];
A5=d[0];
delay(5);
A5=1;
P1=s[now_time[0]%16];
A6=d[0];
delay(5);
A6=1;
}
/***********************************************************************/
uchar read_Byte()
{
uchar i;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=ds1302_IO;
ds1302_SCLK=1;
ds1302_SCLK=0;
}
return(ACC);
}
void write_Byte(uchar tdata)
{
uchar i;
ACC=tdata;
for(i=8;i>0;i--)
{
ds1302_IO=ACC0;
ds1302_SCLK=1;
ds1302_SCLK=0;
ACC=ACC>>1;
}
}
/***********************************************************************************/
void write_data_ds1302(uchar taddr,uchar tdata)
{
ds1302_RST=0;
ds1302_SCLK=0;
ds1302_RST=1;
write_Byte(taddr);
write_Byte(tdata);
ds1302_RST=0;
ds1302_SCLK=1;
}
uchar read_data_ds1302(uchar taddr)
{
uchar tdata;
ds1302_RST=0;
ds1302_SCLK=0;
ds1302_RST=1;
write_Byte(taddr);
tdata=read_Byte();
ds1302_RST=0;
ds1302_SCLK=1;
return(tdata);
}
/***********************************************************************************/
void get_ds1302()
{
uchar k;
uchar taddr = 0x81;
for (k=0; k<3; k++)
{
now_time[k] = read_data_ds1302(taddr);
taddr+=2;
}
}
/***********************************************************************************/
void init_ds1302()
{
ds1302_RST=0;
ds1302_SCLK=0;
A1=1;
A2=1;
A3=1;
A4=1;
A5=1;
A6=1;
write_data_ds1302(0x80,0x00);
}
/***********************************************************************************/
void Time();
/***********************************************************************************/
main()
{
init_ds1302();
while(1)
{
disp();
get_ds1302();
if(key1==0);
{ delay(10);
if(key1==0)
Time();
}
}
}
void timer() interrupt 3
{
uchar i;i++;
TH1=(65535-50000)/256;
TL2=(65535-50000)%256;
if(i==50)
{
d[wei]=1;
}
if(i==100)
{ i=0;
d[wei]=0;
}
}
/***********************************************************************************/
void Time()
{
uchar temp;
uint r=0,p=1;
wei=2;
TMOD=0x01;
EA=1;
ET1=1;
TH1=(65535-50000)/256;
TL2=(65535-50000)%256;
TR1=1;
for(r=0;r<50;r++)
disp();
while( key1 )
{
wei=2;
disp();
if(key2==0)
{
disp();
if(key2==0)
{
temp=now_time[2]/16*10+now_time[2]%16;
temp++;
if(temp>=24)
temp=0;
now_time[2]=temp/10*16+temp%10;
write_data_ds1302(0x84,now_time[2]);
}
while(!key2);
}
}
wei=1;
d[2]=0;
for(r=0;r<50;r++)
disp();
while(key1 )
{ disp();
if(key2==0)
{
disp();
if(key2==0)
{
temp=now_time[1]/16*10+now_time[1]%16;
temp++;
if(temp>=60)
temp=0;
now_time[1]=temp/10*16+temp%10;
write_data_ds1302(0x82,now_time[1]);
}
while(!key2);
}
}
wei=0;
d[1]=0;
for(r=0;r<50;r++)
disp();
while(key1 )
{
disp();
if(key2==0)
{
now_time[0]=0;
write_data_ds1302(0x80,now_time[0]);
}
} TR1=0;
d[0]=0;
for(r=0;r<30;r++)
disp();
}
/*********************************************************************************/