volatile unsigned char *TWBR1=0xB8;
volatile unsigned char *TWCR1=0xBC;
volatile unsigned char *TWSR1=0xB9;
volatile unsigned char *TWDR1=0xBB;
volatile unsigned char *TWAR1=0xBA; 
void port_initialization()
{
  volatile char *portk_dir;
  volatile char *portf_dir;

  portf_dir=0x30;
  portk_dir=0x107;

  *portf_dir=0xFF;
  *portk_dir=0xFF;
}
void outdata(char output)
{
  volatile char *portf_data;
  portf_data=0x31;
  *portf_data=output;
}
volatile E_and_RS_Pin(char data)
{
  volatile char *portk_data;
  portk_data=0x108;
  *portk_data=data & 0x03;
}
void delay1()
{
  volatile long i;
  for(i=0;i<2000;i++);
} 
void Enable_pulse()
{
  E_and_RS_Pin(0x01);
  delay1();
  E_and_RS_Pin(0x00);
  delay1();
}
void LCD_initializations()
{
  outdata(0x38); //Function Set mode
  Enable_pulse();
  outdata(0x0C);  //Display on/off mode
  Enable_pulse();
}
void writing_data(char data_to_write)
{
  outdata(data_to_write);
  E_and_RS_Pin(0x02); //making Rs pin high during writing data
  delay1();
  E_and_RS_Pin(0x03); //both enable and rs should be high
  delay1();
  E_and_RS_Pin(0x02); //making enable low and rs high as it
  delay1();
}
void clear_display()
{
   outdata(0x01);   //clear display
  Enable_pulse(); 
}
void Return_home()
{
  outdata(0x02);
  Enable_pulse();
}
void set_DDRAM_address(char data)
{
  outdata(data);
  Enable_pulse();
}
void string_data(char *ptr)
{
   while(*ptr != 0)
   {
      writing_data(*ptr);
      *ptr++;
   }
}
void I2C_initialization()
{  
  *TWSR1=0x00;
  *TWBR1=0x48;
}
void I2C_start()
{
  *TWCR1=0xA4;
   while((*TWCR1 &0x80)==0);
  // Serial.println("the value of status register after transmission");
  // Serial.println((*TWSR1 & 0xF8),HEX);
}
void I2C_write(unsigned char data1)
{
  *TWDR1=data1;
  *TWCR1=0x84;
   while((*TWCR1 &0x80)==0);
  // Serial.println("the value of status register after write");
  // Serial.println((*TWSR1 & 0xF8),HEX);
}
unsigned char I2C_read(char ack_EA)
{
  if(ack_EA==1)
  {*TWCR1=0xC4;}
  else{
    *TWCR1=0x84;
  }
   while((*TWCR1 &0x80)==0);
   return *TWDR1;
}
void I2C_stop()
{
  *TWCR1=0x94;
   while(!(*TWCR1 &0x80));
}
unsigned char bcd_to_decimal(unsigned char data)
{
  unsigned char data1,data2,data3;
  data1=(data & 0xF0)>>4;
  //Serial.println("the value of bcd to decimal");
 // Serial.println(data1,HEX);
  data2=(data & 0x0F);
 //  Serial.println("the value of bcd to decimal");
 // Serial.println(data2,HEX);
  return data3=(data1*10)+data2;
}
void Time_string(char a[],unsigned char h,unsigned char m,unsigned char s)
{
        int i,x=h;
        for(i=0;i<9;i=i+3)
        {
           if(i==3)
          {x=m;}
          if(i==6)
          {x=s;}
          a[i]=(x/10)+48;
          a[i+1]=(x%10)+48;
          if(i!=6)
          {
              a[i+2]=':';
          }
        }
        a[9]='\0';
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  unsigned char sec,minute,hour,date,day,month,year;
  char time[10];
    port_initialization();
    LCD_initializations();
    clear_display();
    set_DDRAM_address(0x84);
    string_data("RTC CLOCK");
    I2C_initialization();
    while(1)
    {
    I2C_start();
    I2C_write(0xD0);
    I2C_write(0x00);
    I2C_start();
    I2C_write(0xD1);
    sec=I2C_read(1);
    minute=I2C_read(1);
    hour=I2C_read(1);
    day=I2C_read(1);
    date=I2C_read(1);
    month=I2C_read(1);
    year=I2C_read(0);
 //   Serial.print("the time is : ");
 //   Serial.print(hour,HEX);
  //  Serial.print("/");
 //   Serial.print(minute,HEX);
 //   Serial.print("/");
  //  Serial.print(sec,HEX);
 //   Serial.println(" ");
 //   Serial.print("todays date is : ");
  //  Serial.print(date,HEX);
 //   Serial.print("/");
  //  Serial.print(month,HEX);
  //  Serial.print("/");
  //  Serial.print(year,HEX);
    sec=bcd_to_decimal(sec);
  //  Serial.println("the value of sec in decimal");
  //  Serial.println(sec);
    minute=bcd_to_decimal(minute);
  //  Serial.println("the value of minute in decimal");
  //  Serial.println(minute);
    hour=bcd_to_decimal(hour);
   // Serial.println("the value of hour in decimal");
    //Serial.println(hour);
    if(hour>12)
    {
      hour=hour-12;
    }
    Time_string(time,hour,minute,sec);
    Return_home();
    set_DDRAM_address(0xC4);
    string_data(time);
    I2C_stop();
    }
   
}

void loop() {
  // put your main code here, to run repeatedly:

}
GND5VSDASCLSQWRTCDS1307+