RTC 1307
来自YFRobotwiki
产品简介
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 库 : RTC DS1307库简介 、 RTCDS1307库下载
/* 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); }
程序下载地址:ClockWithSerialPrint
提示:程序中使用到的__DATE__,__TIME__;如果不理解请看介绍:宏: DATE , TIME , FILE , LINE
- 串口监视结果
生成缩略图出错:文件可能丢失:
参考资料
更多建议和问题欢迎反馈至 YFRobot论坛
购买方式:YFRobot 电子工作室