How to convert a value from a temperature sensor?

I am working on ST Temperature sensor( hts221 )

, I am using command communication I2C

with sensor.

I see from the document as the following text.

enter code here Temperature data are expressed as TEMP_OUT_H & TEMP_OUT_L as 2’s complement numbers.

And the next image is the description from the doc. enter image description here

And Temperature data

read from the sensor looks like this

TEMP_OUT_L is 0xA8
TEMP_OUT_H is 0xFF

      

How can I convert the TEMP_OUT_L and TEMP_OUT_H values ​​to temperature data?

Thank you in advance?

+3


source to share


3 answers


By concatenating the bits in two values ​​to form one 16-bit value:

const temp_h = i2c_read_byte(TEMP_OUT_H);
const temp_l = i2c_read_byte(TEMP_OUT_L);
const uint16_t temp = (temp_h << 8) | temp_l;

      



It just assumes that you have a function uint8_t i2c_read_byte(uint8_t address);

that can be used to read two registers.

Of course, the next step would be to convert that raw binary number to the actual temperature in some correct block (like Celsius or Kelvin degrees). To do this, you need additional information from the specification.

+2


source


Page 6 of the datasheet says:

Temperature sensitivity 0.016 °C/LSB

      

So, here's what you need to do:



#define TEMP_SENSITIVITY 0.016f
#define TEMP_OFFSET      ???    /* Get this value from the datasheet. */

unsigned char tempOutH;
unsigned char tempOutL;

/* Here you get the values for tempOutH and tempOutL. */

uint16_t tempData = (tempOutH << 8) | tempOutL;
float    temp     = (tempData * TEMP_SENSITIVITY) + TEMP_OFFSET;  

      

So what you do is concatenate two 8-bit high and low values. This gives you one 16-bit value. Then you convert / scale this number from 0 to 65535 to the actual temperature.

I assumed that there should be an offset given in the datasheet, since otherwise the temperature can only be positive: between 0.0

and 65363 * 0.016

. This offset will be negative. I leave this for you to find this offset.

+2


source


0xFF

part 0xFFA8

seems suspicious to me, maybe the device is configured to run in 8-bit mode (if possible), on page 24 of the datasheet he said

T0 and T1 are the actual calibration temperature values ​​multiplied by 8.

So, 0xA8

divided by 8 gives: 31.25 - isn't that the temperature around you at the moment?

0


source







All Articles