How to Request time from RTC 1307

I am searching for a site where i can learn the Wire library and being able to write and read from chips with your own code kinda turns me on. any suggestions are welcome. Thanks for the data sheet tip.

More for you!! Please, follow/read the comments placed against the instructions to understand the meaning and reason for writing this instruction. the program has totally excluded the Library Functions; but, it has utilized the commands of the I2C protocol.

We will be frequently consulting the following two diagrams -- Register Map of RTC1307 and the Clcok System Block diagram,

Figure-1: Register map of RTC1307 RTC


Figure-2: DS1307 RTC Based Clock System

A: Let us first operate the RTC in 24-Hrs Clock Mode with an initial arbitrary Time :

#include <Wire.h>     //needed because DS3231 uses I2C Bus
#include<LiquidCrystal_I2C.h>  //neded by LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); //lcd address; 16 characters 2 line

#define deviceAddress 0x68    //I2C Bus address of RTC
#define secRegAdr 0x00        //this is not I2C bus address; this is internal address of Sec Register

void setup()
{
  //-------initialize I2C LCD using Library functions------------
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);

  //-----initialize I2C BUs at default speed---------------------
  Wire.begin();
  
  //----intialize RTC1307 with 24-hrs Clock. Say, Intial Time: 23 : 58 : 17(hrs:min:sec)
  Wire.beginTransmission(deviceAddress);  //queuing address, data direction bit in buffer
  Wire.write(secRegAdr);                     //pointing Second Register and queuing it
  Wire.write(0x17);  //17-sec (in BCD format) to be written in SecRegister; queing
  Wire.write(0x58); //58-min(in BCD format) to be automatically wrirtten into MinReg; queuing
  Wire.write(0x23); //23-hrs (in BCDformat) to be aut written into HrsReg; queuing
  Wire.endTransmission(); //all queued data written into Time Regs of RTC on handshaking

  //------Clock has already statrted------
  //----- now read time and show it on the bottom line of LCD
}

void loop()
{
  showTimeOnLCD();
}  

void showTimeOnLCD()
{
  //-----Read back the Time from RTC and show on Bottom Line of LCD--------------------
  Wire.beginTransmission(deviceAddress); //START, Roll Cal
  Wire.write(0x00); //set SEC Register; others registers will be auto accessed
  Wire.endTransmission(); //Execute the above queued data, ACK, STOP

  //-------Requesting Time from the RTC; Time is saved in a 32-byte wide FIFO buffer---------
  Wire.requestFrom(deviceAddress, 3);   //SEC, MIN, and HRS to read from RTC as BCD
  byte bcdSeconds = Wire.read();        //Current Second in the variable bcdSeconds in BCD format
  byte bcdMinutes = Wire.read();
  byte bcdHours = Wire.read();

  //---------------------------------------------------------------------------------------------
  lcd.setCursor(0, 1);      //cursor position at the beginning of bottom line
  lcd.print("Time = ");
  //show HRS--
  lcd.write((bcdHours>>4) + 0x30);  //shifting and making ASCII codes
  lcd.write((bcdHours & 0x0F) + 0x30);
  lcd.write(':');
  
  //show MIN--
  lcd.write((bcdMinutes>>4) + 0x30);
  lcd.write((bcdMinutes & 0x0F) + 0x30);
  lcd.print(':');
 
 //shiw SEC
 lcd.write((bcdSeconds>>4) + 0x30);
 lcd.write((bcdSeconds & 0x0F) + 0x30);
}

B: Now, you have got a stating point. You can slowly add the following features Like:
(1) Even you reset the UNO, the intial set time will never be changed.
(2) You can set the Data (yrs/mo/day) and show it on the Top Line of LCD.
(3) You can set 12-hrs time by consulting Fig-1.
(4) You can put the LCD/MCU on sleep. When you will press K1, the LCD will show the current date and Time.

(5) You can also implement your decrement function? How? the RTC is always clocking up and not down?

Any query for questions/clarifications are welcome!