# I2C Trouble

Status
Not open for further replies.

#### TucsonDon

##### Member
I am trying to setup a rtc using I2C but am having issues in getting it to work. I used the code that was generated by MPLab 4.01 code config. Using on an EasyPICv7 board with a PIC18F46K22 and mikro click rtc.

If I run the code with the debugger I can see on the scope (picture) that it starts but when I pause the program after no further activity the pc is at line 663 of the i2c1.c file (while(i2c1_state != S_MASTER_IDLE); )

I don’t think that the master is getting an acknowledgment but unsure as to why.
I have tried to provide all the info but if I missed something forgive me.

#### Attachments

• 81.5 KB Views: 28
• 36.3 KB Views: 24
• 162.2 KB Views: 39

#### Pommie

##### Well-Known Member
When ever I use I2C in the debug environment it hangs due to it being interrupted mid transmission. So, in my init routine I check for the data line being held low and send clock pulses until it is released.
Here's my init code,
Code:
void I2cInit(){
I2cDataTris=1;              //Make data input
I2cClockTris=1;             //and clock
while(!I2cData){            //if something holding line low
I2cClock=0;             //then pulse clock line
I2cClockTris=0;
NOP();
NOP();
I2cClockTris=1;
}
RC0PPS=0x18;        //RC0=SCK/SCL
RC1PPS=0x19;        //RC1=SDO/SDA
SSP1CLKPPS=0x10;    //SSP1CLK = RC0
SSP1DATPPS=0x11;    //SSP1DAT = RC1
SSPSTAT=8;      //I2C Master clock = Fosc/(4*SSPADD)
SSPCON1=0x28;
SSP1IF=0;
BCL1IF=0;
SSP1CON1bits.SSPEN=1;
}
Mike.

Forum Supporter