Hello there,

I am having a problem with understanding how to convert the the MPL3115A2 temperature readings exactly (OUT_T_MSB, OUT_T_LSB). Please consider this function:

inline static float rawTemperature2Float(const uint8_t* const rt)

{

return ((float)((int8_t)rt[0])) + (((rt[1] >> 4) & 0x0F) * 0.0625f);

}

rt[0] = OUT_T_MSB and rt[1] = OUT_T_LSB.

The question is: Does the sign bit in MSB also determinate how should the fractional part be treated, or is it always positive? In the presented function I am always adding the fractional part. But maybe if the sign is negative, I should substract it (or multiply by -1)? Should the function look like this?

inline static float rawTemperature2Float(const uint8_t* const rt)

{

float frac = ((rt[1] >> 4) & 0x0F) * 0.0625f;

if (rt[0] & 0x80)

{

frac *= -1; // variant #1//frac = (1.0f - frac) * -1; // variant #2

}

return ((float)((int8_t)rt[0])) + frac;

}

If so, which variant in the if?

This approach gives similar results (all bits threaded for twos complement conversion):

inline static float rawTemperature2Float(const uint8_t* const rt)

{

int32_t temp = (((int32_t)rt[0]) << 4) | (rt[1] >> 4);

if (temp & 0x800)

temp = (0x1000 - temp) * -1;

return ((float)(temp)) / 16.0f;

}

I would appreciate all help.

Hello Lukasz,

I do apologize for my delayed response, but I was on a business trip last week with a limited time to react on community questions.

In my code I use the following formula:

Temperature = (

float) ((short)((RawData[3] << 8) | (RawData[4] & 0xF0)) >> 4) * 0.0625;You need to get -0.0625 for RawData[3] = OUT_T_MSB = FF and RawData[4] = OUT_T_LSB = F0.

I will retest it again tomorrow when I am back in the office, but if I remember well, I really got -0.0625 for 0xFFF0.

Best regards,

Tomas

PS: If this answer helps to solve your question, please mark it as "Correct" or “Helpful”. Thank you.