I use STM32F4 microcontroller and HAL library. I would like to read temperature from LSM303DLHC sensor. Following code is responsible for this task.
LSM303DLHC_Status_t LSM303DLHC_get_temp(int16_t *temp)
{
uint8_t data_h = 0x00;
uint8_t data_l = 0x00;
uint16_t data_tmp = 0x00;
uint8_t temp_enable = 0x00;
if (HAL_I2C_Mem_Read(&hi2c1, LSM303_MAGNE_ADDRESS, TEMP_OUT_H_M, 1, &data_h, 1, 100) != HAL_OK)
{
return LSM303DLHC_ERROR;
}
if (HAL_I2C_Mem_Read(&hi2c1, LSM303_MAGNE_ADDRESS, TEMP_OUT_L_M, 1, &data_l, 1, 100) != HAL_OK)
{
return LSM303DLHC_ERROR;
}
data_tmp = (int16_t)((data_h << 8) | data_l);
*temp = data_tmp/8;
/*Enable temperature sensor*/
if (HAL_I2C_Mem_Read(&hi2c1, LSM303_MAGNE_ADDRESS, CRA_REG_M, 1, &temp_enable, 1, 100) != HAL_OK)
{
return LSM303DLHC_ERROR;
}
temp_enable |= (0x01 << 7);
if (HAL_I2C_Mem_Write(&hi2c1, LSM303_MAGNE_ADDRESS, CRA_REG_M, 1, &temp_enable, 1, 100) != HAL_OK)
{
return LSM303DLHC_ERROR;
}
/*End enable temperature sensor*/
return LSM303DLHC_OK;
}
According to data sheet and many tutorial temperature is computed by this expression:
temp = (int16_t)((data_h << 8) | data_l);
temp = temp/8;
In room where I am is about 20 degree of Celsius. My function returns about 128-136 value.
Where did I make mistake?