I2C DS1307 RTC实时时钟模块

简介

DFRobot曾出过一款DS1307 RTC 实时时钟模块,它以低廉的价格广受好评。此次DFRobot特意推出了新一代Gravity接口版本。

新版模块采用高精度时钟晶振,大幅度减少了时间误差,实测24小时内误差仅在1秒左右(机械表大约在20s左右)。此外新版RTC模块采用了单节CR1220供电,缩减了整体的体积,电池最小使用寿命为3~5年。保留了4KB的EEPROM的内存,用以记录更多闹钟和一些事件信息,可通过调整电阻设置I2C地址。

Gravity: I2C DS1307 RTC实时时钟模块采用Gravity-I2C接口,可以直插Gravity IO扩展板,配合DFRobot新开发的Arduino DS1307库,可以轻松实现时间设定,时间显示等功能。无论是用于数据采集,还是制作时钟,都是一个非常棒的选择!

产品参数

引脚说明

:实时时钟管脚定义

标号 名称 功能描述
1 VCC 电源输入正极(5v)
2 GND 电源输入负极
3 SCL I2C时钟
4 SDA I2C数据
5 SQ 时钟输出
6 VCC 电源输入正极(5v)
7 GND 电源输入负极
8 SCL I2C时钟
9 SDA I2C数据

实时时钟管脚定义

使用教程

准备

连线图

DFR0151接线图

样例代码

/*!
 * @file  setTimeAndSqw.ino
 * @brief  Set time and start timing, set pin sqw output
 * @details  Read the time and pin sqw mode
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  [qsjhyy](yihuan.huang@dfrobot.com)
 * @version  V1.0
 * @date  2021-11-18
 * @url  https://github.com/DFRobot/DFRobot_DS1307
 */
#include <DFRobot_DS1307.h>


/* Constructor */
DFRobot_DS1307 DS1307;

void setup()
{
  Serial.begin(115200);

  // Initialize sensor
  while( !(DS1307.begin()) ){
    Serial.println("Communication with device failed, please check connection");
    delay(3000);
  }
  Serial.println("Begin ok!");

  /**
   *  According to the set time type, input the value of the corresponding range, set time
   *  type Time type to be obtained, eTimeType_t:
   *    eSEC, eMIN, eHR, eDOW, eDATE, eMTH, eYR
   *  typeTime According to the selected time type, write the value of the corresponding range
   *    When setting eSEC type, range: 00-59
   *    When setting eMIN type, range: 00-59
   *    When setting eHR type, range: 00-23
   *    When setting eDOW type, range: 01-07
   *    When setting eDATE type, range: 01-31
   *    When setting eMTH type, range: 01-12
   *    When setting eYR type, range: 2000-2099
   *  Note: Values out of range will result in a setting error
   */
  DS1307.setTypeTime(DS1307.eYR, 2000);

  /**
   *  Get the time of one type from rtc module and convert it to uint16_t
   *  type Time type to be obtained, eTimeType_t:
   *    eSEC, eMIN, eHR, eDOW, eDATE, eMTH, eYR
   *  According to the selected time type, return the value uint16_t of the corresponding range
   *    When reading eSEC type, range: 00-59
   *    When reading eMIN type, range: 00-59
   *    When reading eHR type, range: 00-23
   *    When reading eDOW type, range: 01-07
   *    When reading eDATE type, range: 01-31
   *    When reading eMTH type, range: 01-12
   *    When reading eYR type, range: 2000-2099
   */
  Serial.print("get type time: ");
  Serial.println(DS1307.getTypeTime(DS1307.eYR));

  /**
   *  Stop RTC timer function
   *  this bit is part of the seconds byte
   */
  DS1307.stop();
  /**
   *  According to the available array, set all the time
   *  setTimeBuff Array in the following format (type is uint16_t)
   *    setTimeBuff[0] for eSEC type, range: 00-59
   *    setTimeBuff[1] for eMIN type, range: 00-59
   *    setTimeBuff[2] for eHR type, range: 00-23
   *    setTimeBuff[3] for eDOW type, range: 01-07
   *    setTimeBuff[4] for eDATE type, range: 01-31
   *    setTimeBuff[5] for eMTH type, range: 01-12
   *    setTimeBuff[6] for eYR type, range: 2000-2099
   *  Note: Values out of range will result in a setting error
   */
  uint16_t setTimeBuff[7] = {5, 1, 7, 6, 9, 9, 2021};
  DS1307.setTime(setTimeBuff);
  /**
   *  Start RTC timing function
   *  this bit is part of the seconds byte
   */
  DS1307.start();

  /**
   *  control the operation of the SQW/OUT pin
   *  mode SQW Pin output mode:
   *    eSquareWave_LOW, eSquareWave_HIGH, eSquareWave_1Hz, 
   *    eSquareWave_4kHz, eSquareWave_8kHz, eSquareWave_32kHz
   */
  DS1307.setSqwPinMode(DS1307.eSquareWave_1Hz);

}

void loop()
{
  /**
   *  Get the current output mode of SQW/OUT pin
   *  eSqwPinMode_t:
   *    eSquareWave_LOW, eSquareWave_HIGH, eSquareWave_1Hz, 
   *    eSquareWave_4kHz, eSquareWave_8kHz, eSquareWave_32kHz
   */
  if(DS1307.eSquareWave_1Hz == DS1307.getSqwPinMode()){
    Serial.print("SQW/OUT pin: 1Hz | ");
  }

  /**
   *  Get the time from rtc module and convert it to uint16_t
   *  getTimeBuff Array for buffering the obtained time, uint16_t *
   *    getTimeBuff[0] for eSEC type, range: 00-59
   *    getTimeBuff[1] for eMIN type, range: 00-59
   *    getTimeBuff[2] for eHR type, range: 00-23
   *    getTimeBuff[3] for eDOW type, range: 01-07
   *    getTimeBuff[4] for eDATE type, range: 01-31
   *    getTimeBuff[5] for eMTH type, range: 01-12
   *    getTimeBuff[6] for eYR type, range: 2000-2099
   */
  uint16_t getTimeBuff[7] = {0};
  DS1307.getTime(getTimeBuff);
  char outputarr[128];
  sprintf(outputarr, "time: %d/%d/%d-%d %d:%d:%d\r\n",
            getTimeBuff[6],
            getTimeBuff[5],
            getTimeBuff[4],
            getTimeBuff[3],
            getTimeBuff[2],
            getTimeBuff[1],
            getTimeBuff[0]
            );
  Serial.print(outputarr);

 delay(1000);
}

结果

DFR0151_串口_1.png

Mind+(基于Scratch3.0)图形化编程

1、下载及安装软件。下载地址:https://www.mindplus.cc 详细教程:Mind+基础wiki教程-软件下载安装 2、切换到“上传模式”。 详细教程:Mind+基础wiki教程-上传模式编程流程 3、“扩展”中选择“主控板”中的“Arduino Uno”。“扩展”中选择“传感器”中的“实时时钟DS1307” 详细教程:Mind+基础wiki教程-加载扩展库流程 4、进行编程,程序如下图: 5、菜单“连接设备”,“上传到设备” 6、程序上传完毕后,打开串口即可看到数据输出。详细教程:Mind+基础wiki教程-串口打印

注意:调整时间的程序被执行就会重置时间,因此调整时间的程序(图1)烧录完成之后,接下来不要断电,烧录一个不含调整时间的程序(图2)

附录:EEPROM IIC地址真值表

DFR0117_Add_Tb.png

:注:EEPROM芯片默认I2C地址为0x50, A0~A2 丝印旁边200R 焊接上为ON 去掉为0ff |

常见问题

问:有什么办法让电脑时间实时更新到模块中吗?

答:请点击下面的链接:电脑时间实时更新

更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。

更多

DFshopping_car1.png DFRobot商城购买链接