x1,x2震荡引脚,sclk时钟信号,ce使能信号,ce置1输出
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302_Byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
#endif
/*void t0_timer()interrupt 1 //中断
{
static uint a,b,c;
TH0=0xF8; //重装初值
TL0=0x30;
display();
if(m2>0)
{
if(++c==500) //3s
{
c=0;
m2--;
}
if(m2==0)
buzzstate=0;
}
if(buzzstate==1)
buzz=1;
else buzz=0;
if(openflag==1 && m1>0) //5s
{
if(++b==500)
{
b=0;
m1--;
if(m1==0)
openflag=0;
}
}
if(openflag==1)
relay=1;
if(openflag==0)
relay=0;
if(++a==50) //100ms
{
a=0;
flag100ms=1;
}
if(++intr==200) //400ms
{
s_flag=1;
intr=0;
}
} */
. c文件
#include <reg52.h>
#include <intrins.h>
sbit SCK=P0^4;
sbit SDA=P0^3;
sbit RST=P0^2;
void Write_Ds1302_Byte(unsigned char temp) //SCLK上升沿写入,下降沿读出
{
unsigned char i;
for (i=0;i<8;i++) //循环8次,写入数据
{
SCK=0;
SDA=temp&0x01; //得到最后一位,每次传输低字节给SDA
temp>>=1; //temp=temp>>1 右移一位
SCK=1; //SCK上升沿写入
}
}
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_(); // 上电后,VCC>2V之前
RST=1; //启动。只有在SCK为低电平时,才能将RST置为高电平(查Ds1302芯片工作原理)
_nop_();
Write_Ds1302_Byte(address); //发送地址
Write_Ds1302_Byte(dat); //发送数据
RST=0; //恢复
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1; //启动数据传输
_nop_();
Write_Ds1302_Byte(address); //写入读命令
for (i=0;i<8;i++) //循环8次,输出数据
{
SCK=0; //SCK脉冲下降沿,读出数据
temp>>=1; //右移,先读最低位
if(SDA) //如果读出数据为1 (下降沿读出数据)
temp|=0x80; //将1取出,写在temp最高位
SCK=1;
}
RST=0; //恢复
_nop_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_(); //Ds1302复位的稳定时间
return (temp);
}