AnsweredAssumed Answered

KL25z I2C two bytes reading

Question asked by asdasd aram on Jan 24, 2014
Latest reply on Jan 26, 2014 by Kerry Zhou

Hello, how would a baremetal code of reading two bytes with I2c from a sensor look like.

 

The following code ist from twr-kl25Demo and reads one byte. How i could read the 2. byte. The Sensor just excect an Acknowledge after the first byte, but how to integrade that in the code below. I Also dont understand why theres    


i2c_give_nack(I2C0_B); after

i2c_get_ack(I2C0_B);

i2c_set_rx_mode(I2C0_B);


and why there is a reading after i2c_stop. Could someone Comment this pls. Thanks a lot

 

 

uint8 result;

 

 

    i2c_start(I2C0_B);

    i2c_write_byte(I2C0_B, MMA8451_I2C_ADDRESS | I2C_WRITE);

  

    i2c_wait(I2C0_B);

    i2c_get_ack(I2C0_B);

 

 

    i2c_write_byte(I2C0_B, addr);

    i2c_wait(I2C0_B);

    i2c_get_ack(I2C0_B);

 

 

    i2c_repeated_start(I2C0_B);

    i2c_write_byte(I2C0_B, MMA8451_I2C_ADDRESS | I2C_READ);

    i2c_wait(I2C0_B);

    i2c_get_ack(I2C0_B);

 

 

    i2c_set_rx_mode(I2C0_B);

 

 

    i2c_give_nack(I2C0_B);

    result = i2c_read_byte(I2C0_B);

    i2c_wait(I2C0_B);

 

 

    i2c_stop(I2C0_B);

    result = i2c_read_byte(I2C0_B);

    pause();

    return result;

Outcomes