📄 ds1302.c
字号:
/**************************************************************************************
* 子程序:底层程序 *
* 功能:时间日期数据处理; *
* *
* *
***************************************************************************************/
# include <intrins.h>
# include "macroandconst.h"
# include "stc12c5a60s2.h"
# include "nokia5110.h"
sbit SCK = P0^2; //DS1302时钟;
sbit SDA = P0^1; //DS1302数据;
sbit RST = P0^0;// DS1302复位;
uchar l_tmpDate[]={0,0,0,1,1,1,13};//秒分时日月周年08-05-15 12:00:00
uchar l_tmpdisplay[13];
code uchar Write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
code uchar 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次 写入数据
{
SCK=0;
SDA=temp&0x01; //每次传输低字节
temp>>=1; //右移一位
SCK=1;
}
}
/******************************************************************/
/* 写入DS1302 */
/******************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_(); //启动
Write_Ds1302_Byte(address); //发送地址
Write_Ds1302_Byte(dat); //发送数据
RST=0; //恢复
}
/******************************************************************/
/* 读出DS1302数据 */
/******************************************************************/
unsigned char Read_Ds1302 ( uchar address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
_nop_();
SCK=0;
_nop_();
_nop_();
RST=1;
_nop_();
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++) //循环8次 读取数据
{
if(SDA)
{
temp|=0x80;
} //每次传输低字节
temp>>=1; //右移一位
SCK=1;
_nop_();
_nop_();
SCK=0;
}
RST=0;
_nop_(); //以下为DS1302复位的稳定时间
_nop_();
RST=0;
SCK=0;
_nop_();
_nop_();
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
SDA=0;
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
return (temp); //返回
}
/******************************************************************/
/* 读时钟数据 */
/******************************************************************/
void Read_RTC(void) //读取 日历
{
unsigned char i,*p;
p=Read_RTC_address; //地址传递
for(i=0;i<7;i++) //分7次读取 秒分时日月周年
{
l_tmpDate[i]=Read_Ds1302(*p);
p++;
}
}
/******************************************************************/
/* 设定时钟数据 */
/******************************************************************/
void Set_RTC(void) //设定 日历
{
uchar i,*p,tmp;
for(i=0;i<7;i++)
{ //BCD处理
tmp=l_tmpDate[i]/10;
l_tmpDate[i]=l_tmpDate[i]%10;
l_tmpDate[i]=l_tmpDate[i]+tmp*16;
}
Write_Ds1302(0x8E,0X00); //关闭写保护;
p=Write_rtc_address; //传地址
for(i=0;i<7;i++) //7次写入 秒分时日月周年
{
Write_Ds1302(*p,l_tmpDate[i]);
p++;
}
Write_Ds1302(0x8E,0x80); //打开写保护;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -