DS1302时钟芯片介绍及使用教程

1.芯片简介

DS1302 涓流充电计时芯片包含一个实时时钟/日历和 31 字节的静态 RAM.通过简单的串行接口与微处理器通讯.这个实时时钟/日历提供年月日,时分秒信息.对于少于 31 天的月份月末会自动调整,还有闰年校正.由于有一个 AM/PM 指示器,时钟可以工作在 12 小时制或者24小时制。

主要特点如下

  • 实时时钟计算年、月、日、时、分、秒、星 期,直到 2100 年,并有闰年调节功能 ,具有31 x 8 位 通用暂存 RAM。

  • 2.0V 至 5.5V 宽电压范围操作 ,在2.0V时工作电流小于300nA 。

  • 读写时钟或 RAM 数据时有单字节或多字节(脉冲串模式)数据传送方式。引脚电平与 TTL 兼容 (VCC = 5V)。

2.DS1302引脚定义

        使用同步串行通讯简化了 DS1302 与微处理器的接口。与时钟/RAM 通讯只需要三根线: CE, I/O (数据线), and SCLK (串行时钟). 数据输出输入时钟/RAM 一次1字节或者在脉冲串中多达 31 字节。除了 DS1202 的基本计时功能以外, DS1302 有额外特点比如,双管脚主电源和备用电源,可编程涓流充电器 VCC1,还附加 7 字节的暂存器。

3.DS18B20驱动程序

(1)DS1302.h

#ifndef __DS1302_H__
#define __DS1302_H__

#include <reg52.h>
#include<intrins.h>

sbit SCK=P1^4;    
sbit SDA=P1^5;    
sbit RST=P1^6;
//复位脚
#define RST_CLR  RST=0//电平置低
#define RST_SET  RST=1//电平置高


//双向数据
#define IO_CLR  SDA=0//电平置低
#define IO_SET  SDA=1//电平置高
#define IO_R  SDA  //电平读取


//时钟信号
#define SCK_CLR  SCK=0//时钟信号
#define SCK_SET  SCK=1//电平置高


#define ds1302_sec_add      0x80    //秒数据地址
#define ds1302_min_add      0x82    //分数据地址
#define ds1302_hr_add      0x84    //时数据地址
#define ds1302_date_add      0x86    //日数据地址
#define ds1302_month_add    0x88    //月数据地址
#define ds1302_day_add      0x8a    //星期数据地址
#define ds1302_year_add      0x8c    //年数据地址
#define ds1302_control_add    0x8e    //控制数据地址
#define ds1302_charger_add    0x90            
#define ds1302_clkburst_add    0xbe

extern unsigned char time_buf1[8];//空年月日时分秒周
extern unsigned char time_buf[8] ;//空年月日时分秒周
/*------------------------------------------------
           向DS1302写入一字节数据
------------------------------------------------*/
void Ds1302_Write_Byte(unsigned char addr, unsigned char d);
/*------------------------------------------------
           从DS1302读出一字节数据
------------------------------------------------*/
unsigned char Ds1302_Read_Byte(unsigned char addr) ;
/*------------------------------------------------
           向DS1302写入时钟数据
------------------------------------------------*/
void Ds1302_Write_Time(void) ;
/*------------------------------------------------
           从DS1302读出时钟数据
------------------------------------------------*/
void Ds1302_Read_Time(void)  ;
/*------------------------------------------------
                DS1302初始化
------------------------------------------------*/
void Ds1302_Init(void);

#endif

(2)DS1302.c

#include "ds1302.h"

unsigned char time_buf1[8] = {20,9,3,13,18,51,00,6};//空年月日时分秒周
unsigned char time_buf[8] ;                         //空年月日时分秒周
/*------------------------------------------------
           向DS1302写入一字节数据
------------------------------------------------*/
void Ds1302_Write_Byte(unsigned char addr, unsigned char d)
{

  unsigned char i;
  RST_SET;  
  
  //写入目标地址:addr
  addr = addr & 0xFE;     //最低位置零
  for (i = 0; i < 8; i ++) 
  { 
    if (addr & 0x01) 
    {
      IO_SET;
    }
    else 
    {
      IO_CLR;
    }
    SCK_SET;
    SCK_CLR;
    addr = addr >> 1;
  }
 
  //写入数据:d
  for (i = 0; i < 8; i ++) 
  {
    if (d & 0x01) 
    {
      IO_SET;
    }
    else 
    {
      IO_CLR;
    }
    SCK_SET;
    SCK_CLR;
    d = d >> 1;
  }
  RST_CLR;          //停止DS1302总线
}
/*------------------------------------------------
           从DS1302读出一字节数据
------------------------------------------------*/

unsigned char Ds1302_Read_Byte(unsigned char addr) 
{

  unsigned char i;
  unsigned char temp;
  RST_SET;  

  //写入目标地址:addr
  addr = addr | 0x01;//最低位置高
  for (i = 0; i < 8; i ++) 
  {  
    if (addr & 0x01) 
    {
      IO_SET;
    }
    else 
    {
      IO_CLR;
    }
    SCK_SET;
    SCK_CLR;
    addr = addr >> 1;
  }
  
  //输出数据:temp
  for (i = 0; i < 8; i ++) 
  {
    temp = temp >> 1;
    if (IO_R) 
    {
      temp |= 0x80;
    }
    else 
    {
      temp &= 0x7F;
    }
    SCK_SET;
    SCK_CLR;
  }
  
  RST_CLR;  //停止DS1302总线
  return temp;
}

/*------------------------------------------------
           向DS1302写入时钟数据
------------------------------------------------*/
void Ds1302_Write_Time(void) 
{
     
    unsigned char i,tmp;
  for(i=0;i<8;i++)
  {                  //BCD处理
    tmp=time_buf1[i]/10;
    time_buf[i]=time_buf1[i]%10;
    time_buf[i]=time_buf[i]+tmp*16;
  }
  Ds1302_Write_Byte(ds1302_control_add,0x00);      //关闭写保护 
  Ds1302_Write_Byte(ds1302_sec_add,0x80);        //暂停 
  //Ds1302_Write_Byte(ds1302_charger_add,0xa9);      //涓流充电 
  Ds1302_Write_Byte(ds1302_year_add,time_buf[1]);    //年 
  Ds1302_Write_Byte(ds1302_month_add,time_buf[2]);  //月 
  Ds1302_Write_Byte(ds1302_date_add,time_buf[3]);    //日 
  Ds1302_Write_Byte(ds1302_day_add,time_buf[7]);    //周 
  Ds1302_Write_Byte(ds1302_hr_add,time_buf[4]);    //时 
  Ds1302_Write_Byte(ds1302_min_add,time_buf[5]);    //分
  Ds1302_Write_Byte(ds1302_sec_add,time_buf[6]);    //秒
  Ds1302_Write_Byte(ds1302_day_add,time_buf[7]);    //周 
  Ds1302_Write_Byte(ds1302_control_add,0x80);      //打开写保护 
}

/*------------------------------------------------
           从DS1302读出时钟数据
------------------------------------------------*/
void Ds1302_Read_Time(void)  
{ 
         unsigned char i,tmp;
  time_buf[1]=Ds1302_Read_Byte(ds1302_year_add);    //年 
  time_buf[2]=Ds1302_Read_Byte(ds1302_month_add);    //月 
  time_buf[3]=Ds1302_Read_Byte(ds1302_date_add);    //日 
  time_buf[4]=Ds1302_Read_Byte(ds1302_hr_add);    //时 
  time_buf[5]=Ds1302_Read_Byte(ds1302_min_add);    //分 
  time_buf[6]=(Ds1302_Read_Byte(ds1302_sec_add))&0x7F;//秒 
  time_buf[7]=Ds1302_Read_Byte(ds1302_day_add);    //周 


  for(i=0;i<8;i++)
  {           //BCD处理
    tmp=time_buf[i]/16;
    time_buf1[i]=time_buf[i]%16;
    time_buf1[i]=time_buf1[i]+tmp*10;
  }
}

/*------------------------------------------------
                DS1302初始化
------------------------------------------------*/
void Ds1302_Init(void)
{
  
  RST_CLR;      //RST脚置低
  SCK_CLR;      //SCK脚置低
    Ds1302_Write_Byte(ds1302_sec_add,0x00);         
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值