#include <Wire.h>
#include <LiquidCrystal.h> // couldn't do without this include since this library is not included by default
//(i am not using the Arduino IDE)
const int DS1307_ADDRESS = 0x68; // I2C address of DS1307 RTC
// LCD module connections (modify these as per your diagram.json)
const int rs = 12;
const int en = 11;
const int d4 = 5;
const int d5 = 4;
const int d6 = 3;
const int d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Wire.begin();
lcd.begin(16, 2); //16 cols, 2 rows
// start i2c communication to initialize DS1307
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Serial.begin(9600);
}
void loop() {
// Request 7 bytes of data from DS1307
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int ss = BinaryToDec(Wire.read() & 0x7F); // remove clock halt bit
int mm = BinaryToDec(Wire.read());
int hh = BinaryToDec(Wire.read() & 0x3F); //24 hour format
Wire.read(); // we need to read a parameter that we dont need to get yy mm dd
int DD = BinaryToDec(Wire.read()) ;
int MM = BinaryToDec(Wire.read()) ;
int YY = BinaryToDec(Wire.read()) ;
Serial.print(DD);
Serial.print("/");
Serial.print(MM);
Serial.print("/");
Serial.print(YY);
Serial.print(" ");
Serial.print(hh);
Serial.print(":");
Serial.print(mm);
Serial.print(":");
Serial.println(ss);
// Display date and time on the LCD
displayDateTime(ss, mm, hh, DD, MM, YY);
delay(1000);
}
int BinaryToDec(int val){ //conversion binary to decimal
return (val / 16 * 10) + (val % 16);
}
void displayDateTime(int ss, int mm, int hh, int DD, int MM, int YY){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(DD);
lcd.print('/');
lcd.print(MM);
lcd.print('/');
lcd.print(YY);
lcd.setCursor(0, 1);
if (hh < 10) lcd.print('0');
lcd.print(hh);
lcd.print(':');
if (mm < 10) lcd.print('0');
lcd.print(mm);
lcd.print(':');
if (ss < 10) lcd.print('0');
lcd.print(ss);
}