Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 11 months ago.
MLX90614A and mbed library for i2c
Hi to all, Melexis Team! I'm sorry if my question is offtopic but i have this problem and i don't know to fix it. i write this easy program for read value from IR MLX90614A, i use stm32Nucleo L152re
title
#define I2C_WRITE 0 #define I2C_READ 1 int main() { pc.printf("\nI2C start"); while(1) { char p1,p2,p3; i2c.start(); int status=i2c.write((0x5A<<1)+I2C_WRITE); int status2=i2c.write(0x07<<1); i2c.start(); int status3=i2c.write((0x5A<<1)+I2C_READ); p1=i2c.read(1); //Tobj low byte p2=i2c.read(1); //Tobj heigh byte p3=i2c.read(0); //PEC i2c.stop(); pc.printf("%d-", status); pc.printf("%d-", status2); pc.printf("%d-", status3); pc.printf("data-"); pc.printf("%d-", (int)p1); pc.printf("%d-", (int)p2); pc.printf("%d-", (int)p3); wait(5); // wait 5 seconds for next scan } }
only status i see the ack, the 1 value the status2 and status3 i see 0 i dont' know where is the error because i read the your application note, can you help me? best regards A.
1 Answer
8 years, 11 months ago.
I dont have your devices, but general recommendation is to use the i2c block read and write operations instead of the byte wise operations. The byte operations are not well implemented on many platforms.
try something like this instead:
int main() { pc.printf("\nI2C start"); while(1) { char data[8]; data[0]=0x07; // assume you are trying to write 0x07 as selected register // assume that 7bit slaveaddress is 0x5A, issue a repeated start int status = i2c.write((0x5A<<1), data, 1, true); // read the 3 data bytes, no need to set the LSB for read or write, that happens automatically int status3=i2c.read((0x5A<<1), data, 3); pc.printf("%d-", status); pc.printf("%d-", status3); pc.printf("data-"); pc.printf("%d-", data[0]); pc.printf("%d-", data[1]); pc.printf("%d-", data[2]); wait(5); // wait 5 seconds for next scan } }
Hi Wim ,thanks for your answer now all work well, i have only i an question, i read 0 the value of status and status3, it's right or wrong best regards
posted by 15 Dec 2015The I2C API documentation may be found here. It states that the return values for blockread and block write methods are:
Returns: 0 on success (ack), non-0 on failure (nack)
So your status and status3 are correct.
posted by 16 Dec 2015