(SKU:DFR0641)DS3231M MEMS RTC实时时钟模块

来自DFRobot Product Wiki
跳转至: 导航搜索

目录

简介

本款实时时钟模块(RTC)采用DS3231M芯片方案,芯片内部集成了MEMS谐振器和温度补偿器,相比石英晶振时钟,降低了机械失效风险,因此该模块可以用于强烈震动的环境。在-45°C至+85°C范围内,DS3231M的精度保持在±5ppm (±0.432秒/天)。
基于MEMS的RTC已通过AEC-Q100认证的冲击和振动测试。它可以承受2900g的机械冲击(JESD22-B104C Condition-H)和20g的变频振动(JESD22-B103B Condition-1)。 DS3231M可以用CR1220纽扣电池供电,断开主电源时仍可保持精确计时。RTC可提供秒、分、时、星期、日、月和年的信息,可以自动调整月末的日期、闰年修正。时钟格式可以是24小时或带AM/PM指示的12小时。提供两个可编程的日历闹钟和一个1Hz方波输出。另外,RST校验引脚可以作为微处理器复位的按键输入。

应用场景

  • 事件提醒器
  • MCU外部唤醒源
  • 时钟、闹钟输出


优势和特性

  • 内部集成MEMS谐振器,抗摔性强,误差小
  • 内部集成温度补偿器,温度漂移小
  • 完整的时钟日历功能
  • 两个可编程日历闹钟

技术规格

  • 工作电压:3.3V ~ 5.5V
  • 外部供电工作电流:130uA
  • 时钟保持电池电流:2uA
  • IIC地址:0x68
  • 计时范围:1970 ~ 2100年
  • 接口方式:2.54间距排针孔全功能引出
  • 工作温度:-40℃ ~ +85℃
  • 计时精度:±5ppm (±0.432秒/天)
  • 产品尺寸:22.5*21mm


引脚说明

  • 顶部示意图
  • 底部示意图
  • 尺寸图


引脚说明
序号 丝印 功能描述
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
    • 若干 杜邦线

关于如何安装库文件,点击链接

  • API接口函数列表
  /*!
   *@功能 初始化芯片
   *@返回 True代表IIC通信成功,false代表通信失败
   */
  bool begin(void);
  /*!
   *@功能 获取当前时间数据
   */
  void getNowTime();
  
  /*!
   *@功能 获取年
   *@返回 年
   */
  uint16_t year();
  
  /*!
   *@功能 获取月
   *@返回 月
   */
  uint8_t  month();
  
  /*!
   *@功能 获取日
   *@返回 日
   */
  uint8_t  day();
  
  /*!
   *@功能 获取时
   *@返回 时
   */
  uint8_t  hour();
  
  /*!
   *@功能 获取分
   *@返回 分
   */
  uint8_t  minute();
  
  /*!
   *@功能 获取秒
   *@返回 秒
   */
  uint8_t  second();
  
  /*!
   *@功能 获取星期几
   *@返回 星期几
   */
  const char* getDayOfTheWeek();
  
  /*!
   *@功能 设置年
   *@参数 年 
   */
  void setYear(uint8_t year);
  
  /*!
   *@功能 设置月
   *@参数 月
   */
  void setMonth(uint8_t month);
  
  /*!
   *@功能 设置日
   *@参数 日
   */
  void setDate(uint8_t date);
  
  /*!
   *@功能 设置时和时制
   *@参数 小时:12小时内1-12小时,24小时内0-23小时
   *@参数 模式:e24hours, eAM, ePM
   */
  void setHour(uint8_t hour, ehours mode);
  
  /*!
   *@功能 设置分 
   *@参数 分
   */
  void setMinute(uint8_t minute);
  
  /*!
   *@功能 设置秒
   *@参数 秒
   */
  void setSecond(uint8_t second);
  
  /*!
   *@功能 将上次编译时间设置为当前时间
   */
  void dateTime();
  
  /*!
   *@功能 校准当前时间
   */
  void adjust();
  
  /*!
   *@功能 输出时间AM或PM 
   */
  const char* getAMorPM();
  
  /*!
   *@功能 获取当前温度
   *@返回 当前温度,单位为摄氏度
   */
  float getTemperatureC();
  
  /*!
   *@功能 判断是否掉电
   *@返回 true为发生掉电,需要重设时间,false为未发生掉电
   */
  bool lostPower(void);
  
  /*!
   *@功能 读取sqw引脚模式
   *@返回 eDS3231M_OFF             = 0x01 // Off
   *@n    eDS3231M_SquareWave_1Hz  = 0x00 // 1Hz square wave
   *@n    eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
   *@n    eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
   *@n    eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
   */
  eDs3231MSqwPinMode_t readSqwPinMode();
  
  /*!
   *@功能 设置sqw引脚模式
   *@参数 模式 eDS3231M_OFF             = 0x01 // Off
   *@n         eDS3231M_SquareWave_1Hz  = 0x00 // 1Hz square wave
   *@n         eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
   *@n         eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
   *@n         eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
   */
  void writeSqwPinMode(eDs3231MSqwPinMode_t mode);
  
  /*!
   *@功能 设置闹钟
   *@参数 闹钟工作模式 typedef enum{
   *@n                                eEverySecond,
   *@n                                eSecondsMatch,
   *@n                                eSecondsMinutesMatch,
   *@n                                eSecondsMinutesHoursMatch,
   *@n                                eSecondsMinutesHoursDateMatch,
   *@n                                eSecondsMinutesHoursDayMatch, //Alarm1
   *@n                                eEveryMinute,
   *@n                                eMinutesMatch,
   *@n                                eMinutesHoursMatch,
   *@n                                eMinutesHoursDateMatch,
   *@n                                eMinutesHoursDayMatch,        //Alarm2
   *@n                                eUnknownAlarm
   *@n                                }eAlarmTypes;
   *@参数 Alarm clock Day (day)
   *@参数 Alarm clock Hour (hour)
   *@参数 mode:   e24hours, eAM, ePM
   *@参数 minutes Alarm clock (minute)
   *@参数 seconds Alarm clock (second)
   */
  void setAlarm(const uint8_t alarmType,int16_t days,int8_t hours,ehours mode,
                int8_t minutes,int8_t seconds, const bool state  = true);
  
  /*!
   *@功能 判断闹钟是否被触发
   *@返回 true为触发,false为未触发
   */
  bool isAlarm();
  /*!
   *@功能 清除闹钟标志位
   */
  void clearAlarm();
  
  /*!
   *@功能 启用或禁用闹钟
   */
  void enAbleAlarm1Int();
  void disAbleAlarm1Int();
  void enAbleAlarm2Int();
  void disAbleAlarm2Int();
  
  /*!
   *@功能 启用32k输出
   */
  void enAble32k();
  
  /*!
   *@功能 禁用32k输出
   */
  void disAble32k();
                         


接线图

DFR0641-CONNECT1.jpg


样例代码1 - 获取与设置时间

将时间设置为指定时间,串口每秒打印一次时间、温度数据

注意:内部温度补偿器精度不高,如果对温度准确度要求较高,请额外添加精准的温度传感器

/*!
 * @file getTimeAndTemperature.ino
 * @brief Show current time 
 * @n Experiment phenomenon: read data every 1 seconds and print it on serial port. 
 *
 * @copyright	Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2019-08-19
 * @url https://github.com/DFRobot/DFRobot_DS3231M
 */
#include "DFRobot_DS3231M.h"

DFRobot_DS3231M 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 Set the vaule of pin sqw
     *@param mode eDS3231M_OFF             = 0x01 // Not output square wave, enter interrupt mode
     *@n          eDS3231M_SquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n          eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
     *@n          eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
     *@n          eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
     */
    rtc.writeSqwPinMode(eDS3231M_SquareWave_1Hz);
    /*!
     *@brief Read the value of pin sqw
     *@return mode eDS3231M_OFF             = 0x01 // Off
     *@n           eDS3231M_SquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n           eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
     *@n           eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
     *@n           eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
     */
    //rtc.readSqwPinMode();
    /*!
     *@brief Set the last compiled time as the current time
     */
    //rtc.dateTime();//If users use this function, please don't set time by other way
    rtc.setYear(19);//Set year, default in the 21st century, input negative number for years in the 20th century.
    rtc.setMonth(10);
    rtc.setDate(23);
    /*!
     *@brief Set the hours and 12hours or 24hours
     *@param hour:1-12 in 12hours,0-23 in 24hours
     *@param mode:e24hours, eAM, ePM
     */
    rtc.setHour(0,e24hours);
    rtc.setMinute(59);
    rtc.setSecond(40);
    
    rtc.adjust();

    /*!
     *@brief enable the 32k output (default is enable)
     */
    //rtc.disAble32k();

    /*!
     *@brief disable the 32k output 
     */
    //rtc.enAble32k();
}

void loop() {
    /*!
     *@brief Get current time data 
     *@return Current time data
     */
    rtc.getNowTime();
    Serial.print(rtc.year(), DEC);//year
    Serial.print('/');
    Serial.print(rtc.month(), DEC);//month
    Serial.print('/');
    Serial.print(rtc.day(), DEC);//date
    Serial.print(" (");
    Serial.print(rtc.getDayOfTheWeek());//day of week
    Serial.print(") ");
    Serial.print(rtc.hour(), DEC);//hour
    Serial.print(':');
    Serial.print(rtc.minute(), DEC);//minute
    Serial.print(':');
    Serial.print(rtc.second(), 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.lostPower()) {
        Serial.println("RTC lost power, plrase reset the time!");
    }
}
                         

结果

DFR0641时间获取结果.png

样例代码2 - 闹钟触发查询

本样例是通过loop函数里面rtc.isAlarm();函数查询闹钟是否被触发的

注意:如果设置的时时钟时制和设置的闹钟时制不同,闹钟不会触发,例如:时钟时制为24小时时制,闹钟为12小时时制,则闹钟不能正常触发

/*!
 * @file setAlarmInQuery.ino
 * @brief Set alarm clock 
 * @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 (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2019-08-19
 * @url https://github.com/DFRobot/DFRobot_DS3231M
 */
#include "DFRobot_DS3231M.h"

DFRobot_DS3231M 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 Set the vaule of pin sqw
     *@param mode eDS3231M_OFF             = 0x01 // Not output square wave, enter interrupt mode
     *@n          eDS3231M_SquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n          eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
     *@n          eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
     *@n          eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
     */
    rtc.writeSqwPinMode(eDS3231M_OFF);
    /*!
     *@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                                  eEveryMinute,
     *@n                                  eMinutesMatch,
     *@n                                  eMinutesHoursMatch,
     *@n                                  eMinutesHoursDateMatch,
     *@n                                  eMinutesHoursDayMatch,        //Alarm2
     *@n                                  eUnknownAlarm
     *@n                                  }eAlarmTypes;
     *@param days    Alarm clock Day (day)
     *@param hours   Alarm clock Hour (hour)
     *@param mode:   e24hours, eAM, ePM
     *@param minutes Alarm clock (minute)
     *@param seconds Alarm clock (second)
     */
    rtc.setAlarm(eSecondsMatch,/*date,0-30*/27,/*hour,1-12 in 12hours,0-23 in 24hours*/12,eAM,/*minute,0-59*/0,/*second,0-59*/0);
    rtc.setAlarm(eMinutesHoursDateMatch,/*date,0-30*/27,/*hour,1-12 in 12hours,0-23 in 24hours*/12,eAM,/*minute,0-59*/0,/*second,0-59*/0);
    
    rtc.setYear(19);//Set year, default in the 21st century,
    rtc.setMonth(8);
    rtc.setDate(26);
    /*!
     *@brief Set the hours and 12hours or 24hours
     *@param hour:1-12 in 12hours,0-23 in 24hours
     *@param mode:e24hours, eAM, ePM
     */
    rtc.setHour(11,ePM);//1-12 in 12hours,0-23 in 24hours
    rtc.setMinute(59);
    rtc.setSecond(50);
    rtc.adjust();
}
void loop() {
    /*!
     *@brief Get current time data 
     *@return Current time data
     */
    rtc.getNowTime();
    /*!
     *@brief Judge if the alarm clock is triggered
     *@return true, triggered; false, not triggered
     */
    if (rtc.isAlarm()){ // If the alarm bit is set
        Serial.println("Alarm clock is triggered.");
        /*!
         *@brief Clear trigger flag
         */
        rtc.clearAlarm();
    }
    Serial.print(rtc.year(), DEC);
    Serial.print('/');
    Serial.print(rtc.month(), DEC);
    Serial.print('/');
    Serial.print(rtc.day(), DEC);
    Serial.print(" (");
    Serial.print(rtc.getDayOfTheWeek());
    Serial.print(") ");
    Serial.print(rtc.hour(), DEC);
    Serial.print(':');
    Serial.print(rtc.minute(), DEC);
    Serial.print(':');
    Serial.print(rtc.second(), DEC);
    Serial.print(' ');
    /*if rtc works in 24hours mode,this function doesn't print anything*/
    Serial.print(rtc.getAMorPM());
    Serial.println();
    if (rtc.lostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
    delay(1000);
}
                         

结果

DFR0641查询闹钟结果.png

样例代码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 SQW pin with DIGITALPIN2
 * @n                        print information on serial port after the alarm clock is triggered.
 * @copyright	Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2019-08-19
 * @url https://github.com/DFRobot/DFRobot_DS3231M
 */
#include "DFRobot_DS3231M.h"

volatile  int8_t alarmFlag = 0;

DFRobot_DS3231M 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 eDS3231M_OFF             = 0x01 // Not output square wave, enter interrupt mode
     *@n          eDS3231M_SquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n          eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
     *@n          eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
     *@n          eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
     */
    rtc.writeSqwPinMode(eDS3231M_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,
     *@n                                  eSecondsMatch,
     *@n                                  eSecondsMinutesMatch,
     *@n                                  eSecondsMinutesHoursMatch,
     *@n                                  eSecondsMinutesHoursDateMatch,
     *@n                                  eSecondsMinutesHoursDayMatch, //Alarm1
     *@n
     *@n                                  eEveryMinute,
     *@n                                  eMinutesMatch,
     *@n                                  eMinutesHoursMatch,
     *@n                                  eMinutesHoursDateMatch,
     *@n                                  eMinutesHoursDayMatch,        //Alarm2
     *@n                                  eUnknownAlarm
     *@n                                  }eAlarmTypes;
     *@param days    Alarm clock (day)
     *@param hours   Alarm clock (hour)
     *@param mode:   e24hours, eAM, ePM
     *@param minutes Alarm clock (minute)
     *@param seconds Alarm clock (second)
     */
    //Alarm1
    rtc.setAlarm(eSecondsMatch,/*date,0-30*/30,/*hour,1-12 in 12hours,0-23 in 24hours*/15,e24hours,/*minute,0-59*/12,/*second,0-59*/35);
    //Alarm2
    rtc.setAlarm(eMinutesHoursDayMatch,/*date,0-30*/30,/*hour,1-12 in 12hours,0-23 in 24hours*/15,e24hours,
                 /*minute,0-59*/13,/*second,0-59, this argument doesn't work in Alarm2*/42);
    /*!
     *@brief Judge if it is power-down 
     *@return if return true, power-down, time needs to reset; false, work well
     */
    if (rtc.lostPower()) {
        Serial.println("RTC lost power, lets set the time!");
        /*!
         *@brief Adjust the current time
         */
        rtc.setYear(19);//Set year, default in the 21st century. 
        rtc.setMonth(9);
        rtc.setDate(30);
        /*!
         *@brief Set the hours and 12hours or 24hours
         *@param hour:1-12 in 12hours,0-23 in 24hours
         *@param mode:e24hours, eAM, ePM
         */
        rtc.setHour(15,e24hours);
        rtc.setMinute(12);
        rtc.setSecond(30);
        rtc.adjust();
    }
    attachInterrupt(0, interrupt, FALLING);
}
void loop() {
    /*!
     *@brief Judge if the alarm clock is triggered
     *@return true, triggered; false, not triggered
     */
    rtc.getNowTime();
    Serial.print(rtc.year(), DEC);
    Serial.print('/');
    Serial.print(rtc.month(), DEC);
    Serial.print('/');
    Serial.print(rtc.day(), DEC);
    Serial.print(" (");
    Serial.print(rtc.getDayOfTheWeek());
    Serial.print(") ");
    Serial.print(rtc.hour(), DEC);
    Serial.print(':');
    Serial.print(rtc.minute(), DEC);
    Serial.print(':');
    Serial.print(rtc.second(), 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.lostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
}

void interrupt(){
  alarmFlag = 1;
}
                         

结果

DFR0641闹钟触发中断.png

样例代码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 (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2019-08-19
 * @url https://github.com/DFRobot/DFRobot_DS3231M
 */
#include "DFRobot_DS3231M.h"
#include <avr/sleep.h>

volatile  int8_t alarmFlag = 0;

DFRobot_DS3231M 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 eDS3231M_OFF             = 0x01 // Not output square wave, enter interrupt mode
     *@n          eDS3231M_SquareWave_1Hz  = 0x00 // 1Hz square wave
     *@n          eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
     *@n          eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
     *@n          eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
     */
    rtc.writeSqwPinMode(eDS3231M_OFF);
    rtc.enAbleAlarm1Int();
    //rtc.disAbleAlarm1Int();
    rtc.enAbleAlarm2Int();
    //rtc.disAbleAlarm2Int();
    /*!
     *@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                                  eEveryMinute,
     *@n                                  eMinutesMatch,
     *@n                                  eMinutesHoursMatch,
     *@n                                  eMinutesHoursDateMatch,
     *@n                                  eMinutesHoursDayMatch,        //Alarm2
     *@n                                  eUnknownAlarm
     *@n                                  }eAlarmTypes;
     *@param days    Alarm clock (day)
     *@param hours   Alarm clock (hour)
     *@param mode:   e24hours, eAM, ePM
     *@param minutes Alarm clock (minute)
     *@param seconds Alarm clock (second)
     */
    rtc.setAlarm(eSecondsMatch,/*date,0-30*/29,/*hour,1-12 in 12hours,0-23 in 24hours*/9,e24hours,/*minute,0-59*/10,/*second,0-59*/40);
    
    rtc.setYear(19);//Set year, default in the 21st century. 
    rtc.setMonth(9);
    rtc.setDate(29);
    /*!
     *@brief Set the hours and 12hours or 24hours
     *@param hour:1-12 in 12hours,0-23 in 24hours
     *@param mode:e24hours, eAM, ePM
     */
    rtc.setHour(9,e24hours);
    rtc.setMinute(10);
    rtc.setSecond(30);
    rtc.adjust();
    attachInterrupt(0, interrupt, FALLING);
    
    /*!
     *@brief Set avr sleep mode
     */
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);
    sleep_cpu();
    sleep_enable();
    
}
void loop() {
    /*!
     *@brief Judge if the alarm clock is triggered
     *@return true, triggered; false, not triggered
     */
    if(alarmFlag == 1){
        Serial.println("Alarm is up");
        rtc.clearAlarm();
        alarmFlag = 0;
        while (t < 10){
            rtc.getNowTime();
            Serial.print(rtc.year(), DEC);
            Serial.print('/');
            Serial.print(rtc.month(), DEC);
            Serial.print('/');
            Serial.print(rtc.day(), DEC);
            Serial.print(" (");
            Serial.print(rtc.getDayOfTheWeek());
            Serial.print(") ");
            Serial.print(rtc.hour(), DEC);
            Serial.print(':');
            Serial.print(rtc.minute(), DEC);
            Serial.print(':');
            Serial.print(rtc.second(), 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.lostPower()) {
        Serial.println("RTC lost power, please reset the time!");
    }
    sleep_enable();
    //energy.PowerDown();
}


void interrupt(){
    alarmFlag = 1;
    sleep_disable();
}
                         

结果

DFR0641低功耗唤醒结果.png

样例代码5 - 获取NTP时间

本样例是从NTP服务器上获取当前时间然后每秒打印

注意:本样例需要联网,所以需要ESP32系列主控板

如果没有编译环境,请配置ESP32编译环境

在配置好ESP32编译环境之后,需要选择FireBeetle-ESP32开发板和端口

/*!
 * @file getTimefromNTP.ino
 * @brief Get time from ntpServer and show current time 
 * @n Experiment phenomenon: read data every second and print it on serial port. 
 * @n                        This demo only works on mpython
 *
 * @copyright	Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @author [LuoYufeng](yufeng.luo@dfrobot.com)
 * @version  V0.1
 * @date  2019-08-19
 * @url https://github.com/DFRobot/DFRobot_DS3231M
 */
#include "DFRobot_DS3231M.h"
#include <WiFi.h>
#include "time.h"

DFRobot_DS3231M rtc;

const char* ssid       = "dfrobotYanfa";//wlan information
const char* password   = "hidfrobot";

const char* ntpServer = "ntp.ntsc.ac.cn";//local ntp server
const long  gmtOffset_sec = 8*3600;  //GMT+08:00
const int   daylightOffset_sec = 0;


void getTimeFromNTP()
{
    struct tm timeinfo;
    if(!getLocalTime(&timeinfo)){
        Serial.println("Failed to obtain time");
        return;
    }
    
    rtc.setYear(timeinfo.tm_year - 100);
    rtc.setMonth(timeinfo.tm_mon + 1);
    rtc.setDate(timeinfo.tm_mday);
    rtc.setHour(timeinfo.tm_hour, e24hours);
    rtc.setMinute(timeinfo.tm_min);
    rtc.setSecond(timeinfo.tm_sec);
    rtc.adjust();
    
}

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()
{
    rtc.getNowTime();
    Serial.print(rtc.year(), DEC);//year
    Serial.print('/');
    Serial.print(rtc.month(), DEC);//month
    Serial.print('/');
    Serial.print(rtc.day(), DEC);//date
    Serial.print(" (");
    Serial.print(rtc.getDayOfTheWeek());//day of week
    Serial.print(") ");
    Serial.print(rtc.hour(), DEC);//hour
    Serial.print(':');
    Serial.print(rtc.minute(), DEC);//minute
    Serial.print(':');
    Serial.print(rtc.second(), DEC);//second
    Serial.println();
    delay(1000);
    
}
                         

结果

DFR0641获取NTP时间.png

常见问题

还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!


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


更多


DFshopping car1.png DFRobot商城购买链接

个人工具
名字空间

变换
操作
导航
工具箱