#include<reg52.h>
#include <INTRINS.H>
#define unchar unsigned char
#define unint unsigned int
sbit RST=P3^5;
sbit IO=P3^3;
sbit CLK=P3^4;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
unchar xdata time[]="10:03:30";
void reset() //reset DS1302 system
{
RST=0;
CLK=0;
RST=1;
}
void sendbyte(unchar cDATA) //send one byte to DS1302
{
unchar i;
ACC=cDATA;
for(i=0;i<8;i++)
{
IO=ACC0;
CLK=0;
_nop_();
CLK=1;
_nop_();
CLK=0;
ACC=ACC>>1;
}
}
unchar read_c_byte() //read one byte from DS1302
{
unchar i;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=IO;
CLK=1;
_nop_();
CLK=0;
}
return (ACC);
}
void write_byte(unchar caddr,unchar cDATA)
{
reset();
sendbyte(0x8E);
sendbyte(0);
reset();
sendbyte(caddr);
sendbyte(cDATA);
sendbyte(0x80);
CLK=1;
RST=0;
CLK=0;
}
void set_time()
{
reset();
write_byte(0xBE,0x00);
write_byte(0x90,0xA5);
write_byte(0x80,0x10); //second
write_byte(0x82,0x58); //minute
write_byte(0x84,0x10); //hour
write_byte(0x86,0x14); //day
write_byte(0x88,0x03); //month
write_byte(0x8A,0x05); //week
write_byte(0x8C,0x10); //year
write_byte(0x8E,0x80);
}
unchar read_byte(unchar caddr)
{
unchar cDATA;
reset();
sendbyte(caddr|0x01);
cDATA=read_c_byte();
CLK=1;
RST=0;
return (cDATA);
}
void Rtime()
{
unchar d,b;
d=read_byte(0x80);
b=d/16+48;
writechar(13, 0, b,0);
b=d%16+'0';
writechar(12, 0, b,0);
b=':';
writechar(11, 0, b,0);
d=read_byte(0x82);
b=d/16+'0';
writechar(10, 0, b,0);
b=d%16+'0';
writechar(9, 0, b,0);
b=':';
writechar(8, 0, b,0);
d=read_byte(0x84);
b=d/16+'0';
writechar(7, 0, b,0);
b=d%16+'0';
writechar(6, 0, b,0);
} |