“RTC 1307”的版本间的差异

来自YFRobotwiki
跳转至: 导航搜索
应用示例
第63行: 第63行:
  
 
<source lang="c">
 
<source lang="c">
//thermometer - Measuring temperature, record temperature fluctuations
+
/* arduino sketch -- arduino print date and time with ds1307 RTC breakout
 +
  SETUP:
 +
    arduino (UNO R3)    ds1307
 +
      A4/SDA          →    SDA
 +
      A5/SCL          →    SCL
 +
      GND             →    GND
  
int lm35Pin = A0;
+
  Read More about DS1307: http://www.yfrobot.com.cn/wiki/index.php?title=RTC_1307
  
int temp = 0;
+
  BY YFROBOT
int tempMin = 100;
+
*/
int tempMax = -100;
+
  
void setup() {
+
#include <Wire.h>  // must be incuded here so that Arduino library object file references work
  // put your setup code here, to run once:
+
#include <RtcDS1307.h>
  Serial.begin(9600);
+
}
+
  
void loop() {
+
RtcDS1307<TwoWire> Rtc(Wire);
   // put your main code here, to run repeatedly:
+
 
   for (int i = 0; i <= 9; i++) {
+
void setup () {
     int signVal = analogRead(lm35Pin);
+
   Serial.begin(115200);
    temp += (signVal * 5 * 100) / 1024;
+
 
     signVal = 0;
+
  Serial.print("compiled: ");
    delay(80);
+
  Serial.print(__DATE__);      // 编译文档的日期
 +
  Serial.println(__TIME__);    // 编译文档的时间
 +
 
 +
  //--------RTC SETUP ------------
 +
  // if you are using ESP-01 then uncomment the line below to reset the pins to
 +
   // the available pins for SDA, SCL
 +
  // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
 +
  Rtc.Begin();
 +
 
 +
  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); // 存储当前编译的时间及日期并打印
 +
  printDateTime(compiled);
 +
  Serial.println();
 +
 
 +
  if (!Rtc.IsDateTimeValid()) {
 +
     if (Rtc.LastError() != 0) {
 +
      // we have a communications error
 +
      // see https://www.arduino.cc/en/Reference/WireEndTransmission for
 +
      // what the number means
 +
      Serial.print("RTC communications error = ");
 +
      Serial.println(Rtc.LastError());
 +
     } else {
 +
      // Common Causes:
 +
      //    1) first time you ran and the device wasn't running yet
 +
      //    2) the battery on the device is low or even missing
 +
 
 +
      Serial.println("RTC lost confidence in the DateTime!");
 +
      // following line sets the RTC to the date & time this sketch was compiled
 +
      // it will also reset the valid flag internally unless the Rtc device is
 +
      // having an issue
 +
 
 +
      Rtc.SetDateTime(compiled);
 +
    }
 
   }
 
   }
  temp = temp / 10;
+
 
 
+
   if (!Rtc.GetIsRunning()) {
   if (temp > tempMax) {
+
     Serial.println("RTC was not actively running, starting now");
     tempMax = temp;
+
    Rtc.SetIsRunning(true);
 
   }
 
   }
   if (temp < tempMin) {
+
 
     tempMin = temp;
+
  RtcDateTime now = Rtc.GetDateTime();  // 获取当前DS1307时间及日期
 +
   if (now < compiled) {
 +
     Serial.println("RTC is older than compile time!  (Updating DateTime)");
 +
    Rtc.SetDateTime(compiled);
 +
  } else if (now > compiled) {
 +
    Serial.println("RTC is newer than compile time. (this is expected)");
 +
    // Rtc.SetDateTime(compiled);
 +
  } else if (now == compiled) {
 +
    Serial.println("RTC is the same as compile time! (not expected but all is fine)");
 
   }
 
   }
 
  Serial.print("Temp:");
 
  Serial.print(temp);
 
  Serial.print(" Cels,");
 
  Serial.print(" Min:");
 
  Serial.print(tempMin);
 
  Serial.print(" Max:");
 
  Serial.println(tempMax);
 
  temp = 0;
 
  delay(500);
 
 
}
 
}
 +
 +
void loop () {
 +
  if (!Rtc.IsDateTimeValid()) {
 +
    if (Rtc.LastError() != 0) {
 +
      // we have a communications error
 +
      // see https://www.arduino.cc/en/Reference/WireEndTransmission for
 +
      // what the number means
 +
      Serial.print("RTC communications error = ");
 +
      Serial.println(Rtc.LastError());
 +
    } else {
 +
      // Common Causes:
 +
      //    1) the battery on the device is low or even missing and the power line was disconnected
 +
      Serial.println("RTC lost confidence in the DateTime!");
 +
    }
 +
  }
 +
 +
  RtcDateTime now = Rtc.GetDateTime();  // 获取当前DS1307时间及日期
 +
  printDateTime(now);  // 串口打印日期时间
 +
  Serial.println();
 +
 +
  delay(10000); // ten seconds
 +
}
 +
 +
#define countof(a) (sizeof(a) / sizeof(a[0]))
 +
 +
// 串口打印日期时间
 +
void printDateTime(const RtcDateTime& dt) {
 +
  char datestring[20];
 +
 +
  snprintf_P(datestring, countof(datestring), PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
 +
            dt.Month(), dt.Day(), dt.Year(),
 +
            dt.Hour(), dt.Minute(), dt.Second() );
 +
  Serial.print(datestring);
 +
}
 +
 
</source>
 
</source>
  

2020年12月23日 (三) 16:02的版本

RTCDS1307模块


产品简介


RTC-DS1307时钟模块是一款基于I2C接口的实时时钟芯片DS1307而设计的模块,可用来计时。

DS1307串行实时时钟(RTC)是低功耗,全二进制编码的十进制(BCD)时钟/日历以及56字节的NV SRAM。地址和数据通过I2C双向总线串行传输。

时钟/日历提供秒,分钟,小时,日期,日期,月份和年份信息。对于少于31天的月份,将自动调整月末日期,包括闰年的更正。

带有AM / PM指示器的时钟以24小时或12小时格式运行。 DS1307具有内置的电源检测电路,可检测到电源故障并自动切换到备用电源。

当零件通过备用电源工作时,计时工作将继续进行。


规格参数


  • 供电电压:DC5V
  • 实时时钟(RTC)可以计算秒,分钟,小时,月份,月份,星期几和年份,并具有有效的闰年补偿,有效期至2100年
  • 56字节,电池支持的通用RAM,具有无限写入
  • 自动断电检测和开关电路
  • 在振荡器运行的情况下,电池备份模式下的功耗小于500nA
  • 工作温度:0℃~70℃
  • 安装孔径:3MM
  • 模块尺寸:45*19.5*6.5MM (长*宽*高)
  • 孔间距:14.5MM
  • 模块重量:5.2g


应用示例


电路连接

RTCDS1307时钟模块 Arduino UNO
VCC +5V
GND GND
SDA SDA/A4
SCL SCL/A5

电路连接示意图

生成缩略图出错:文件可能丢失:


示例代码
/* arduino sketch -- arduino print date and time with ds1307 RTC breakout
   SETUP:
    arduino (UNO R3)     ds1307
      A4/SDA          →    SDA
      A5/SCL          →    SCL
      GND             →    GND
 
   Read More about DS1307: http://www.yfrobot.com.cn/wiki/index.php?title=RTC_1307
 
   BY YFROBOT
*/
 
#include <Wire.h>  // must be incuded here so that Arduino library object file references work
#include <RtcDS1307.h>
 
RtcDS1307<TwoWire> Rtc(Wire);
 
void setup () {
  Serial.begin(115200);
 
  Serial.print("compiled: ");
  Serial.print(__DATE__);      // 编译文档的日期
  Serial.println(__TIME__);    // 编译文档的时间
 
  //--------RTC SETUP ------------
  // if you are using ESP-01 then uncomment the line below to reset the pins to
  // the available pins for SDA, SCL
  // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
  Rtc.Begin();
 
  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); // 存储当前编译的时间及日期并打印
  printDateTime(compiled);
  Serial.println();
 
  if (!Rtc.IsDateTimeValid()) {
    if (Rtc.LastError() != 0) {
      // we have a communications error
      // see https://www.arduino.cc/en/Reference/WireEndTransmission for
      // what the number means
      Serial.print("RTC communications error = ");
      Serial.println(Rtc.LastError());
    } else {
      // Common Causes:
      //    1) first time you ran and the device wasn't running yet
      //    2) the battery on the device is low or even missing
 
      Serial.println("RTC lost confidence in the DateTime!");
      // following line sets the RTC to the date & time this sketch was compiled
      // it will also reset the valid flag internally unless the Rtc device is
      // having an issue
 
      Rtc.SetDateTime(compiled);
    }
  }
 
  if (!Rtc.GetIsRunning()) {
    Serial.println("RTC was not actively running, starting now");
    Rtc.SetIsRunning(true);
  }
 
  RtcDateTime now = Rtc.GetDateTime();  // 获取当前DS1307时间及日期
  if (now < compiled) {
    Serial.println("RTC is older than compile time!  (Updating DateTime)");
    Rtc.SetDateTime(compiled);
  } else if (now > compiled) {
    Serial.println("RTC is newer than compile time. (this is expected)");
    // Rtc.SetDateTime(compiled);
  } else if (now == compiled) {
    Serial.println("RTC is the same as compile time! (not expected but all is fine)");
  }
}
 
void loop () {
  if (!Rtc.IsDateTimeValid()) {
    if (Rtc.LastError() != 0) {
      // we have a communications error
      // see https://www.arduino.cc/en/Reference/WireEndTransmission for
      // what the number means
      Serial.print("RTC communications error = ");
      Serial.println(Rtc.LastError());
    } else {
      // Common Causes:
      //    1) the battery on the device is low or even missing and the power line was disconnected
      Serial.println("RTC lost confidence in the DateTime!");
    }
  }
 
  RtcDateTime now = Rtc.GetDateTime();  // 获取当前DS1307时间及日期
  printDateTime(now);   // 串口打印日期时间
  Serial.println();
 
  delay(10000); // ten seconds
}
 
#define countof(a) (sizeof(a) / sizeof(a[0]))
 
// 串口打印日期时间
void printDateTime(const RtcDateTime& dt) {
  char datestring[20];
 
  snprintf_P(datestring, countof(datestring), PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
             dt.Month(), dt.Day(), dt.Year(),
             dt.Hour(), dt.Minute(), dt.Second() );
  Serial.print(datestring);
}

程序下载地址:thermometer


串口监视结果


LM35串口监视结果

参考资料





返回首页

更多建议和问题欢迎反馈至 YFRobot论坛

购买方式:YFRobot 电子工作室


Arduino 库

RTC DS1307 DS3231库

参考文档





返回首页

更多建议和问题欢迎反馈至 YFRobot论坛

购买方式:YFRobot 电子工作室