AnsweredAssumed Answered

MPL3115A2 temperature conversion

Question asked by Lukasz Przenioslo on Apr 9, 2019
Latest reply on Apr 15, 2019 by Tomas Vaverka

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.

Outcomes