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 DS3231庫 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 電子工作室