This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Able to read hour & min from DS1307 but not able to read seconds

I am using LM3S9D92 microcontroller. I interfaced DS1307 with microcontroller using I2C protocol. Everthing is working fine in the sense it is updating time properly. But when I am displaying on LCD it is not displaying seconds also sometimes it is displaying garbage values.

 while(1)
    {
          temp1=0;
          temp2=0;
          flag = rtc_set_pointer();
          if(flag==0)
          {
              value=0x20;
              ROM_GPIOPinWrite(GPIO_PORTE_BASE,0x60,value);
          }  
          else
          {
              value=0x40;
              ROM_GPIOPinWrite(GPIO_PORTE_BASE,0x60,value);
          }
          
           ROM_SysCtlDelay(100000);
          flag = rtc_get(&hour,&min,&sec,&day,&month,&year);
          if(flag==0)
           {
             value=0x20;
             ROM_GPIOPinWrite(GPIO_PORTE_BASE,0x60,value);
           }  
           else
           {
             value=0x40;
             ROM_GPIOPinWrite(GPIO_PORTE_BASE,0x60,value);
           }
          
          h = (char) hour;
          m = (char) min;
          s = (char) sec;
          d = (char) day;
          
          mon = (char) month;
          y = (char) year;
          temp1 = (h & 0x0F) | 0x30;
          temp2 = ((h & 0xF0) >> 4) | 0x30;
          temp[0]=temp2;
          temp[1]=temp1;
          
          temp[2]=':';
          
          temp1 = (m & 0x0F) | 0x30;
          temp2 = ((m & 0xF0) >> 4) | 0x30;
          temp[3]=temp2;
          temp[4]=temp1;
          
          temp[5]=':';
        
          temp1 = (s & 0x0F) | 0x30;
          temp2 = ((s & 0xF0) >> 4) | 0x30;
          temp[6]=temp2;
          temp[7]=temp1;
          if(temp[0]!=';' && temp[1]!='<')
          {
               if(h<0x24 && m<0x60)
               {
                 LCD_goto(2,2);
               LCD_send_command(0x14);
               LCD_print(temp);  
               }
          }
          ROM_SysCtlDelay(64000000);
           for(i=0;i<10;i++)
           {
              temp[i]=0;
           }

}

unsigned char rtc_set_pointer()
{
    unsigned char flag;
    ROM_I2CMasterInitExpClk(I2C1_MASTER_BASE,ROM_SysCtlClockGet(),false);
    ROM_I2CMasterSlaveAddrSet(GPIO_PORTE_BASE,0x68,false);
     ROM_I2CMasterDataPut(I2C1_MASTER_BASE,0x00);
    
    
    ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    
       //ROM_I2CMasterDataPut(I2C1_MASTER_BASE,0x00);
        ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    return 1;
}

unsigned char rtc_get(unsigned long *hour,unsigned long *min, unsigned long *sec,unsigned long *day,unsigned long *month, unsigned long *year)
{
    unsigned char flag;
    //unsigned long temp;
    //ROM_I2CMasterInitExpClk(I2C1_MASTER_BASE,ROM_SystCtlGet(),false);
    ROM_I2CMasterSlaveAddrSet(I2C1_MASTER_BASE,0x68,true);
    ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START);
    *sec = ROM_I2CMasterDataGet(I2C1_MASTER_BASE);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    
    ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    *min = ROM_I2CMasterDataGet(I2C1_MASTER_BASE);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    
    
    ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    *hour = ROM_I2CMasterDataGet(I2C1_MASTER_BASE);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    
    
    ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    temp = ROM_I2CMasterDataGet(I2C1_MASTER_BASE);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    
    
    ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    *day = ROM_I2CMasterDataGet(I2C1_MASTER_BASE);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    
    
    ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    *month = ROM_I2CMasterDataGet(I2C1_MASTER_BASE);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    
    ROM_I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    *year = ROM_I2CMasterDataGet(I2C1_MASTER_BASE);
    flag = error_check();
    if(flag == 0)
    {
        return 0;
    }
    
    return 1;
}