#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RtcDS1302.h>
LiquidCrystal_I2C lcd(0x27,16,2);
ThreeWire myWire(2,3,1);
RtcDS1302<ThreeWire> Rtc(myWire);
void setup()
{
//lcd 초기화
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
//RTC 모듈 초기화
Rtc.Begin();
//RTC 모듈 쓰기 금지 확인 후 해제
if(Rtc.GetIsWriteProtected())
Rtc.SetIsWriteProtected(false);
//RTC 모듈이 동작중이 아니라면 구동
if(!Rtc.GetIsRunning())
Rtc.SetIsRunning(true);
//RTCDateTime 클래스 생성(컴파일시 pc 시간으로 설정)
RtcDateTime nowTime = RtcDateTime(__DATE__,__TIME__);
//RTC 모듈 시간을 컴파일시의 PC 시간으로 설정
Rtc.SetDateTime(nowTime);
}
void loop()
{
lcdprintDateTime(Rtc.GetDateTime());
delay(1000);
}
#define countof(a) (sizeof(a)/ sizeof(a[0]))
void lcdprintDateTime(const RtcDateTime &dt)
{
char dateString[20] = {0,};
char timeString[20] = {0,};
snprintf_P(dateString,
countof(dateString),
PSTR("%04u-%02u-%02u"),
dt.Year(),
dt.Month(),
dt.Day());
snprintf_P(timeString,
countof(dateString),
PSTR("%02u:%02u-%02u"),
dt.Hour(),
dt.Minute(),
dt.Second());
lcd.setCursor(0,0);
lcd.print(dateString);
lcd.setCursor(0,1);
lcd.print(timeString);
}