#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar sec,min,hr;
sbit clk=P1^0;
sbit dat=P1^1;
sbit rst=P1^2;
sbit A0=ACC^0;
sbit A1=ACC^1;
sbit A2=ACC^2;
sbit A3=ACC^3;
sbit A4=ACC^4;
sbit A5=ACC^5;
sbit A6=ACC^6;
sbit A7=ACC^7;
sbit blue=P1^5;
sbit green=P1^6;
sbit red=P1^7;
void InputByte(uchar dd)//mcu to ds1302
{
uchar i;
ACC=dd;
for(i=8;i>0;i--)
{
dat=A0;
clk=1;
clk=0;
ACC=ACC>>=1;
}
}
//-------------------------------------------------------
uchar OutputByte(void) //ds1302 to mcu
{
uchar i;
dat=1;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
A7=dat;
clk=1;
clk=0;
}
return(ACC);
}
//--------------------------------------------------
void Write(uchar addr,uchar num)
{
rst=0;
clk=0;
rst=1;
InputByte(addr); // address
InputByte(num); //data
clk=1;
rst=0;
}
//-------------------------------------------------
uchar Read(uchar addr)
{
uchar dd=0;
rst=0;
clk=0;
rst=1;
InputByte(addr);
dd=OutputByte();
clk=1;
rst=0;
return(dd);
}
//--------------------------------------------------------------
uchar ReadSec()
{
uchar dd;
dd=Read(0x81);
return(dd);
}
//--------------------------------------------------------------
uchar ReadMin()
{
uchar dd;
dd=Read(0x83);
return(dd);
}
//--------------------------------------------------------------
uchar ReadHr()
{
uchar dd;
dd=Read(0x85);
return(dd);
}
//------------------------------------------------------------
uchar ReadWe()
{
uchar dd;
dd=Read(0x8b);
return(dd);
}
//------------------------------------------------------------
uchar ReadDay()
{
uchar dd;
dd=Read(0x87);
return(dd);
}
//------------------------------------------------------------
uchar ReadMn()
{
uchar dd;
dd=Read(0x89);
return(dd);
}
//------------------------------------------------------------
uchar ReadYs()
{
uchar dd;
dd=Read(0x8d);
return(dd);
}
//------------------------------------------------------------
void WriteSec(uchar num)
{
Write(0x80,num);
}
//-----------------------------------------------------------
void WriteMin(uchar num)
{
Write(0x82,num);
}
//-----------------------------------------------------------
void WriteHr(uchar num)
{
Write(0x84,num);
}
//-----------------------------------------------------------
void WriteDay(uchar num)
{
Write(0x86,num);
}
//-----------------------------------------------------------
void WriteMn(uchar num)
{
Write(0x88,num);
}
//-----------------------------------------------------------
void WriteWe(uchar num)
{
Write(0x8a,num);
}
//-----------------------------------------------------------
void WriteYs(uchar num)
{
Write(0x8c,num);
}
//-----------------------------------------------------------
void DisableWP(void) //write data,read 0
{
Write(0x8e,0x00);
}
//----------------------------------------------------------
void EnableWP(void) //can''d write,read data
{
Write(0x85,0x80);
}
void delay()
{uint i,j;
for(i=0;i<1000;i++)
{
for(j=0;j<40;j++){}
}
}
void main(void)
{
DisableWP();
WriteSec(0x00);
WriteMin(0x57);
WriteHr(0x10);
WriteDay(0x07);
WriteMn(0x06);
WriteWe(0x02);
WriteYs(0x05);
}