/** ********************************************************************* * * @file sht_40.c * @brief SHT40 driver header file * * @date 2024-04-14 19:18:15 * @author CT * * @details Provides functions to interact with the SHT40 temperature and * humidity sensor. * ************************************************************************* **/ #include #include "i2c.h" #include "sht_40.h" #define SHT40_ADDRESS (0x44 << 1) // REGISTER DEFINES #define SHT40_HPM 0xFD // High precision measurement #define SHT40_MPM 0xF6 // Medium precision measurement #define SHT40_LPM 0xE0 // Low precision measurement #define SHT40_READ_SN 0x89 // Read serial number #define SHT40_RESET 0x94 // Soft reset command static I2C_HandleTypeDef *m_hi2c; static float m_humidity; static float m_temperature_C; static float m_temperature_F; /** @brief Checks if the SHT4X device is on the bus and ready to communicate @param hi2c Pointer to a I2C_HandleTypeDef structure that contains * the configuration information for the specified I2C. @returns 0 on sucess, 1 on failure **/ uint32_t sht40_init(I2C_HandleTypeDef *hi2c) { uint32_t ret = 0; // Store I2C interface m_hi2c = hi2c; // Check that SHT4X is present on the bus if(HAL_OK == HAL_I2C_IsDeviceReady(m_hi2c, SHT40_ADDRESS, 3, 100)) { ret = 0; } else { ret = 1; } return ret; } /* * TODO: Convert to fixed point or floating point */ uint32_t sht40_read(void) { uint8_t buf[8]; buf[0] = SHT40_HPM; // high precision measurement can take up to 8.3ms HAL_I2C_Master_Transmit(m_hi2c, SHT40_ADDRESS, buf, 1, 10); HAL_Delay(10); HAL_I2C_Master_Receive(m_hi2c, SHT40_ADDRESS, buf, 6, 10); m_temperature_C = (float)((buf[1] + (buf[0] << 8)) / 65534.0f); m_temperature_F = m_temperature_C; m_temperature_C = (m_temperature_C * 175.0f) - 45.0f; m_temperature_F = (m_temperature_F * 315.0f) - 49.0f; m_humidity = buf[4] + (buf[3] << 8); m_humidity = (125.0f * (float)(m_humidity / (65534.0f))) - 6.0f; return (0); } float sht40_get_rh(void) { return(m_humidity); } float sht40_get_temp_C(void) { return(m_temperature_C); } float sht40_get_temp_F(void) { return(m_temperature_F); }