[pre lang=""]#include "RTClib.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
RTC_DS1307 DS1307; //给DS1307的库设置对象,对象名DS1307
LiquidCrystal_I2C lcd(0x27,16,2);
int h;
int m;
int s;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16,2);
Wire.begin();
lcd.clear();
lcd.init(); // 给LCD的I2C通讯初始化,需要执行两次
delay(20);
lcd.init(); // 给LCD的I2C通讯初始化
delay(20);
lcd.backlight();//点亮LCD背光灯
DS1307.begin(); //初始化时钟
DS1307.set(RTC_SECOND, 00); //初始化时钟的时间
DS1307.set(RTC_MINUTE, 30);
DS1307.set(RTC_HOUR, 15);
DS1307.set(RTC_DAY, 16);
DS1307.set(RTC_MONTH, 2);
DS1307.set(RTC_YEAR, 16);
}
void loop() {
DateTime now = DS1307.now();// 获取当前时间给now实例
h = now.hour(); // 获取当前时间小时
m = now.minute(); // 获取当前时间给分钟
s = now.second(); // 获取当前时间给秒
/*把信息输出到LCD上*/
lcd.clear();
lcd.print("T");
lcd.print("i");
lcd.print("m");
lcd.print("e");
lcd.print(":");
lcd.setCursor(0, 1) ; //将光标显示在第二行
lcd.print(" ");
lcd.print(" ");
lcd.print(" ");
lcd.print(" ");
lcd.print(" ");
lcd.print(h);//在LCD第二行上输出hour
lcd.print(':');
lcd.print(m);//在LCD上输出minute
lcd.print(':');
lcd.print(s);//在LCD上输出second
lcd.print(" ");//在LCD上输出两个空格
delay(1);
}
void printDateTime(DateTime dateTime) {
//传送年份
Serial.print(dateTime.year(), DEC);
Serial.print('/');
//传送月份
Serial.print(dateTime.month(), DEC);
Serial.print('/');
//传送月份中的第几天
Serial.print(dateTime.day(), DEC);
Serial.print(' ');
//传送小时
Serial.print(dateTime.hour(), DEC);
Serial.print(':');
//传送分钟
Serial.print(dateTime.minute(), DEC);
Serial.print(':');
//传送秒
Serial.print(dateTime.second(), DEC);
Serial.println();
}
[/code] |