- #include <DS1302.h>
- DS1302 rtc(2,3,4); //對應(yīng)DS1302的RST,DAT,CLK
- void initRTCTime(void)
- {
- rtc.write_protect(false); //關(guān)閉寫保護
- rtc.halt(false); //清除時鐘停止標志
- Time t(2025, 11, 30, 15, 0, 0, 1); //新建時間對象,最后位星期,周日為1
- rtc.time(t); //向DS1302設(shè)置時間數(shù)據(jù)
- }
- void printTime()
- {
- Time tim= rtc.time(); //從DS1302獲取時間數(shù)據(jù)
- char buf[50];
- snprintf(buf,sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d %01d",
- tim.yr, tim.mon,tim.date, tim.hr, tim.min, tim.sec, tim.day);
- Serial.println(buf);
- }
- void setup() {
- Serial.begin(9600);
-
- // 初始化RTC
- initRTCTime();
- }
- void loop() {
- printTime();
- delay(1000); // 每秒更新一次
- }
復(fù)制代碼
|