网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 杭州晶控电子有限公司 >> 文章中心 >> 单片机应用 >> 文章正文
  时钟芯片ds1302程序,调试通过(转载)            【字体:
时钟芯片ds1302程序,调试通过(转载)
作者:佚名    文章来源:不详    点击数:    更新时间:2008-01-06    

#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);
}


文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    利用实时时钟IC RV5C338A实现
    虚拟环绕处理器芯片MM1454
    HDNS2000光学感测芯片在运动
    一块接口芯片控制两块智能卡
    串行AD和DA芯片的应用
    通用异步收发芯片SCC2691的原
    极低功耗无线收发集成芯片CC
    基于C51串行时钟DS1305在无纸
    数字温度测控芯片DS1620的应
    TDC-GP1高精度时间间隔测量芯
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    杭州晶控电子有限公司 版权所有 站长:hificat