RTC 3231
出自YFRobotwiki
DS3231是低成本、高精度I²C實時時鐘(RTC),具有集成的溫補晶振(TCXO)和晶體。該器件包含電池輸入端,斷開主電源時仍可保持精確計時。集成微機電系統(MEMS)提高了器件的長期精確度,並減少了生產線的元件數量。DS3231提供商用級和工業級溫度範圍,採用16引腳300mil的SO封裝。
RTC保存秒、分、時、星期、日期、月和年信息。少於31天的月份,將自動調整月末的日期,包括閏年修正。時鐘格式可以是24小時或帶/AM/PM指示的12小時格式。提供兩個可設置的日曆鬧鐘和一個1Hz輸出。地址與數據通過I²C雙向匯流排串列傳輸。
精密的、經過溫度補償的電壓基準和比較器電路用來監視VCC狀態,檢測電源故障,提供複位輸出,並在必要時自動切換到備份電源。另外,/RST監測引腳可以作為產生微處理器複位的按鍵輸入,詳細信息請參考完整數據資料中的方框圖。
除計時精度高之外,DS3231還具有一些其它功能,這些功能擴展了系統主機的附加功能和選擇範圍。該器件內部集成了一個非常精確的數字溫度感測器,可通過I2C*介面對其進行訪問(如同時間一樣)。這個溫度感測器的精度為±3°C。片上控制電路可實現自動電源檢測,並管理主電源和備用電源(即低壓電池)之間的電源切換。如果主電源掉電,該器件仍可繼續提供精確的計時和溫度,性能不受影響。當主電源重新加電或電壓值返回到容許範圍內時,片上複位功能可用來重新啟動系統微處理器。
特性
- 0°C 至 +40°C 範圍內精度為±2ppm
- -40°C 至 +85°C 範圍內精度為±3.5ppm
- 為連續計時提供電池備份輸入
- 工作溫度範圍
- 商用級:0°C 至 +70°C (DS3231S#)
- 工業級:-40°C 至 +85°C (DS3231SN#)
- 低功耗
- 實時時鐘產生秒、分、時、星期、日期、月和年計時,並提供有效期到2100年的閏年補償
- 兩個日曆鬧鐘
- 可編程方波輸出
- 高速(400kHz) I2C介面
- 支持工作電壓範圍2.3 - 5.5V(推薦工作電壓:3V-3.3V)
- 數字溫度感測器輸出:精度為±3°C(溫度64秒更新一次)
- 老化修正寄存器
- /RST輸出/按鈕複位去抖輸入
Arduino 庫
應用示例
- 電路連接
| RTC3231時鐘模塊 | Arduino UNO |
| VCC | +5V |
| GND | GND |
| SDA | SDA/A4 |
| SCL | SCL/A5 |
- 示例代碼
/* arduino sketch -- arduino print date and time with ds3231 RTC breakout
SETUP:
arduino (UNO R3) ds3231
A4/SDA → SDA
A5/SCL → SCL
GND → GND
Read More about DS3231: http://www.yfrobot.com/wiki/index.php?title=RTC_3231
BY YFROBOT
*/
#include <Wire.h> // must be incuded here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231 Rtc;
void setup ()
{
Serial.begin(115200);
Serial.print("compiled: ");
Serial.print(__DATE__); // 编译文档的日期
Serial.println(__TIME__); // 编译文档的时间
//--------RTC SETUP ------------
Rtc.Begin();
// 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
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); // 存储当前编译的时间及日期并打印
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 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(); // 获取当前DS3231时间及日期
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)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
// Common Cuases:
// 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();
printDateTime(now);
Serial.println();
//输出温度 -- 温度每64秒更新一次 / 精度±3°C
RtcTemperature temp = Rtc.GetTemperature();
Serial.print("Temp:");
Serial.print(temp.AsFloat());
Serial.println("C");
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 密碼:04gm
參考文檔
更多建議和問題歡迎反饋至 YFRobot論壇
購買方式:YFRobot 電子工作室