1. 程式人生 > >【Arduino基礎教程】RS1307時鐘模組

【Arduino基礎教程】RS1307時鐘模組

1670644-df05465e71921af7.jpg RS1307時鐘模組

RS1307是一個低功耗的外接時鐘模組,它可以讓你的專案即使在斷電的情況下,也能保證在重新啟動後走時正確。

所需材料

  • 1x Arduino UNO
  • 1x RS1307時鐘模組
  • 4x 跳線

接線示意圖

RS1307/RS1302與Arduino相連的接線圖
RS1307 Arduino
SDA -> Analog A4
SCL -> Analog A5
VCC -> 5V
GND -> 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 )----------------------

參考文獻