将时间设置为指定时间,串口每秒打印一次时间、温度数据
注意:内部温度补偿器精度不高,如果对温度准确度要求较高,请额外添加精准的温度传感器
/*!
* @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!");
}
}
|
本样例是通过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);
}
|
本样例是通过时钟模块的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;
}
|
本样例是在设置完时钟、闹钟之后让主控板进入低功耗,当闹钟触发后主控板退出低功耗,打印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();
}
|
本样例是从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);
}
|
常见问题
还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!
更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。
|
更多
DFRobot商城购买链接
|