📄 ds1302.c
字号:
#include <DS1302.h>
#include <intrins.h>
/********************************************************************
*
* 名称: OscEnable
* 说明:
* 功能: 时钟停止位使能
* call: v_W1302
* in: no
* return:no
*********************************************************************/
void OscEnable()
{
WriDs(0x80,0);
}
/********************************************************************
*
* name: Osc24
* state:
* fun: use 24 hour record
* call: v_W1302
* in: no
* return:no
*********************************************************************/
void Osc24()
{
WriDs(0x84,0);
}
/********************************************************************
*
* 名称: ChargeEnable
* state:
* fun : 涓流充电使能
* 调用: v_W1302
* in: no
* 返回值: 无
*********************************************************************/
void ChargeEnable()
{
WriDs(0x90,0xa5); /*涓流充电,一个二极管,一个2k的电阻*/
}
/********************************************************************
*
* 名称: DsInit()
* 说明:
* fun : 时钟芯片初始化
* call: No
* in: no
* return: no
*********************************************************************/
void DsInit()
{
//T_CLK = 0;
//T_RST = 0;
//_nop_();
//T_RST = 1;
DSCLKLOW();
DSRSTLOW();
DSRSTHIGH();
OscEnable(); /* 时钟停止位使能 */
WriDs(0x8e,0x0); /* 允许写入单个数据字节 */
ChargeEnable(); /* 充电使能 */
Osc24();
}
/**************************************************
*原型:void WriteDS(uchar addr, uchar dat)
*功能:向ds1302时钟芯片写入数据
*说明:无
*参数:dat 写入数据;addr 写入地址
*返回:无
**************************************************/
void WriDs(uchar addr, uchar dat)
{
//uchar dsWriDsI;
//uchar dsWriCh = 0x01;
dsWriDsI = 0;
dsWriCh = 0x01;
EA = 0;
DSCLKLOW();
DSRSTLOW();
DSRSTHIGH();
for(dsWriDsI = 0; dsWriDsI < 8; dsWriDsI ++)
{
if(addr & dsWriCh)
{
DSDIO = 1;
}
else
{
DSDIO = 0;
}
dsWriCh = dsWriCh << 1;
DSCLKHIGH();
DSCLKLOW();
}
dsWriCh = 0x01;
for(dsWriDsI = 0; dsWriDsI < 8; dsWriDsI ++)
{
if(dat & dsWriCh)
{
DSDIO = 1;
}
else
{
DSDIO = 0;
}
dsWriCh = dsWriCh << 1;
DSCLKHIGH();
DSCLKLOW();
}
DSRSTLOW();
EA = 1;
}
/**************************************************
*原型:uchar ReadDS(uchar addr)
*功能:读取ds时钟内容
*说明:无
*参数:内部寄存器地址
*返回:寄存器内容
**************************************************/
uchar ReadDs(uchar addr)
{
//uchar dsReadDsK;
//uchar dsReadCl;
dsReadDsK = 0;
dsReadCl = 0;
DSRSTLOW();
DSCLKLOW();
DSRSTHIGH();
dsReadCl = 0x01;
for(dsReadDsK = 0; dsReadDsK < 8; dsReadDsK ++)
{
if(addr & dsReadCl)
{
DSDIO = 1;
}
else
{
DSDIO = 0;
}
dsReadCl = dsReadCl << 1;
DSCLKHIGH();
DSCLKLOW();
}
dsReadCl = 0;
for(dsReadDsK = 0; dsReadDsK < 8; dsReadDsK ++)
{
dsReadCl = dsReadCl >> 1;
if(DSDIO)
{
dsReadCl = dsReadCl + 0x80;
}
DSCLKHIGH();
DSCLKLOW();
}
DSRSTLOW();
return(dsReadCl);
}
void SetTimer(uchar *pSetTimer)
{
WriDs(YEAR_WRI, *(pSetTimer + 0));
WriDs(MON_WRI , *(pSetTimer + 1));
WriDs(DATE_WRI, *(pSetTimer + 2));
WriDs(HR_WRI , *(pSetTimer + 3));
WriDs(MIN_WRI , *(pSetTimer + 4));
WriDs(SEC_WRI , *(pSetTimer + 5));
}
void GetTimer(uchar *pGetTimer)
{
*(pGetTimer + 0) = ReadDs(YEAR_READ);
*(pGetTimer + 1) = ReadDs(MON_READ);
*(pGetTimer + 2) = ReadDs(DATE_READ);
*(pGetTimer + 3) = ReadDs(HR_READ);
*(pGetTimer + 4) = ReadDs(MIN_READ);
*(pGetTimer + 5) = ReadDs(SEC_READ);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -