Few queries on how to set real time clock value in DS1307

rtc

I need to integrate DS1307 RTC in my project. I have somehow managed to write I2C code and interfaced it with PIC32. I need to make a logic so that it shows real time values on UART. Now to display it on UART, the values need to be converted into ASCII. So lets say if I set 13 to its seconds register then I am getting this value in I2C2RCV register but on the terminal it is displaying some random ASCII character.

After converting this value to ASCII, I am receiving 51 49 on terminal. I am using below logic for conversion:

//i2cbyte : Received values from I2C

char  i2cbyte;
unsigned char x,y,p1,p2;
char value1[10] ;
char value2[10] ;
 x = i2cbyte & 0x0F;
 p1 = x | 0x30;
 y = i2cbyte & 0xF0;
 y = y >> 4;
 p2 = y | 0x30;
 sprintf(value1,"%d",p1);
 sprintf(value2,"%d",p2); 
 putsUART1(value1);
 putsUART1(value2);

So I set 13 and I received 51 49. So what value should I set in my code to get real time value on UART. Is it possible to get values on UART because inside the code I am getting the real time value but to display it outside the code, the values need to be converted which becomes something else. How can I resolve this?

After setting the values in the register of RTC, do I need to use some logic to update it. Datasheet tell that registers are automatically updated. But what happens during programming is I am setting values to registers and then reading it but the values are not updating. I am always receiving what I am writing.
How to set values

Please help.!

CODE:

int main(void)
{

OpenUART1( UART_EN | UART_NO_PAR_8BIT | UART_1STOPBIT  , UART_RX_ENABLE | UART_TX_ENABLE, (FPB/16/BAUDRATE)-1 );


I2C2BRG = 0xA3;     //I2C Baudrate
I2CEnable(EEPROM_I2C_BUS, TRUE);    //I2C module On

StartTransfer(FALSE);      //I2C Start
TransmitOneByte(0xD0);  //slave address
TransmitOneByte(0x00); //register pointer pointing to first register (seconds)
TransmitOneByte(0x13); //seconds register value
StopTransfer(); //I2C Stop

IdleI2C2(); //I2C wait

while(1)
{
    RTC_read();

    putsUART1(value2);
    putsUART1(value1);
    putsUART1("\n");
    DelayMs(1000);
}

}

//RTC_read function:

void RTC_read()
{
StartTransfer(FALSE); //I2C Start
TransmitOneByte(0xD0); //I2C slave address
TransmitOneByte(0x00); //Register address
StopTransfer(); //I2C Stop

IdleI2C2(); //i2C Stop

StartTransfer(TRUE);    //I2C Restart
TransmitOneByte(0xD1);  //Slave address to read

//Reading from I2C slave
if(I2CReceiverEnable(EEPROM_I2C_BUS, TRUE) == I2C_RECEIVE_OVERFLOW)
{
  putsUART1("Error: I2C Receive Overflow\n");
}
else
{
   while(!I2CReceivedDataIsAvailable(EEPROM_I2C_BUS));
    sec = I2CGetByte(EEPROM_I2C_BUS); //storing seconds into sec variable
}
StopTransfer(); //I2C Stop

    //converting sec to ASCII
    x1 = sec & 0x0F;
    p1 = x1 | 0x30;
    y1 = sec & 0xF0;
    y1 = y1 >> 4;
    p2 = y1 | 0x30;

    sprintf(value1,"%c",p1);
    sprintf(value2,"%c",p2);
}

Best Answer

The 51 49makes perfect sense. They are the ASCII codes for 3 and 1, resp. Why the reverse order? You started by selecting the low order nibble, instead of the high order nibble.

How to send the character instead of its ASCII code?
Use "%c" as format string instead of "%d" (which formats a decimal number).