cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 I2C reading data

MNoro.2
Associate II

Hey guys, i want to stablish communication between LM75 and STM32F030C8T6 model.

The I2C bus detect device perfectly, but i have a problem in reading temperature from LM75.

Like the datasheet says, if i read the 0x00 register it will return the temperature in 2 bytes. i'm storing the data in 2 uint8 array. after checking this array the temperature isn't right. my question is that the i2c bus put MSB bytes 7-15 in array[0] and 0-7 bits in array[1]?

here is what you will need.

0693W00000QLNR1QAP.pngand this the code :

static const uint8_t TMP102_ADDR = 0x48 << 1;
static const uint8_t REG_TEMP = 0x00;
float temp;
uint8_t buf[2];
 if(HAL_I2C_Master_Transmit(&hi2c1, TMP102_ADDR, buf, 1, 5000) == HAL_OK ){
     HAL_I2C_Master_Receive(&hi2c1, TMP102_ADDR, buf, 2, 5000);
}

So , the first of 8bits of 16 bits temperature(MSB) is in buf[0] and next 8 bits in buf[1]??

thanks for your answer and sorry for my bad English.

4 REPLIES 4
Danish1
Lead II

Ok so what happens when you run your code? That often gives a clue as to what’s wrong.

Does the write return HAL_OK?

If so, what do you get in buf[0] and buf[1] after the read?

One thing I noticed is that you never seem to use REG_TEMP. Maybe the write should say &REG_TEMP not buf. Or you should set buf[0] to 0 before the write.

Hope this helps,

Danish

MNoro.2
Associate II

Yes, it will return HAL_OK and put stuff in buf array. like i said i can communicate between device and stm32. this is buf array after i add a break point .(i changed size of array to 3 to test so don't pay attention to buf[2] in screenshots.

0693W00000QLNV3QAP.png0693W00000QLNV8QAP.png

i used the REG_TEMP and i forgot to copy that line here

Pavel A.
Evangelist III

IIRC the 16-bit value is returned in big endian order. So buf[0] is D15-D7