问题
I am working on ST Temperature sensor( hts221 )
, I use I2C
command communication with sensor.
I see from the document like the following text.
enter code here Temperature data are expressed as TEMP_OUT_H & TEMP_OUT_L as 2’s complement numbers.
And the following picture is the description from document.
And the Temperature data
read from the sensor is like the following
TEMP_OUT_L is 0xA8
TEMP_OUT_H is 0xFF
How to convert the value of TEMP_OUT_L and TEMP_OUT_H to the Temperature data ?
Thanks in advance ?
回答1:
By concatenating the bits in the two values, to form a single 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;
This just assumes you have a function uint8_t i2c_read_byte(uint8_t address);
that can be used to read out the two registers.
Of course, the next step would be to convert this raw binary number into an actual temperature in some proper unit (like degrees Celsius, or Kelvin). To do that, you need more information from the data sheet.
回答2:
On page 6 of the datasheet it 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 the two 8-bit high and low values. This gives you a single 16-bit value. Then you convert/scale that number between 0 and 65535 to the real temperature value.
I assumed that there must be an offset specified in the datasheet, because otherwise the temperature can only positive: between 0.0
and 65363 * 0.016
. This offset will be a negative value. I leave it up to you to find this offset.
回答3:
0xFF
part of 0xFFA8
seems suspicious for me, probably the device is configured to work in 8-bit mode (if it's even possible) , on the page 24 of the datasheet it's said
T0 and T1 are the actual calibration temperature values multiplied by 8.
So 0xA8
divided by 8 gives: 31.25 - isn't it temperature around you currently?
来源:https://stackoverflow.com/questions/25322210/how-to-convert-the-value-get-from-temperature-sensor