1
\$\begingroup\$

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.!

asked Dec 1, 2015 at 9:07
\$\endgroup\$
6
  • \$\begingroup\$ I don't understand : "I am able to send data but not able to receive it. I am receiving the data on UART so also converting it into ASCII, I receive 48 which is 0." Do you receive the data in UART? And you don't in I2C? \$\endgroup\$ Commented Dec 1, 2015 at 9:58
  • \$\begingroup\$ I mean to say I am trying to get data from I2C and then transmitting it serially but on serial I am receiving 0 \$\endgroup\$ Commented Dec 1, 2015 at 10:06
  • 2
    \$\begingroup\$ Did you look on an oscilloscope if there was data on the SDA line? The data sended seems ok to me then it could come from different problem: 1) Timing, your nop may not be enought time there is function given by microchip to wait until the I2C has start or the I2C is IDLE and so on. 2) The hardware, that can come from the resistors on the I2C line. Also I don't get why you don't ack I2C at the end of the read. \$\endgroup\$ Commented Dec 1, 2015 at 10:44
  • \$\begingroup\$ I don't have oscilloscope so can't check with it. Earlier I was using IdleI2C() which wait for the bus to be Idle, but it was still the problem, so I thought of using Nop(). On the hardware, there is 1k resistors on I2C line. \$\endgroup\$ Commented Dec 1, 2015 at 11:04
  • \$\begingroup\$ @user007 Sounds like this question should be closed since you have said you resolved it. If you have another question, create a new question. Don't add unrelated topics in the same question. \$\endgroup\$ Commented Dec 1, 2015 at 15:14

1 Answer 1

1
\$\begingroup\$

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.

answered Dec 1, 2015 at 12:03
\$\endgroup\$
3
  • \$\begingroup\$ I think the mention of the UART working is how the debug information is being returned, it appears the OpenI2C2 etc is the I2C code. \$\endgroup\$ Commented Dec 1, 2015 at 12:07
  • 1
    \$\begingroup\$ ...unless the device tries to stretch the clock. \$\endgroup\$ Commented Dec 1, 2015 at 12:09
  • \$\begingroup\$ 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 am confused.! \$\endgroup\$ Commented Dec 1, 2015 at 13:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.