Search code examples
mathheximpsquirrel

Why do you divide the raw data by 16?


http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf

Read page 3, Operation – Measuring Temperature. The following code works to get the temp. I understand all of it except why they divide the number by 16.

local raw = (data[1] << 8) | data[0];
    local SignBit = raw & 0x8000;  // test most significant bit
    if (SignBit) {raw = (raw ^ 0xffff) + 1;} // negative, 2's compliment

    local celsius = raw / 16.0;
    if (SignBit) {celsius *= -1;}

I’ve got another situation http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Pressure/MPL3115A2.pdf Page 23, section 7.1.3, temperature data. It’s only twelve bits, so the above code works for it also (just change the left shift to 4 instead of 8), but again, the /16 is required for the final result. I don’t get where that is coming from.


Solution

  • The raw temperature data is in units of sixteenths of a degree, so the value must be divided by 16 in order to convert it to degrees.