/*
日期:2022年4月28日
作者:RACHEL SYSTEME
該程式允許您透過模組讀取日期和時間
RTC DS1307 然後將其顯示在 16x02 I2C LCD 螢幕上。
注意:程式上傳後,刪除該功能
“config_RTC()”然後再次上傳程式。
原始網址:https://github.com/RS-malik-el/RTC_DS1307/blob/main/LCD_I2C_RTC_DS1307.ino
影片:https://youtu.be/ehHwCxttW6I?si=tMlnMKj6DmivbCod
*/
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
// 建立 RTC_DS1307 對象
RTC_DS1307 rtc;
// 建立日期時間對象
DateTime Now;
// 建立 LiquidCrystal_I2C 對象
LiquidCrystal_I2C lcd(0x27,16,2);
String jour[7]{
"Dim",
"Lun",
"Mar",
"Mer",
"Jeu",
"Ven",
"Sam"
};
// 函數原型
void config_RTC(int decalage = 10);
void date(void);
void time_f24(void);
void time_f12(void);
void setup(){
// 初始化 LCD 通信
lcd.init();
lcd.backlight();
lcd.clear();
// 初始化與 RTC_DS1307 模組的通信
rtc.begin();
/*
下載程式後,刪除該功能即可
重新上傳程序。
注意:如果不刪除,每次啟動程式時,
日期和時間將設定為上傳日期
程式.
在函數參數中插入近似延遲
例如:config_RTC(20); // 最大值 60
*/
//config_RTC(14);
}
void loop(){
date();
//time_f24();
time_f12();
delay(1000);
}
void config_RTC(int decalage){
// 如果延遲超過 60 秒
if (decalage > 60)
decalage = 60;
/*
配置 RTC 模組日期和時間
程式編譯的日期和時間。
手動設定 2022 年 4 月 27 日 11:00:00
rtc.調整(日期時間(2022,4,27,11,0,0));
*/
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Now = rtc.now(); // 更新
int date = (int) Now.day();
int mois = (int) Now.month();
int annee = (int) Now.year();
int H = (int) Now.hour();
int M = (int) Now.minute();
int S = (int) Now.second();
// 總秒數
int S_Total = S + decalage;
// 總分鐘數
int M_Total = M + (S_Total / 60);
// 總小時數
int H_Total = H + (M_Total / 60);
S = S_Total % 60;
M = M_Total % 60;
H = H_Total % 24;
/*
配置 RTC 模組日期和時間
程式編譯的日期和時間
加上傳輸到arduino時產生的延遲
*/
rtc.adjust(DateTime(annee, mois, date, H, M, S));
}
void date(void){
Now = rtc.now();
lcd.clear();
lcd.setCursor(1,0);
lcd.print(jour[Now.dayOfTheWeek()]);
lcd.print(" ");
lcd.print(Now.day());
lcd.print("/");
lcd.print(Now.month());
lcd.print("/");
lcd.print(Now.year());
}
// 24小時制
void time_f24(){
Now = rtc.now();
lcd.setCursor(0,1);
lcd.print("Heure ");
lcd.print(Now.hour());
lcd.print(":");
lcd.print(Now.minute());
lcd.print(":");
lcd.print(Now.second());
}
// 12小時制
void time_f12(){
Now = rtc.now();
lcd.setCursor(0,1);
lcd.print("H ");
lcd.print(Now.twelveHour());
lcd.print(":");
lcd.print(Now.minute());
lcd.print(":");
lcd.print(Now.second());
if (Now.isPM())
lcd.print(" PM");
else
lcd.print(" AM");
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL