What equation is used to translate binary/hex readings to temperature readings?
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.
/*
* ======== TMP75B_toIntCelsius ========
* Convert raw temperature register value to degrees Celsius rounded to the
* nearest integer
*/
int32_t TMP75B_toIntCelsius(int32_t x)
{
/* Shift raw register value to form a proper Q4 value */
x >>= 4;
/* Optional: Add bias to round before the final truncation */
x += (x >= 0) ? (1 << 3) : -(1 << 3);
/* Convert Q4 value to whole number */
x /= 1 << 4; /* use division so small negative values round to 0 */
return x;
}
/*
* ======== TMP75B_toMilliCelsius ========
* Convert raw temperature register value to milli-degrees Celsius
*/
int32_t TMP75B_toMilliCelsius(int32_t x)
{
/* Shift raw register value to form a proper Q4 value */
x >>= 4;
/* Scale to milli-degrees, convert Q4 value to a whole number */
return ((1000 * x) >> 4);
}
/*
* ======== TMP75B_toFloatCelsius ========
* Convert raw temperature register value to degrees Celsius
*/
float TMP75B_toFloatCelsius(int32_t x)
{
/* Shift raw register value to form a proper Q4 value */
x >>= 4;
/* Convert Q4 value to a float */
return ((float)x * 0.0625f);
}
where input x is the two bytes read from I2C:
x = (((int32_t)(int8_t)rxBuf[0]) << 8) | rxBuf[1];
additionally, if only one byte is read, that byte is integer temperature in signed 8 bit format (signed char or similar.)