• 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 Data

TucsonDon

Member
I am using a PIC18F46K42 as a master and a PIC18F46K22 as a slave. The master calls for an ADC result from the slave, the slave will load the MSB into the buffer and clock it out but the will not load the LSB in the buffer and just clocks the MSB out again.
C:
        case PoolTemp:
            data = RawPool >> 8;
            i2cSend(data);
            data = RawPool;
            i2cSend(data);
            break;

static inline void i2cSend (char data)
{
    uint8_t delayCounter = 255;

    while (--delayCounter)
    {
        if (EMPTY == SSP1STATbits.BF)
        {
            SSP1BUFbits.SSPBUF = data;
            SSP1CON1bits.CKP = 1; // release SCL
            break;

        }
        else
        {
           __delay_us(10);
       }
   }
}
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Is the DHEN (Data Hold Enable) bit set to allow clock stretching?

Mike.
Edit, why have you got a delay?
 

TucsonDon

Member
Yes, the AHEN, DHEN, & SEN are all enabled. I have it delayed just to hold in loop tell the byte is clocked out and the buffer is ready to transfer the next byte.
 

Pommie

Well-Known Member
Most Helpful Member
I assume we're looking at the slave code. No delay should be needed as the BF bit will be set until it's been transmitted - I don't use the BF bit but rely on the SSPIF to tell me when things have completed. Your routine can also timeout which will cause the second byte not to be sent. BTW, indenting your code would make it much more readable.

Mike.
 

TucsonDon

Member
Mike,
you are correct it is slave code. I didn't think about it timing out. I will try it tonight waiting for the SSP1IF.

BTW, indenting your code would make it much more readable.

Mike.
I looked at it and thought I should indent but didn't do it.
 

Latest threads

EE World Online Articles

Loading

 
Top