| #include"1302.h" 
 code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
 
 code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
 
 void Write_Ds1302_Byte(unsigned char temp)
 
 {
 
 unsigned char i;
 
 for (i=0;i<8;i++) //循环8次 写入数据
 
 {
 
 sclk=0;
 
 io=temp&0x01; //每次传输低字节
 
 temp>>=1; //右移一位
 
 sclk=1;
 
 }
 
 }
 
 /******************************************************************/
 
 /* 写入DS1302 */
 
 /******************************************************************/
 
 void Write_Ds1302( unsigned char address,unsigned char dat )
 
 {
 
 rst=0;
 
 _nop_();
 
 sclk=0;
 
 _nop_();
 
 rst=1;
 
 _nop_(); //启动
 
 Write_Ds1302_Byte(address); //发送地址
 
 Write_Ds1302_Byte(dat); //发送数据
 
 rst=0; //恢复
 
 }
 
 /******************************************************************/
 
 /* 读出DS1302数据 */
 
 /******************************************************************/
 
 unsigned char Read_Ds1302 ( unsigned char address )
 
 {
 
 unsigned char i,temp=0x00;
 
 rst=1;
 
 _nop_();
 
 _nop_();
 
 Write_Ds1302_Byte(address);
 
 for (i=0;i<8;i++) //循环8次 读取数据
 
 {
 
 if(io)
 
 temp|=0x80; //每次传输低字节
 
 sclk=1;
 
 temp>>=1; //右移一位
 
 _nop_();
 
 _nop_();
 
 _nop_();
 
 sclk=0;
 
 }
 
 rst=0;
 
 _nop_(); //以下为DS1302复位的稳定时间
 
 _nop_();
 
 rst=0;
 
 sclk=0;
 
 _nop_();
 
 _nop_();
 
 _nop_();
 
 _nop_();
 
 /*sclk=1;
 
 _nop_();
 
 _nop_();
 
 io=0;
 
 _nop_();
 
 _nop_();
 
 io=1;
 
 _nop_();
 
 _nop_(); */
 
 return (temp); //返回
 
 }
 
 /******************************************************************/
 
 /* 读时钟数据 */
 
 /******************************************************************/
 
 uchar Read_RTC1(uchar j) //读取 日历
 
 {
 
 uchar c;
 
 unsigned char *p;
 
 p=&read_rtc_address[j]; //地址传递
 
 c=Read_Ds1302(*p);
 
 c=(( c&0x70)>>4)*10 + ( c&0x0F);
 
 return(c);
 
 }
 
 /******************************************************************/
 
 /* 设定时钟数据 */
 
 /******************************************************************/
 
 void Set_RTC(uchar a,uint c) //设定 日历
 
 {
 
 unsigned char *p ,shi,ge ;
 
 Write_Ds1302(0x8E,0X00);
 
 shi=a/10;
 
 ge=a%10;
 
 a=shi*16+ge;
 
 p=&write_rtc_address[c]; //传地址
 
 Write_Ds1302(*p,a) ;
 
 // Write_Ds1302(0x8E,0x80);
 
 } 、
 
 这是头文件:
 
 #include
 
 #include
 
 #define uchar unsigned char
 
 #define uint unsigned int
 
 sbit sclk=P1^2; //时钟
 
 sbit io=P1^1; //数
 
 sbit rst = P1^0;
 
 void Set_RTC(uchar a,uint c);
 
 uchar Read_RTC1(uchar j);
 |