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;
Hi asdasd aram
I think you can refer the Typical I2C interrupt routine in KL25P80M48SF0RM.pdf
See below:
Hope that help.
Best Regards
Robin
Thanks for that, but it did not help, i need to understand the code. The routine in the figure is clear, but how to programm, thats the real challenge. Its unclear where to set and unset acknowledge and where to read the two bytes, both after i2c_stop(I2C0_B)?. It would help a lot if someone could explain the code and set it to read two bytes.
Thanks a lot
Best Regards
Hi asdasd aram,
Please see my reply in your another tip:https://community.freescale.com/thread/318549
I wish it useful to you!
If you still have question, please let me know!
Best regards!
Jing