• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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

Pommie

Well-Known Member
Most Helpful 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
    SSPADD=80;      //8000/100;
    SSPSTAT=8;      //I2C Master clock = Fosc/(4*SSPADD)
    SSPCON1=0x28;
    SSP1IF=0;
    BCL1IF=0;
    SSP1CON1bits.SSPEN=1;
}
Mike.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
In my experience, Interrupts on I2C and SPI are not really necessary.. Especially at high speeds..

I read / write the clock very quickly and it doesn't affect program flow much at all... Clocks only need to be read once / twice a second as they only store seconds...
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top