我是靠谱客的博主 文静电灯胆,最近开发中收集的这篇文章主要介绍Arduino UNO驱动DS1307数字实时时钟RTC,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

DS1307时钟模块简介

DS1307是一款低功耗,具有56字节非失性RAM的全BCD码时钟日历实时时钟芯片,地址和数据通过两线双向的串行总线的传输,芯片可以提供秒,分,小时等信息,每一个月的天数能自动调整。并且有闰年补偿功能,记年上限2100。AM/PM 标志位决定时钟工作于24小时或12小时模式,芯片有一个内置的电源感应电路,具有掉电检测和电池切换功能。
模块配备了一个后备2032可充电锂电池,并带充电电路,充满电后可提供最长一年的计时。保留了4KB的EEPROM的内存,用以记录更多闹钟和一些事件信息。预留一个DS18B20数字温度传感器接口,根据需要焊接。
EEPROM和DS1307共用一个IIC通信接口,用器件地址加以区分。

模块引脚定义

在这里插入图片描述
SQ引脚已经接了上拉电阻,当DS1307芯片内SQWE位置1,则可输出四种频率的脉冲信号1Hz、4…096KHz、8.192KHz、32.768KHz

Arduino UNO与模块接线

Arduino UNODS1307
A4SDA
A5SCL
5VVCC
GNDGND

在这里插入图片描述

测试代码

在Arduino IDE库管理器搜索DS1307,并安装如图示所例程库
在这里插入图片描述
打开setTimeAndSqw.ino例程

/*!
 * @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, 17, 3, 9, 11, 2022};//设置当前时间
  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:%drn",
            getTimeBuff[6],
            getTimeBuff[5],
            getTimeBuff[4],
            getTimeBuff[3],
            getTimeBuff[2],
            getTimeBuff[1],
            getTimeBuff[0]
            );
  Serial.print(outputarr);

 delay(1000);
}

实验结果

通过串口助手显示出进行初始时间设置后开始计时,并在模块的SQ引脚用示波器测得输出1Hz的秒脉冲
在这里插入图片描述
在这里插入图片描述

最后

以上就是文静电灯胆为你收集整理的Arduino UNO驱动DS1307数字实时时钟RTC的全部内容,希望文章能够帮你解决Arduino UNO驱动DS1307数字实时时钟RTC所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部