KL25z I2C two bytes reading

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

KL25z I2C two bytes reading

1,265 Views
asdasdaram
Contributor III

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;

0 Kudos
3 Replies

895 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi asdasd aram

I think you can refer the Typical I2C interrupt routine in KL25P80M48SF0RM.pdf

See below:

Typical I2C interrupt routine.jpg

Hope that help.

Best Regards

Robin

0 Kudos

895 Views
asdasdaram
Contributor III

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

0 Kudos

895 Views
kerryzhou
NXP TechSupport
NXP TechSupport

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

0 Kudos