1. 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.
    Dismiss Notice

I2C Trouble

Discussion in 'Microcontrollers' started by TucsonDon, Oct 11, 2017.

  1. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    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.
     

    Attached Files:

  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,152
    Likes:
    339
    Location:
    Brisbane Australia
    ONLINE
    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 (text):

    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.
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,303
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    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...
     

Share This Page