|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
#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";
unchar code time_set[7]={0x28,0x57,0x01,0x13,0x03,0x06,0x05};
void reset() //reset DS1302 system
{
RST=0;
CLK=0;
RST=1;
}
void sendbyte(unchar cDATA) //send one byte to DS1302
{
unchar i;
ACC=cDATA;
RST=1;
for(i=0;i<8;i++)
{
IO=ACC0;
CLK=0;
CLK=1;
ACC=ACC>>1;
}
}
unchar read_c_byte() //read one byte from DS1302
{
unchar i;
RST=1;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
IO=1;
CLK=1;
CLK=0;
ACC7=IO;
}
return (ACC);
}
void write_byte(unchar caddr,unchar cDATA)
{
reset();
//sendbyte(0x8E);
//sendbyte(0);
//reset();
sendbyte(caddr);
_nop_();
_nop_();
_nop_();
sendbyte(cDATA);
//sendbyte(0x80);
RST=0;
CLK=1;
}
unchar read_byte(unchar caddr)
{
unchar cDATA;
reset();
sendbyte(caddr|0x01);
_nop_();
cDATA=read_c_byte();
RST=0;
CLK=1;
return (cDATA);
}
void set_time()
{
unchar i,temp;
temp=0x80;
write_byte(0x8E,0x00);
//write_byte(0x90,0xA5);
for(i=0;i<7;i++)
{
write_byte(temp,time_set[i]);
temp=temp+2;
}
write_byte(0x80,0x00); //second
write_byte(0x82,0x01); //minute
write_byte(0x84,0x05); //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);
}
void Rtime()
{
unchar d;
d=read_byte(0x80);
time[6]=((d&0x70)>>4)+42;
time[7]=(d&0x0F)+'0';
time[5]=':';
d=read_byte(0x82);
time[3]=((d&0x70)>>4)+41;
time[4]=(d&0x0F)+33;
time[2]=':';
d=read_byte(0x84);
time[0]=((d&0x70)>>4)+41;
time[1]=(d&0x0F)+33;
} |
|