简介
DS3231M高精度微机电(MEMS)实时时钟(RTC)是内部集成了MEMS谐振器的温补RTC。内部集成微机电系统(MEMS)谐振器,相比石英晶振时钟,降低机械失效风险,解决温度漂移的问题。
该器件可以用纽扣电池供电,断开主电源时仍可保持精确计时。RTC可提供秒、分、时、星期、日期、月和年信息。少于31天的月,将自动调整月末的日期,包括闰年修正。
时钟格式可以是24小时或带AM/PM指示的12小时格式。提供两个可设置的日历闹钟和一个1Hz输出。在-45°C至+85°C温度范围内,计时精度保持在±5ppm (±0.432秒/天)。
通过I2C进行数据传输。当检测电源故障时,可以自动切换到备份电源。另外,RST监测引脚可以作为产生微处理器复位的按键输入。
优势和特性
- 高精度RTC,集成MEMS谐振器,完备管理全部计时功能
- 完整的时钟日历功能包括秒、分、时、星期、日期、月和年计时,并提供有效期到2100年的闰年补偿
- -45℃至+85℃温度范围内,计时精度保持在±5ppm(±0.432秒/天)
- 功能与DS3231兼容
- 两个日历闹钟
- 1Hz和32.768Hz输出
- 复位输出和按钮去抖输入
- 精度为±3℃的数字温度传感器
- +2.3V至+5.5V电源电压
 
- 简单的串行接口,可连接至大多数微控制器
- 高速(400kHz)I2C串行总线
 
- 为连续计时提供备用电池输入
- 低功耗工作,延长备用电池工作时间
 
- 工作温度范围:-40℃至+85℃
- 通过美国保险商实验室协会(UL)认证
技术规格
- 工作电压:3.3V ~ 5.5V
- 外部供电工作电流:130uA
- 时钟保持电池电流:2uA
- 计时范围:1970 ~ 2100年,带闰年补偿
- 接口方式:2.54间距排针孔全功能引出
- 工作温度:-45℃ ~ +85℃
引脚说明
 
 
 
引脚说明
| 序号 | 丝印 | 功能描述 | 
| 1 | VCC | 电源正极 | 
| 2 | GND | 电源负极 | 
| 3 | SCL | IIC时钟线 | 
| 4 | SDA | IIC数据线 | 
| 5 | INT | 低电平有效中断或1Hz方波输出 | 
| 6 | RST | 复位校验引脚 | 
| 7 | 32K | 32.768KHz脉冲输出 | 
使用教程
兼容
| MCU | Work Well | Untest | 
| Arduino Uno | √ | |
| Mega2560 | √ | |
| Leonardo | √ | |
| ESP32 | √ | |
| micro:bit | √ | 
准备
- 硬件
- 1 x Arduino UNO控制板
- 1 x DFRobot Precision DS3231M MEMS Real Time Clock
- 若干 杜邦线
 
- 软件
- Arduino IDE, 点击下载Arduino IDE
- 点击下载DS323X库文件
 
关于如何安装库文件,点击链接
- API接口函数列表
 /*!
   *@brief 设置时间
   *@param 年, 1900~2100
   *@param 月, 1~12
   *@param 日, 1~31
   *@param 时: 0~23
   *@param 分, 0~59
   *@param 秒, 0~59
   */
  void setTime(uint16_t year, uint8_t month, uint8_t date, uint8_t hour, uint8_t minute, uint8_t second);
  /*!
   *@brief 设置时制
   *@param eHours_t mode:e24hours(24小时制), e12hours(12小时制). 默认24小时制
   */
  void setHourSystem(eHours_t mode)
  /*!
   *@功能 获取年
   *@返回 年
   */
  uint16_t year();
  /*!
   *@功能 获取月
   *@返回 月
   */
  uint8_t  month();
  /*!
   *@功能 获取日
   *@返回 日
   */
  uint8_t  day();
  /*!
   *@功能 获取时
   *@返回 时
   */
  uint8_t  hour();
  /*!
   *@功能 获取分
   *@返回 分
   */
  uint8_t  minute();
  /*!
   *@功能 获取秒
   *@返回 秒
   */
  uint8_t  second();
  /*!
   *@brief 获取星期几
   *@return 星期几
   */
  String getDayOfWeek();
  typedef enum{
      eEverySecond,                  //每秒触发
      eSecondsMatch,                 //每分钟触发
      eSecondsMinutesMatch,          //每小时触发
      eSecondsMinutesHoursMatch,     //每天触发
      eSecondsMinutesHoursDateMatch, //每月触发
      eSecondsMinutesHoursDayMatch,  //每周触发
  }eAlarm1Types_t;   //Alarm1
  /*!
   *@brief 设置闹钟1
   *@param 闹钟工作模式
   *@param 日     (1-31)
   *@param 时    (0-23)
   *@param 分  (0-59)
   *@param 秒  (0-59)
   */
  void setAlarm1(eAlarm1Types_t alarmType,int16_t days,int8_t hours,int8_t minutes,int8_t seconds);
  typedef enum{
      eEveryMinute,                  //每分钟触发
      eMinutesMatch,                 //每小时触发
      eMinutesHoursMatch,            //每天触发
      eMinutesHoursDateMatch,        //每月触发
      eMinutesHoursDayMatch,         //每周触发
  }eAlarm2Types_t;   //Alarm2
  /*!
   *@brief 设置闹钟2
   *@param 闹钟工作模式
   *@param 日     (1-31)
   *@param 时    (0-23)
   *@param 分  (0-59)
   */
  void setAlarm2(eAlarm2Types_t alarmType,int16_t days,int8_t hours,int8_t minutes);
  /*!
   *@brief 判断闹钟是否被触发
   *@return eNoTrigger(没有触发),eAlarm1Trigger(闹钟1),eAlarm2Trigger(闹钟2),eAllTrigger(都触发)
   */
  eTrigger_t isAlarmTrig();
接线图

样例代码1 - 获取与设置时间
将时间设置为指定时间,串口每秒打印一次时间、温度数据
注意:内部温度传感器是用来补偿时钟的,精度不高,如果对温度准确度要求较高,请选择其他温度传感器
/*!
 * @file getTimeAndTemperature.ino
 * @brief Show current time and temperature of chip
 * @n Experiment phenomenon: 由用户自行设置初始时间,并从芯片获取实时时间和温度
 *
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"
DFRobot_DS323X rtc;
void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        delay(1000);
    }
    /*!
     *@brief 设置时间输出格式
     *@param eHours_t:e24hours, e12hours. default is e24hours
     */
    rtc.setHourSystem(rtc.e12hours);
    rtc.setTime(/*year,1900-2100*/2021, /*mouth,1-12*/2, /*date,1-31*/28, /*hour,0-23*/23,/*minute,0-59*/59,/*second,0-59*/55);//Set Set initial time .
}
void loop() {
    Serial.print(rtc.getYear(), DEC);//year
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);//month
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);//date
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());//day of week
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);//hour
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);//minute
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);//second
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    Serial.print("Temperature: ");
    /*!
     *@brief Get current temperature
     *@return Current temperautre, unit: ℃ 
     */
    Serial.print(rtc.getTemperatureC());
    Serial.println(" C");
    delay(1000);
    /*!
     *@brief Judge if it is power-down
     *@return If retrun true, power down, needs to reset time; false, work well.
     */
    if (rtc.isLostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
}
结果

样例代码2 - 闹钟触发查询
本样例是通过loop函数里面rtc.isAlarm();函数查询闹钟是否被触发的
注意:如果设置的时时钟时制和设置的闹钟时制不同,闹钟不会触发
/*!
 * @file setAlarmInQuery.ino
 * @brief Set alarm clock and trigger it in query
 * @n Experiment phenomenon: set the alarm clock to trigger at a sepcified time
 * @n           Trigger when the set time of clock is the same with the RTC time
 *
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"
DFRobot_DS323X rtc;
void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("failed to init chip, please check if the chip connection is fine");
        delay(1000);
    }
    //rtc.setHourSystem();//Set time mode, default in the 24 hours mode, e24hours, eAM, ePM.
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEverySecond,                 //repeat in every second
     *@n                                  eSecondsMatch,                //repeat in every minute
     *@n                                  eSecondsMinutesMatch,         //repeat in every hour
     *@n                                  eSecondsMinutesHoursMatch,    //repeat in every day
     *@n                                  eSecondsMinutesHoursDateMatch,//repeat in every month
     *@n                                  eSecondsMinutesHoursDayMatch, //repeat in every week  //Alarm1
     *@n                                  eUnknownAlarm1
     *@n                                  }eAlarm1Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     *@param seconds Alarm clock Second (second)
     */
    rtc.setAlarm1(rtc.eSecondsMatch,/*date,0-30*/1,/*hour,0-23*/0,/*minute,0-59*/0,/*second,0-59*/10);//Alarm1
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEveryMinute,                 //repeat in every minute
     *@n                                  eMinutesMatch,                //repeat in every hour
     *@n                                  eMinutesHoursMatch,           //repeat in every day
     *@n                                  eMinutesHoursDateMatch,       //repeat in every month
     *@n                                  eMinutesHoursDayMatch,        //repeat in every week  //Alarm2
     *@n                                  eUnknownAlarm2
     *@n                                  }eAlarm2Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     */
    rtc.setAlarm2(rtc.eEveryMinute,/*date,0-30*/1,/*hour,0-23*/0,/*minute,0-59*/0);//Alarm2
    if (rtc.isLostPower())
        rtc.setTime(/*year,1901-2099*/2021, /*mouth,1-12*/2, /*date,1-31*/28, /*hour,0-23*/23,/*minute,0-59*/59,\
                    /*second,0-59*/55);//Set Set initial time .
}
void loop() {
    /*!
     *@brief Judge if the alarm clock is triggered
     *@return eNoTrigger          // No alarm is triggered
     *@n      eAlarm1Trigger      // Alarm1 is triggered
     *@n      eAlarm2Trigger      // Alarm2 is triggered
     *@n      eAllTrigger         // All alarms are triggered
     */
    if (rtc.isAlarmTrig() == rtc.eAlarm1Trigger){ // If the alarm bit is set
        Serial.println("Alarm1 clock is triggered.");
        /*!
         *@brief Clear trigger flag
         */
        rtc.clearAlarm();
    }else if (rtc.isAlarmTrig() == rtc.eAlarm2Trigger){
        Serial.println("Alarm2 clock is triggered.");
        rtc.clearAlarm();
    }else if (rtc.isAlarmTrig() == rtc.eAllTrigger){
        Serial.println("Both Alarm clocks are triggered.");
        rtc.clearAlarm();
    }
    Serial.print(rtc.getYear(), DEC);
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    if (rtc.isLostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
    delay(1000);
}
结果

样例代码3 - 闹钟触发中断
本样例是通过时钟模块的INT引脚来判断闹钟是否被触发的
注意:如果设置的时钟时制和设置的闹钟时制不同,闹钟不会触发
本样例需要将传感器的INT引脚连接到主控板相应的中断引脚(样例选用的UNO中断引脚D2)
| AVR系列中断引脚与中断号 | |||||||
| Uno,Nano,Mini | |||||||
| 其他328主板 | 中断引脚 | D2 | D3 | ||||
| 中断号 | 0 | 1 | |||||
| Mega2560 | 中断引脚 | D2 | D3 | D21 | D20 | D19 | D18 | 
| 中断号 | 0 | 1 | 2 | 3 | 4 | 5 | |
| Leonardo,其他32u4主板 | 中断引脚 | D3 | D2 | D0 | D1 | D7 | |
| 中断号 | 0 | 1 | 2 | 3 | 4 | 
/*!
 * @file setAlarmInterrupt.ino
 * @brief Set alarm, and use interrput pin to trigger it
 * @n Experiment phenomenon: set the alarm clock to trigger at a specified time 
 * @n                        connect int pin with DIGITALPIN2
 * @n                        print information on serial port after the alarm clock is triggered.
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"
volatile  int8_t alarmFlag = 0;
DFRobot_DS323X rtc;
void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("failed to init chip, please check if the chip connection is correct. ");
        delay(1000);
    }
    /*!
     *@brief Set the value of pin sqw
     *@param mode eSquareWave_OFF  = 0x1C // Not output square wave, enter interrupt mode
     *@n          eSquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n          eSquareWave_1kHz = 0x08 // 1kHz square wave
     *@n          eSquareWave_4kHz = 0x10 // 4kHz square wave
     *@n          eSquareWave_8kHz = 0x18 // 8kHz square wave
     */
    rtc.writeSqwPinMode(rtc.eSquareWave_OFF);
    /*!
     *@brief enable Alarm1 interrupt
     */
    rtc.enableAlarm1Int();
    /*!
     *@brief disable Alarm1 interrupt
     */
    //rtc.disableAlarm1Int();
    /*!
     *@brief enable Alarm2 interrupt
     */
    rtc.enableAlarm2Int();
    /*!
     *@brief disable Alarm2 interrupt
     */
    //rtc.disableAlarm2Int();
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEverySecond,                 //repeat in every second
     *@n                                  eSecondsMatch,                //repeat in every minute
     *@n                                  eSecondsMinutesMatch,         //repeat in every hour
     *@n                                  eSecondsMinutesHoursMatch,    //repeat in every day
     *@n                                  eSecondsMinutesHoursDateMatch,//repeat in every month
     *@n                                  eSecondsMinutesHoursDayMatch, //repeat in every week  //Alarm1
     *@n                                  eUnknownAlarm1
     *@n                                  }eAlarm1Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     *@param seconds Alarm clock Second (second)
     */
    rtc.setAlarm1(rtc.eSecondsMatch,/*date,0-31*/1,/*hour,0-23*/0,/*minute,0-59*/0,/*second,0-59*/10);//Alarm1
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEveryMinute,                 //repeat in every minute
     *@n                                  eMinutesMatch,                //repeat in every hour
     *@n                                  eMinutesHoursMatch,           //repeat in every day
     *@n                                  eMinutesHoursDateMatch,       //repeat in every month
     *@n                                  eMinutesHoursDayMatch,        //repeat in every week  //Alarm2
     *@n                                  eUnknownAlarm2
     *@n                                  }eAlarm2Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     */
    rtc.setAlarm2(rtc.eEveryMinute,/*date,0-31*/1,/*hour,0-23*/0,/*minute,0-59*/0);//Alarm2
    /*!
     *@brief Judge if it is power-down 
     *@return if return true, power-down, time needs to reset; false, work well
     */
    if (rtc.isLostPower())
        rtc.setTime(/*year,1900-2099*/2021, /*mouth,1-12*/2, /*date,1-31*/28, /*hour,0-23*/23,/*minute,0-59*/59,/*second,0-59*/55);//Set Set initial time .
    #if defined(ESP32) || defined(ESP8266)||defined(ARDUINO_SAM_ZERO)
    attachInterrupt(digitalPinToInterrupt(D6)/*Query the interrupt number of the D6 pin*/,interrupt,FALLING);
    #else
    /*    The Correspondence Table of AVR Series Arduino Interrupt Pins And Terminal Numbers
    * ---------------------------------------------------------------------------------------
    * |                                        |  DigitalPin  | 2  | 3  |                   |
    * |    Uno, Nano, Mini, other 328-based    |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  |                   |
    * |-------------------------------------------------------------------------------------|
    * |                                        |    Pin       | 2  | 3  | 21 | 20 | 19 | 18 |
    * |               Mega2560                 |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  | 5  |
    * |-------------------------------------------------------------------------------------|
    * |                                        |    Pin       | 3  | 2  | 0  | 1  | 7  |    |
    * |    Leonardo, other 32u4-based          |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  |    |
    * |--------------------------------------------------------------------------------------
    */
    /*                      The Correspondence Table of micro:bit Interrupt Pins And Terminal Numbers
    * ---------------------------------------------------------------------------------------------------------------------------------------------
    * |             micro:bit                       | DigitalPin |P0-P20 can be used as an external interrupt                                     |
    * |  (When using as an external interrupt,      |---------------------------------------------------------------------------------------------|
    * |no need to set it to input mode with pinMode)|Interrupt No|Interrupt number is a pin digital value, such as P0 interrupt number 0, P1 is 1 |
    * |-------------------------------------------------------------------------------------------------------------------------------------------|
    */
    attachInterrupt(/*Interrupt No*/0,interrupt,FALLING);//Open the external interrupt 0, connect INT1/2 to the digital pin of the main control: 
    //UNO(2), Mega2560(2), Leonardo(3), microbit(P0).
    #endif
}
void loop() {
    Serial.print(rtc.getYear(), DEC);
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    if(alarmFlag == 1){
        alarmFlag = 0;
        Serial.println("Alarm clock is triggered.");
        delay(1000);
        rtc.clearAlarm();
    }
    else
        delay(1000);
    if (rtc.isLostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
}
void interrupt(){
  alarmFlag = 1;
}
结果

样例代码4 - 低功耗与唤醒
本样例是在设置完时钟、闹钟之后让主控板进入低功耗,当闹钟触发后主控板退出低功耗,打印10次时间后又进入低功耗。
本样例需要将传感器的INT引脚连接到主控板相应的中断引脚(样例选用的UNO中断引脚D2)。
 /*!
 * @file lowPowerAndWakeUp.ino
 * @brief Set alarm, and use interrput pin to trigger MCU wake up
 * @n Experiment phenomenon: Set the alarm clock to trigger at a specified time.
 * @n                        Connect SQW pin with DIGITALPIN2.
 * @n                        Print information on serial port after the alarm clock is triggered.
 * @n                        This demo only works on avr
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"
#include <avr/sleep.h>
volatile  int8_t alarmFlag = 0;
DFRobot_DS323X rtc;
int t = 0;
void setup(void)
{
    Serial.begin(9600);
    /*Wait for the chip to be initialized completely, and then exit*/
    while(rtc.begin() != true){
        Serial.println("failed to init chip, please check if the chip connection is correct. ");
        delay(1000);
    }
    /*!
     *@brief Set the value of pin sqw
     *@param mode eSquareWave_OFF  = 0x1C // Not output square wave, enter interrupt mode
     *@n          eSquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n          eSquareWave_1kHz = 0x08 // 1kHz square wave
     *@n          eSquareWave_4kHz = 0x10 // 4kHz square wave
     *@n          eSquareWave_8kHz = 0x18 // 8kHz square wave
     */
    rtc.writeSqwPinMode(rtc.eSquareWave_OFF);
    rtc.enableAlarm1Int();
    //rtc.disableAlarm1Int();
    //rtc.enableAlarm2Int();
    rtc.disableAlarm2Int();
    //rtc.setHourSystem();    //set hour system of time, e24hours,e12hours, default is the 24 hours mode.
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEverySecond,
     *@n                                  eSecondsMatch,
     *@n                                  eSecondsMinutesMatch,
     *@n                                  eSecondsMinutesHoursMatch,
     *@n                                  eSecondsMinutesHoursDateMatch,
     *@n                                  eSecondsMinutesHoursDayMatch, //Alarm1
     *@n                                  eUnknownAlarm1
     *@n                                  }eAlarm1Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     *@param seconds Alarm clock Second (second)
     */
    rtc.setAlarm1(rtc.eSecondsMatch,/*date,0-30*/1,/*hour,0-23*/0,/*minute,0-59*/0,/*second,0-59*/15);//Alarm1
    /*!
     *@brief Set alarm clock
     *@param alarmType Alarm clock working mode typedef enum{
     *@n                                  eEveryMinute,
     *@n                                  eMinutesMatch,
     *@n                                  eMinutesHoursMatch,
     *@n                                  eMinutesHoursDateMatch,
     *@n                                  eMinutesHoursDayMatch,        //Alarm2
     *@n                                  eUnknownAlarm2
     *@n                                  }eAlarm2Types_t;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param minutes Alarm clock Minute (minute)
     */
    //rtc.setAlarm2(rtc.eEveryMinute,/*date,0-30*/1,/*hour,1-12 in 12hours,0-23 in 24hours*/0,/*minute,0-59*/0);//Alarm2
    rtc.setTime(/*year,1900-2100*/2021, /*mouth,1-12*/2, /*date,1-31*/28, /*hour,0-23*/23,/*minute,0-59*/59,/*second,0-59*/55);//Set Set initial time .
    #if defined(ESP32) || defined(ESP8266)||defined(ARDUINO_SAM_ZERO)
    attachInterrupt(digitalPinToInterrupt(D6)/*Query the interrupt number of the D6 pin*/,interrupt,FALLING);
    #else
    /*    The Correspondence Table of AVR Series Arduino Interrupt Pins And Terminal Numbers
    * ---------------------------------------------------------------------------------------
    * |                                        |  DigitalPin  | 2  | 3  |                   |
    * |    Uno, Nano, Mini, other 328-based    |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  |                   |
    * |-------------------------------------------------------------------------------------|
    * |                                        |    Pin       | 2  | 3  | 21 | 20 | 19 | 18 |
    * |               Mega2560                 |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  | 5  |
    * |-------------------------------------------------------------------------------------|
    * |                                        |    Pin       | 3  | 2  | 0  | 1  | 7  |    |
    * |    Leonardo, other 32u4-based          |--------------------------------------------|
    * |                                        | Interrupt No | 0  | 1  | 2  | 3  | 4  |    |
    * |--------------------------------------------------------------------------------------
    */
    /*                      The Correspondence Table of micro:bit Interrupt Pins And Terminal Numbers
    * ---------------------------------------------------------------------------------------------------------------------------------------------
    * |             micro:bit                       | DigitalPin |P0-P20 can be used as an external interrupt                                     |
    * |  (When using as an external interrupt,      |---------------------------------------------------------------------------------------------|
    * |no need to set it to input mode with pinMode)|Interrupt No|Interrupt number is a pin digital value, such as P0 interrupt number 0, P1 is 1 |
    * |-------------------------------------------------------------------------------------------------------------------------------------------|
    */
    attachInterrupt(/*Interrupt No*/0,interrupt,FALLING);//Open the external interrupt 0, connect INT1/2 to the digital pin of the main control: 
    //UNO(2), Mega2560(2), Leonardo(3), microbit(P0).
    #endif
    /*!
     *@brief Set avr sleep mode
     */
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);
    sleep_cpu();
    sleep_enable();
}
void loop() {
    if(alarmFlag == 1){
        /*!
         *@brief Judge if the alarm clock is triggered
         *@return eNoTrigger          // No alarm is triggered
         *@n      eAlarm1Trigger      // Alarm1 is triggered
         *@n      eAlarm2Trigger      // Alarm2 is triggered
         *@n      eAllTrigger         // All alarms are triggered
         */
        if (rtc.isAlarmTrig() == rtc.eAlarm1Trigger){ // If the alarm bit is set
            Serial.println("Alarm1 clock is triggered.");
            /*!
             *@brief Clear trigger flag
             */
            rtc.clearAlarm();
        }else if (rtc.isAlarmTrig() == rtc.eAlarm2Trigger){
            Serial.println("Alarm2 clock is triggered.");
            rtc.clearAlarm();
        }else if (rtc.isAlarmTrig() == rtc.eAllTrigger){
            Serial.println("Both Alarm clocks are triggered.");
            rtc.clearAlarm();
        }
        alarmFlag = 0;
        while (t < 10){
            Serial.print(rtc.getYear(), DEC);
            Serial.print('/');
            Serial.print(rtc.getMonth(), DEC);
            Serial.print('/');
            Serial.print(rtc.getDate(), DEC);
            Serial.print(" (");
            Serial.print(rtc.getDayOfWeek());
            Serial.print(") ");
            Serial.print(rtc.getHour(), DEC);
            Serial.print(':');
            Serial.print(rtc.getMinute(), DEC);
            Serial.print(':');
            Serial.print(rtc.getSecond(), DEC);
            Serial.print(' ');
            /*if rtc works in 24hours mode,this function doesn't print anything*/
            Serial.print(rtc.getAMorPM());
            Serial.println();
            delay(1000);
            t = t + 1;
        }
        t = 0;
    }
    else
        delay(1000);
    /*!
     *@brief Judge if it is power-down 
     *@return if return true, power-down, time needs to reset; false, work well
     */
    if (rtc.isLostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
    sleep_enable();
    //energy.PowerDown();
}
void interrupt(){
    alarmFlag = 1;
    sleep_disable();
}
结果

样例代码5 - 获取NTP时间
本样例是从NTP服务器上获取当前时间然后每秒打印
**注意:**本样例需要联网,所以需要ESP32系列主控板
如果没有编译环境,请配置ESP32编译环境
在配置好ESP32编译环境之后,需要选择FireBeetle-ESP32开发板和端口
/*!
 * @file getTimefromNTP.ino
 * @brief Get time from ntpServer and show current time ,only work on ESP32
 *
 * @copyright    Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2021-2-23
 * @url https://github.com/DFRobot/DFRobot_DS323X
 */
#include "DFRobot_DS323X.h"
#include <WiFi.h>
#include "time.h"
DFRobot_DS323X rtc;
const char* ssid       = "WIFI_ID";//wlan information
const char* password   = "WIFI_PASSWORD";
const char* ntpServer = "ntp.ntsc.ac.cn";//local ntp server
const long  gmtOffset_sec = 8*3600;
const int   daylightOffset_sec = 0;
void getTimeFromNTP()
{
    struct tm timeinfo;
    if(!getLocalTime(&timeinfo)){
        Serial.println("Failed to obtain time");
        return;
    }
    rtc.setTime(timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour,timeinfo.tm_min,timeinfo.tm_sec);//Set Set initial time .
}
void setup()
{
    Serial.begin(9600);
    while(rtc.begin() != true){
        Serial.println("Failed to init chip, please check if the chip connection is fine. ");
        delay(1000);
    }
    //connect to WiFi
    Serial.printf("Connecting to %s ", ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println(" CONNECTED");
    //init and get the time
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    getTimeFromNTP();
    //disconnect WiFi as it's no longer needed
    WiFi.disconnect(true);
    WiFi.mode(WIFI_OFF);
}
void loop()
{
    Serial.print(rtc.getYear(), DEC);
    Serial.print('/');
    Serial.print(rtc.getMonth(), DEC);
    Serial.print('/');
    Serial.print(rtc.getDate(), DEC);
    Serial.print(" (");
    Serial.print(rtc.getDayOfWeek());
    Serial.print(") ");
    Serial.print(rtc.getHour(), DEC);
    Serial.print(':');
    Serial.print(rtc.getMinute(), DEC);
    Serial.print(':');
    Serial.print(rtc.getSecond(), DEC);
    Serial.println(' ');
    delay(1000);
}
结果

常见问题
还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!
更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。
