PIC, new type of I2C

Thread Starter

Georg_B

Joined Apr 18, 2022
16
Hello!

MPLABX, PIC18F16Q40, I2C, MLX90614 sensor.

To get the correct result from the sensor, a Repeated Start required in the communication sequence. Whith this new type of I2C module, i don't know how to do that. Attached the read code which is using Stop and Start instead of Repeated Start. Without the correct communication i got only 0xFFFF.

Can somebody tell me, how to modify the code?

See picture, what is the difference between the good (PIC18F14K22) and the not good communication.

Thank you!

C:
    extern volatile uint8_t mlx_msb, mlx_lsb;
    
    static inline void wait4stop(void) {
        while (!I2C1PIRbits.PCIF);
        I2C1PIRbits.PCIF = 0;
        I2C1PIRbits.SCIF = 0;
        I2C1STAT1bits.CLRBF = 1;
    }
    
    void read_mlx(void) {
    
        I2C1CNTH = 0;
    
        I2C1TXB = 0xB4; //write address
        I2C1CNTL = 1;
        I2C1CON0bits.S = 1;
        while (!I2C1STAT1bits.TXBE);
        I2C1TXB = 0x07; //RAM Tobj1
        while (!I2C1STAT1bits.TXBE);
    
        wait4stop();
    
        I2C1TXB = 0xB5; //read address
        I2C1CNTL = 0;
        I2C1CON0bits.RSEN = 1;
        I2C1CON0bits.S = 1;
        while (!I2C1CON0bits.MDR);
        I2C1CNTL = 2;
        I2C1CON0bits.S = 1;
        I2C1CON0bits.RSEN = 0;
    
        while (!I2C1STAT1bits.RXBF);
        mlx_lsb = I2C1RXB; //LSB
        while (!I2C1STAT1bits.RXBF);
        mlx_msb = I2C1RXB; //MSB
    
        wait4stop();
    
    }
MLX.png
 

Thread Starter

Georg_B

Joined Apr 18, 2022
16
Got some help from other source. The correct sequence:

C:
extern volatile uint8_t mlx_msb, mlx_lsb;

static inline void wait4stop(void) {
    while (!I2C1PIRbits.PCIF);
    I2C1PIRbits.PCIF = 0;
    I2C1PIRbits.SCIF = 0;
    I2C1STAT1bits.CLRBF = 1;
}

void read_mlx(void) {

    I2C1CNTH = 0;

    I2C1TXB = 0xB4; //write address
    I2C1CNTL = 1;
    I2C1CON0bits.S = 1;
    while (!I2C1STAT1bits.TXBE);
    I2C1TXB = 0x07; //RAM Tobj1
    while (!I2C1STAT1bits.TXBE);

    I2C1CON0bits.RSEN = 1;
    I2C1CON0bits.S = 1;
    while (!I2C1CON0bits.MDR);

    I2C1TXB = 0xB5; //read address
    I2C1CNTL = 0;
    I2C1CON0bits.S = 1;
    while (!I2C1CON0bits.MDR);

    I2C1CNTL = 2;
    I2C1CON0bits.S = 1;

    while (!I2C1STAT1bits.RXBF);
    mlx_lsb = I2C1RXB; //LSB
    while (!I2C1STAT1bits.RXBF);
    mlx_msb = I2C1RXB; //MSB

    while (!I2C1CON0bits.MDR);
    I2C1CON1bits.P = 1;
    wait4stop();

}
 
Top