|
void ChargeEnable(); void DsInit();
#endif #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 : 涓流充電使能 * 調(diào)用: 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); /* 允許寫入單個數(shù)據(jù)字節(jié) */ ChargeEnable(); /* 充電使能 */ Osc24(); }
/************************************************** *原型:void WriteDS(uchar addr, uchar dat) *功能:向ds1302時鐘芯片寫入數(shù)據(jù) *說明:無 *參數(shù):dat 寫入數(shù)據(jù);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時鐘內(nèi)容 *說明:無 *參數(shù):內(nèi)部寄存器地址 *返回:寄存器內(nèi)容 **************************************************/ 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); }
|