【Arduino基础教程】RS1307时钟模块

img_46ac397d2b3f481110c0ff6ed592f46a.jpe
RS1307时钟模块

RS1307是一个低功耗的外置时钟模块,它可以让你的项目即使在断电的情况下,也能保证在重新启动后走时正确。

所需材料
  • 1x Arduino UNO
  • 1x RS1307时钟模块
  • 4x 跳线
接线示意图
RS1307/RS1302与Arduino相连的接线图
RS1307/RS1302与Arduino相连的接线图
RS1307Arduino
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 )----------------------
参考文献

https://arduino-info.wikispaces.com/DS1307_RealTime_Clock_Brick

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值