Unable to read data from I2C

ci2cpic

I am interfacing DS1307 with PIC32MX795F512L using peripheral library. I am able to send data but not able to receive it.

Code:

#include <plib.h>

#define BAUDRATE    115200
#define Fsck        100000
#define BRG         ((FPB/2/Fsck)-2)

#define FCY     66000000L
#define FPB     (FCY/2)

#pragma config POSCMOD=HS, FNOSC=PRIPLL 
#pragma config FPLLIDIV=DIV_3, FPLLMUL=MUL_18, FPLLODIV=DIV_1 
#pragma config FPBDIV=DIV_2, FWDTEN=OFF  
#pragma config FSOSCEN = OFF 
#pragma config ICESEL = ICS_PGx2

int main(int argc, char** argv)
{

 OpenUART1( UART_EN | UART_NO_PAR_8BIT | UART_1STOPBIT  , UART_RX_ENABLE | UART_TX_ENABLE, (FPB/16/BAUDRATE)-1 );
putsUART1("START\n");
int res;

OpenI2C2(I2C_EN, 163); // I2C channel Configuration

StartI2C2();

IdleI2C2();
MasterWriteI2C2(0xD0); 
Nop();
MasterWriteI2C2(0x01); 
Nop();
MasterWriteI2C2(0b00010011); 
Nop();
StopI2C2();

IdleI2C2(); 
StartI2C2();
IdleI2C2(); 
MasterWriteI2C2(0xD0);

Nop();
MasterWriteI2C2(0x01);
Nop();
StopI2C2();

RestartI2C2();
IdleI2C2();
MasterWriteI2C2(0xD1);
Nop();
MasterWriteI2C2(0x01);
IdleI2C2();
res = MasterReadI2C2();
Nop();
NotAckI2C2();
Nop();
StopI2C2();

 //converting to ASCII
unsigned char x,y,p1,p2;
x = res & 0x0F;
p1 = x | 0x30;
y = res & 0xF0;
y = y >> 4;
p2 = y | 0x30;

char value1[5] ;
char value2[5] ;
sprintf(value1,"%d",p1);
sprintf(value2,"%d",p2);
putsUART1(value1);
putsUART1(value2);
putsUART1("\n");


while(1)
{

}

return (EXIT_SUCCESS);
}

UART is working fine and there is no collision because the function MasterWriteI2C2() returns -1 if there is collision and returns 0 if there is no collision and data transmission is successful & I have received 0, that means data is being transmitted but I don't know why its not able to read back data. Please help.

EDIT:

I resolved the problem which I was facing and now I2C is working as I can see the received value in I2CRCV register is same which I am sending. But I need to send the value to UART for which I think I need to convert them to ASCII but after converting the values will change. Lets say I have set 13 value in seconds register then its value will change during conversion, then how can I set RTC value. I need to make a logic so that I can set the RTC value from UART.
Please help.!

Best Answer

I didn't wade thru your code, but one obvious problem pops out immediately.

You don't use a UART to do IIC. Note the "A" in UART stands for "asynchronous", which IIC is definitely not.

To do IIC, use the IIC peripheral or do it all in firmware. That's actually quite easy when you're the master since you own the clock.