#include "Wire.h"
#include <LiquidCrystal_I2C.h>

// RTC module at address 0x68
#define DS1307_ADDRESS 0x68

// LCD module at address 0x27
LiquidCrystal_I2C lcd(0x27, 20, 4);

uint8_t clear = 0x00;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  lcd.begin (16,2);
  lcd.backlight();
  // Use a line below to customize a date and time
  // setDateTime(20, 16, 8, 1, 4, 1, 21);
}

void loop()
{
  // Read the values ​​(date and time) of the DS1307 module
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(clear);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 0x07);

  uint8_t seconds = bcdToDec(Wire.read());
  uint8_t minutes = bcdToDec(Wire.read());
  uint8_t hours = bcdToDec(Wire.read() & 0xff);
  uint8_t wday = bcdToDec(Wire.read());
  uint8_t mday = bcdToDec(Wire.read());
  uint8_t month = bcdToDec(Wire.read());
  uint8_t year = bcdToDec(Wire.read());

  // Shows the data on the display
  lcd.setCursor(0,0);
  lcd.print("    ");

  // Adds 0 (clear) if the time is less than 10
  if (hours < 10)
    lcd.print("0");

  lcd.print(hours);
  if (seconds & 1)
    lcd.print(":");
  else
    lcd.print(" ");

  // Adds 0 (clear) if minutes are less than 10
  if (minutes < 10)
     lcd.print("0");

  lcd.print(minutes);

  if (seconds & 1)
    lcd.print(":");
  else
    lcd.print(" ");

  if (seconds < 10)
     lcd.print("0");

  lcd.print(seconds);

  lcd.setCursor(2,1);

  // Show the day of the week
  switch(wday)
    {
      case 0:lcd.print("Sun");
      break;
      case 1:lcd.print("Mon");
      break;
      case 2:lcd.print("Tue");
      break;
      case 3:lcd.print("Wed");
      break;
      case 4:lcd.print("Thu");
      break;
      case 5:lcd.print("Fri");
      break;
      case 6:lcd.print("Sat");
    }

    lcd.setCursor(6,1);

    // Adds 0 (clear) if day of the month is less than 10
    if (mday < 10)
      lcd.print("0");

    lcd.print(mday);
    lcd.print("/");

    // Add 0 (clear) if month is less than 10
    if (month < 10)
      lcd.print("0");

    lcd.print(month);
    lcd.print("/");
    lcd.print(year);
}

// Set the date and time of the DS1307
void setDateTime(uint8_t seconds, uint8_t minutes, uint8_t hours,
  uint8_t wday, uint8_t mday, uint8_t month, uint8_t year)
{
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(clear); // Write clear, so that it can receive data

  // The lines below write in the CI the date and time values ​​
  // that were placed in the variables above
  Wire.write(decToBcd(seconds));
  Wire.write(decToBcd(minutes));
  Wire.write(decToBcd(hours));
  Wire.write(decToBcd(wday));
  Wire.write(decToBcd(mday));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(clear);
  Wire.endTransmission();
}

uint8_t decToBcd(uint8_t value)
{
  // Converts the decimal number to BCD
  return ((value / 10 * 16) + (value % 10));
}

uint8_t bcdToDec(uint8_t value)
{
  // Converts from BCD to decimal
  return ((value / 16 * 10) + (value % 16));
}
GND5VSDASCLSQWRTCDS1307+