MPL3115A2 temperature conversion

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

MPL3115A2 temperature conversion

1,902 Views
lukaszprzeniosl
Contributor II

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.

Labels (1)
Tags (1)
2 Replies

1,786 Views
TomasVaverka
NXP TechSupport
NXP TechSupport

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.

1,786 Views
lukaszprzeniosl
Contributor II

Hello Tomas, thank you for answer.

Code-wise, your method is the same as my last one, which I found working. But yours is optimized better. Thank you for help, now everything is clear.

0 Kudos
%3CLINGO-SUB%20id%3D%22lingo-sub-853523%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EMPL3115A2%20temperature%20conversion%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-853523%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%20there%2C%3C%2FP%3E%3CP%3EI%20am%20having%20a%20problem%20with%20understanding%20how%20to%20convert%20the%20the%20MPL3115A2%20temperature%20readings%20exactly%20(OUT_T_MSB%2C%20OUT_T_LSB).%20Please%20consider%20this%20function%3A%3C%2FP%3E%3CBLOCKQUOTE%20class%3D%22jive_macro_quote%20jive-quote%20jive_text_macro%22%3E%3CP%3Einline%20static%20float%20rawTemperature2Float(const%20uint8_t*%20const%20rt)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3Breturn%20((float)((int8_t)rt%5B0%5D))%20%2B%20(((rt%5B1%5D%20%26gt%3B%26gt%3B%204)%20%26amp%3B%200x0F)%20*%200.0625f)%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3Ert%5B0%5D%20%3D%20OUT_T_MSB%20and%20rt%5B1%5D%20%3D%20OUT_T_LSB.%3C%2FP%3E%3CP%3EThe%20question%20is%3A%20Does%20the%20sign%20bit%20in%20MSB%20also%20determinate%20how%20should%20the%20fractional%20part%20be%20treated%2C%20or%20is%20it%20always%20positive%3F%20In%20the%20presented%20function%20I%20am%20always%20adding%20the%20fractional%20part.%20But%20maybe%20if%20the%20sign%20is%20negative%2C%20I%20should%20substract%20it%20(or%20multiply%20by%20-1)%3F%20Should%20the%20function%20look%20like%20this%3F%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CBLOCKQUOTE%20class%3D%22jive_macro_quote%20jive-quote%20jive_text_macro%22%3E%3CP%3Einline%20static%20float%20rawTemperature2Float(const%20uint8_t*%20const%20rt)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bfloat%20frac%20%3D%20((rt%5B1%5D%20%26gt%3B%26gt%3B%204)%20%26amp%3B%200x0F)%20*%200.0625f%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bif%20(rt%5B0%5D%20%26amp%3B%200x80)%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bfrac%20*%3D%20-1%3B%20%2F%2F%20variant%20%231%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2Ffrac%20%3D%20(1.0f%20-%20frac)%20*%20-1%3B%20%2F%2F%20variant%20%232%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3Breturn%20((float)((int8_t)rt%5B0%5D))%20%2B%20frac%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3EIf%20so%2C%20which%20variant%20in%20the%20if%3F%3C%2FP%3E%3CP%3EThis%20approach%20gives%20similar%20results%20(all%20bits%20threaded%20for%20twos%20complement%20conversion)%3A%3C%2FP%3E%3CBLOCKQUOTE%20class%3D%22jive_macro_quote%20jive-quote%20jive_text_macro%22%3E%3CP%3Einline%20static%20float%20rawTemperature2Float(const%20uint8_t*%20const%20rt)%3CBR%20%2F%3E%7B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bint32_t%20temp%20%3D%20(((int32_t)rt%5B0%5D)%20%26lt%3B%26lt%3B%204)%20%7C%20(rt%5B1%5D%20%26gt%3B%26gt%3B%204)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bif%20(temp%20%26amp%3B%200x800)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20temp%20%3D%20(0x1000%20-%20temp)%20*%20-1%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%20%26nbsp%3Breturn%20((float)(temp))%20%2F%2016.0f%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3EI%20would%20appreciate%20all%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-853523%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3EPressure%20Sensors%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-853525%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20MPL3115A2%20temperature%20conversion%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-853525%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%20Tomas%2C%20thank%20you%20for%20answer.%20%3C%2FP%3E%3CP%3ECode-wise%2C%20your%20method%20is%20the%20same%20as%20my%20last%20one%2C%20which%20I%20found%20working.%20But%20yours%20is%20optimized%20better.%20Thank%20you%20for%20help%2C%20now%20everything%20is%20clear.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-853524%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20MPL3115A2%20temperature%20conversion%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-853524%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%20Lukasz%2C%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EI%20do%20apologize%20for%20my%20delayed%20response%2C%20but%20I%20was%20on%20a%20business%20trip%20last%20week%20with%20a%20limited%20time%20to%20react%20on%20community%20questions.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EIn%20my%20code%20I%20use%20the%20following%20formula%3A%3C%2FP%3E%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%3CSPAN%20style%3D%22color%3A%20black%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20font-size%3A%2010pt%3B%22%3ETemperature%20%3D%20(%3C%2FSPAN%3E%3CSPAN%20style%3D%22color%3A%20%237f0055%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20font-size%3A%2010pt%3B%22%3E%3CSTRONG%20style%3D%22border%3A%200px%3B%20font-weight%3A%20bold%3B%20font-size%3A%2013.3333px%3B%22%3Efloat%3C%2FSTRONG%3E%3C%2FSPAN%3E%3CSPAN%20style%3D%22color%3A%20black%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20font-size%3A%2010pt%3B%22%3E)%20((%3C%2FSPAN%3E%3CSPAN%20style%3D%22color%3A%20%237f0055%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20font-size%3A%2010pt%3B%22%3E%3CSTRONG%20style%3D%22border%3A%200px%3B%20font-weight%3A%20bold%3B%20font-size%3A%2013.3333px%3B%22%3Eshort%3C%2FSTRONG%3E%3C%2FSPAN%3E%3CSPAN%20style%3D%22color%3A%20black%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20font-size%3A%2010pt%3B%22%3E)((RawData%5B3%5D%20%26lt%3B%26lt%3B%20%3CLI-EMOJI%20id%3D%22lia_smiling-face-with-sunglasses%22%20title%3D%22%3Asmiling_face_with_sunglasses%3A%22%3E%3C%2FLI-EMOJI%3E%20%7C%20(RawData%5B4%5D%20%26amp%3B%200xF0))%20%26gt%3B%26gt%3B%204)%20*%200.0625%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%3CSPAN%20style%3D%22color%3A%20black%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20font-size%3A%2010pt%3B%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EYou%20need%20to%20get%20-0.0625%20for%20RawData%5B3%5D%20%3D%20OUT_T_MSB%20%3D%20FF%20and%20RawData%5B4%5D%20%3D%20OUT_T_LSB%20%3D%20F0.%3C%2FP%3E%3CP%3EI%20will%20retest%20it%20again%20tomorrow%20when%20I%20am%20back%20in%20the%20office%2C%20but%20if%20I%20remember%20well%2C%20I%20really%20got%20-0.0625%20for%200xFFF0.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EBest%20regards%2C%3C%2FP%3E%3CP%3ETomas%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%3CEM%20style%3D%22border%3A%201pt%20none%20windowtext%3B%20font-weight%3A%20inherit%3B%20font-size%3A%209pt%3B%20padding%3A%200in%3B%22%3EPS%3A%26nbsp%3BIf%26nbsp%3Bthis%20answer%20helps%20to%20solve%20your%20question%2C%20please%20mark%20it%20as%20%22Correct%22%20or%20%E2%80%9CHelpful%E2%80%9D.%20Thank%20you.%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E