I want to use my HIH6030 humidity and temperature sensor with my STM32F4 board, but I have the following problem:
I turn my board on and start debugging. The first time when I try to read out it's ok. It seems I got good values, but next time I read data from the sensor I get exactly the same values as before. All the times I get the same values, until I restart my board.
I can't see what could be the problem. I hope you guys can help me.
My code:
#define HIH_ADDRESS 0x27
typedef struct {
double temperature;
double humidity;
} HIH_readout;
void initI2C() {
__HAL_RCC_I2C2_CLK_ENABLE();
hi2c2.Instance = I2C2;
hi2c2.Init.ClockSpeed = 400000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
initStatus = ERROR_I2C_INIT;
}
}
int HIH_read(HIH_readout* buffer) {
uint8_t data[4];
uint16_t temp_humidity;
uint16_t temp_temperature;
if(HAL_I2C_Master_Receive(&hi2c2, HIH_ADDRESS << 1,data, 4, 10) != HAL_OK){
buffer->humidity = 0;
buffer->temperature = 0;
return HIH_FAIL;
}
uint8_t status = (data[0] & (uint8_t)0xC0U);
//Masking MSB (status) bits
temp_humidity = ((data[0] & ((uint8_t)0x3FU)) << 8) | data[1];
//Masking LSB (don't care) bits
temp_temperature = (data[2] << 8) | (data[3] & ((uint8_t)0xFCU));
//The last two bits -> don't care
temp_temperature = temp_temperature >> 2;
//SCALE AND CAST
buffer->humidity = (double)(temp_humidity * 100 / (pow(2,14) - 1));
buffer->temperature = (double)(temp_temperature / (pow(2, 14) - 1) * 165 - 40);
return HIH_OK;
}
When my main begins I call HAL_Init(); initI2C(); and after in while(1), I call HIH_read; I used Cubemx to generate initialzation code, so hopefully it works well.