
RS1307時鐘子產品
RS1307是一個低功耗的外置時鐘子產品,它可以讓你的項目即使在斷電的情況下,也能保證在重新啟動後走時正确。
所需材料
- 1x Arduino UNO
- 1x RS1307時鐘子產品
- 4x 跳線
接線示意圖
RS1307/RS1302與Arduino相連的接線圖
RS1307 | Arduino | |
---|---|---|
SDA | -> | Analog A4 |
SCL | Analog A5 | |
VCC | 5V | |
GND |
加載庫檔案
- 在 這裡 下載下傳RTClib庫,然後手動加載到Arduino IDE中。
示例代碼
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc; // Create a RealTimeClock object
void setup() {
Serial.begin(9600);
Serial.println("YourDuino.com DS1307 Real Time Clock - Set / Run Utility");
#if def AVR
Wire.begin();
#else
Wire1.begin();
#end if
rtc.begin();
// Start the RTC library code
/*----( SET the date and time. Comment OUT these lines after setting )----*/
// Put these "//" in front of the line you do NOT want to use // following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set
// May 21, 2015 at 6pm you would call: (use 24 hour time)
// rtc.adjust(DateTime(2015, 5, 21, 18, 0, 0));
}
//--(end setup )---
void loop(){
DateTime now = rtc.now(); // Read data from the RTC Chip
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(3000);
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//NONE//*********( THE END )***********
輸出結果
---------------------( COPY )--------------------------
2015/4/26 19:29:32
2015/4/26 19:29:35
2015/4/26 19:29:38
-----------------( END COPY )----------------------