Jump to content
Sign in to follow this  
makiyang614

DS18B20 temperature interpretation

Recommended Posts

I have a problem interpretting the temperature reading of a DS18B20 chip. There is a certain situation I am not abble to resolve. This is the example temp table for the chip from the datasheet:

an example for reading of -0.5*C is shown as FFF8. Cutting off the 4 LSBits we get 0x0FFF. In signed data structure that is equal to -1, not 0. How am I abble to resolve the difference between 2 readings: -0.5*C and -1.5*C? If 0xFFF8 = -0.5 then what is the form of -1.5?

The table then shows an example for -10.125*C. Cutting off the 4 LSBits gives 0x0FF5 which really is -10, then there is no reason to think that there is some kind of a shift for reading negative values... Then how to test DS18B20?

Share this post


Link to post
Share on other sites

I am not sure what you are trying to ask, or what processor you are working with, but it appears that you don't have a good understanding of fixed point representations

 

The value from the DS18B20 is a signed (2's complement) fixed point, 16 bit value. Cutting off the last four bits using a right shift that shifts zeros in to the left removes the sign. The shift can be done so as to preserve the sign by using the appropriate arithmetic right shift (arithmetic right shift RRA in assembler on the MSP430, or right shift operator in C on CSS).  This will give an integer in 16 bit 2's complement. The rounding will ALWAYS be the floor (truncate to smaller value; round down) operation.

To get round towards zero, you can use a condition such as (in C):

if (t<0) t+=16;
         
t=t>>4;

or

t=t>>4;
if (t<0) t++;

to do the job.

 

If you want 'conventional' (grammar school) rounding (round down for the fraction <1/2, up for fractions >=1/2), then

t+=8;
t=t>>4;

would be the cannonical way.

If you want symmetrical rounding such that if the fraction of |t| < 1/2 is toward zero, and if hte fraction of |t|>=1/2, round away from zero, then, again, the easiest way is a condition:

if (t<0) 
         {t+=7;}
    else 
         {t+=8;}

Really, you need to know the desired behaviour. There are a lot of reasons why the default action is desirable (always round to negative infinity), in particular because then each count represents a range of one degree, rather than the range for zero being two degrees.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×