|
#ifndef _DS1302_H #define _DS1302_H
#ifndef __C8051F020_H__ #include <c8051f020.h> #endif
#ifndef _MACRO_H #include "macro.h" #endif
sbit ACC0 = ACC ^ 0; sbit ACC7 = ACC ^ 7;
sbit DSRST = P2 ^ 7; //时钟复位信号线 sbit DSDIO = P2 ^ 6; //时钟数据线 sbit DSCLK = P2 ^ 5; //时钟时钟线
#define SEC_WRI 0x80 #define SEC_READ 0x81 #define MIN_WRI 0x82 #define MIN_READ 0x83 #define HR_WRI 0x84 #define HR_READ 0x85 #define DATE_WRI 0x86 #define DATE_READ 0x87 #define MON_WRI 0x88 #define MON_READ 0x89 #define WEEK_WRI 0x8A #define WEEK_READ 0x8B #define YEAR_WRI 0x8C #define YEAR_READ 0x8D #define WRI_PROTECT_WRI 0x8E #define WRI_PROTECT_READ 0x8F #define CLOCK_MULTI_BYTE_WRI 0xBE #define CLOCK_MULTI_BYTE_READ 0xBF #define RAM_MULTI_BYTE_WRI 0xFE #define RAM_MULTI_BYTE_READ 0xFF
#define DSRSTLOW() DSRST = 0; _nop_(); _nop_(); _nop_(); _nop_();\ _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); \ _nop_(); _nop_(); _nop_(); _nop_(); _nop_()
#define DSRSTHIGH() DSRST = 1; _nop_(); _nop_(); _nop_(); _nop_();\ _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); \ _nop_(); _nop_(); _nop_(); _nop_(); _nop_()
#define DSCLKLOW() DSCLK = 0; _nop_(); _nop_(); _nop_(); _nop_();\ _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); \ _nop_(); _nop_(); _nop_(); _nop_(); _nop_() #define DSCLKHIGH() DSCLK = 1; _nop_(); _nop_(); _nop_(); _nop_();\ _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); \ _nop_(); _nop_(); _nop_(); _nop_(); _nop_()
extern uchar xdata dsWriDsI ; extern uchar xdata dsWriCh ;
extern uchar xdata dsReadDsK ; extern uchar xdata dsReadCl ;
extern uchar xdata ds1302SettimerBuffer[]; extern uchar xdata ds1302GettimerBuffer[];
void WriDs(uchar addr, uchar dat); uchar ReadDs(uchar addr);
void SetTimer(uchar *pSetTimer); void GetTimer(uchar *pGetTimer);
void OscEnable(); void Osc24(); 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 : 涓流充电使能 * 调用: 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); }
|