This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMP125

Other Parts Discussed in Thread: TMP125

Hi

im a student .. for my project im using TMP125 temperature sensor connect to PIC24FJ256GB..

while reading temp register im not getting the actual temperature... From data sheet its saying it s a 16bit register using only 10bit..

How to remove remaining bits?? Is it need to convert from 2's compliment?

 

  • Hello Nikhil,

    The temperature data in the TMP125 is stored in bits D5-D14 (T0-T9) of the 16-bit temperature register.   

    To extract the temperature data, you must shift the 16-bit register data by 5, and then either directly convert the 10-bit answer in two's complement form, or convert it to a 16-bit two's complement number that your processor will understand as a signed 16-bit integer.

    Please find a piece of example code I put together quickly to show how to do this.  I didn't show the declarations for any of the variables (int, char, etc.) but you should be able to use this process to convert the 16-bit temperature register with the 10-bit data in it to a 16-bit signed temperature value. 

    //********************************************************************************************************
    //The example code below will extract the 10-bit temperature data in the
    //TMP125 16-bit output and translate it to a 16-bit two's complement signed integer
    Step1 = (SPI_Read >> 5);    //Shift the 16-bit SPI output to the right by 5 to remove "D0 - D4"

    Step2 = Step1&0x0200;       //Determine if the 10-bit temperature value is
                                //positive or negative and then back fill the MSBs (D10-D15)
                                // of the 16-bit integer with either 1's or 0's depending
                                // if the 10-bit temperature data MSB was a 1 or a 0

    if(Step2 == 0x0200)         //if the Step2 value equals 0x0200 then we know that the
      {                         //temperature data MSB was a 1 and the temperature
                                //was negative.
      Step3 = Step1|0xFE00;     //If the 10-bit data is negative, back fill D10-D15 with 1's
      }

    else
      {                        //Else the MSB was a 0 and the data was positive
      Step3 = Step1&0x03FF;    //Back fill D10- D15 with 0's
      }

    temperature16 = Step3;   
    temperature16 = temperature16/4;         //temperature in Celcius will be in 16-bit 2's complement form

    //***********************************************************************************************

     

    Best Regards,
    Collin Wells
    Precision Analog

  • Thanks for Reply...

    i understood the procedure and im going to try it...

    one query ..

    Why adding ones or zeros at D10 - D15?     whats the usage by adding???

    ***********************************************************************************

    Like this i did previously ...

    Spi_val = Spi_val & 0x7FFE;   // covering D15 and D4-D0 with Zeros

    Spi_val = Spi_val >>5;        // Shifting D4 - D0

    Temperature = (WORD) Spi_val;      // converting to word

    *************************************************************************************

    and

    While displaying the temperature it need to convert from two compliments form, Is it??

    regards..

    Nikhil

  • Hello Nikhil,

    You need to either add the 0's or the 1's to the MSB's of the shifted result to format it so it is a16-bit signed word. This is only really necessary for the negative numbers as you will see in an example I provided below.

    For example:

    Assume the full 16-bit register output of the TMP125 is: "0111 0011 1000 0000 " , this means that the 10-bit temperature output was "11 1001 1100". This value is a two's complement output and the MSB is "1" so we know it corresponds to a negative value. To convert to a negative two's complement value to decimal you must:
    -Switch all 0's and 1's and then add "1"

    "11 1001 1100" --> "00 0110 0011" + "1" = 00 0110 0100 = 0x064

    0x64 corresponds to a decimal value of 100, and since we know it was negative the final value is -100. To get the final temperature value, you will have to divide by 4.

    -100/4 = -25C.

    This calculated value matches with the value in Table 2 of the datasheet.

    However, since you have a 16-bit processor, when you shift it the data to the right, the MSBs are filled with 0's, so you will be left with a 16-bit register with a value of

    "0000 0011 1001 1100"

    Since it is a 16-bit signed register it will look at the 15th bit to determine if the data is positive or negative and since 0's are added when the shift occurs, the data looks like a signed 16-bit value of 0x039C, which corresponds to a decimal value of 924. After dividing by 4 you will get a temperature reading of 231C which is not correct.

    If you add use the example I provided to add the 1's to the MSBs then the final register will be equal to:

    "1111 1111 1001 1100"

    Now the MSB is a "1" so it will be accepted as a negative number and performing the same calculations as above we get:

    "1111 1111 1001 1100" --> "0000 0000 0110 0011" + "1" = "0000 0000 0110 0100" = 0x0064 = 100

    Since the MSB was a 1, we know it was negative so the value becomes -100 and the final temperature reading is -100/4 = -25C as desired.

    I hope this clears things up.

    Regards,
    Collin Wells
    Precision Analog

  • hi...

    Thank u very much......

    Now its all clear...

    and its working fine...

    I know this s simple one and i made it as complicated... Sorry for it ...And

    again thanking u...

  • Hi Nikhil,

    It was my pleasure.  I'm sure your post and questions will help other TMP12X users as well.

    Best Regards,
    Collin Wells
    Precision Analog