Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: VL53L0X
Fork of BSP_B-L475E-IOT01 by
hts221.c
00001 /** 00002 ****************************************************************************** 00003 * @file hts221.c 00004 * @author MCD Application Team 00005 * @version V1.0.0 00006 * @date 14-February-2017 00007 * @brief This file provides a set of functions needed to manage the HTS221 00008 * humidity and temperature devices 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without modification, 00015 * are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00022 * may be used to endorse or promote products derived from this software 00023 * without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00028 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00029 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00030 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00031 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00033 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00034 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 * 00036 ****************************************************************************** 00037 */ 00038 00039 /* Includes ------------------------------------------------------------------*/ 00040 #include "hts221.h" 00041 00042 /** @addtogroup BSP 00043 * @{ 00044 */ 00045 00046 /** @addtogroup Component 00047 * @{ 00048 */ 00049 00050 /** @defgroup HTS221 HTS221 00051 * @{ 00052 */ 00053 00054 /** @defgroup HTS221_Private_Variables HTS221 Private Variables 00055 * @{ 00056 */ 00057 /* HTS221 Humidity Private Variables */ 00058 HSENSOR_DrvTypeDef HTS221_H_Drv = 00059 { 00060 HTS221_H_Init, 00061 HTS221_H_ReadID, 00062 HTS221_H_ReadHumidity 00063 }; 00064 00065 /* HTS221_Temperature_Private_Variables */ 00066 TSENSOR_DrvTypeDef HTS221_T_Drv = 00067 { 00068 HTS221_T_Init, 00069 0, 00070 0, 00071 HTS221_T_ReadTemp 00072 }; 00073 /** 00074 * @} 00075 */ 00076 00077 /** @defgroup HTS221_Humidity_Private_Functions HTS221 Humidity Private Functions 00078 * @{ 00079 */ 00080 /** 00081 * @brief Set HTS221 humidity sensor Initialization. 00082 */ 00083 void HTS221_H_Init(uint16_t DeviceAddr) 00084 { 00085 uint8_t tmp; 00086 00087 /* Read CTRL_REG1 */ 00088 tmp = SENSOR_IO_Read(DeviceAddr, HTS221_CTRL_REG1); 00089 00090 /* Enable BDU */ 00091 tmp &= ~HTS221_BDU_MASK; 00092 tmp |= (1 << HTS221_BDU_BIT); 00093 00094 /* Set default ODR */ 00095 tmp &= ~HTS221_ODR_MASK; 00096 tmp |= (uint8_t)0x01; /* Set ODR to 1Hz */ 00097 00098 /* Activate the device */ 00099 tmp |= HTS221_PD_MASK; 00100 00101 /* Apply settings to CTRL_REG1 */ 00102 SENSOR_IO_Write(DeviceAddr, HTS221_CTRL_REG1, tmp); 00103 } 00104 00105 /** 00106 * @brief Read HTS221 ID. 00107 * @retval ID 00108 */ 00109 uint8_t HTS221_H_ReadID(uint16_t DeviceAddr) 00110 { 00111 uint8_t ctrl = 0x00; 00112 00113 /* IO interface initialization */ 00114 SENSOR_IO_Init(); 00115 00116 /* Read value at Who am I register address */ 00117 ctrl = SENSOR_IO_Read(DeviceAddr, HTS221_WHO_AM_I_REG); 00118 00119 return ctrl; 00120 } 00121 00122 /** 00123 * @brief Read humidity value of HTS221 00124 * @retval humidity value; 00125 */ 00126 float HTS221_H_ReadHumidity(uint16_t DeviceAddr) 00127 { 00128 int16_t H0_T0_out, H1_T0_out, H_T_out; 00129 int16_t H0_rh, H1_rh; 00130 uint8_t buffer[2]; 00131 float tmp_f; 00132 00133 SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_H0_RH_X2 | 0x80), buffer, 2); 00134 00135 H0_rh = buffer[0] >> 1; 00136 H1_rh = buffer[1] >> 1; 00137 00138 SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_H0_T0_OUT_L | 0x80), buffer, 2); 00139 00140 H0_T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; 00141 00142 SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_H1_T0_OUT_L | 0x80), buffer, 2); 00143 00144 H1_T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; 00145 00146 SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_HR_OUT_L_REG | 0x80), buffer, 2); 00147 00148 H_T_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; 00149 00150 tmp_f = (float)(H_T_out - H0_T0_out) * (float)(H1_rh - H0_rh) / (float)(H1_T0_out - H0_T0_out) + H0_rh; 00151 tmp_f *= 10.0f; 00152 00153 tmp_f = ( tmp_f > 1000.0f ) ? 1000.0f 00154 : ( tmp_f < 0.0f ) ? 0.0f 00155 : tmp_f; 00156 00157 return (tmp_f / 10.0f); 00158 } 00159 00160 00161 /** 00162 * @} 00163 */ 00164 00165 /** @defgroup HTS221_Temperature_Private_Functions HTS221 Temperature Private Functions 00166 * @{ 00167 */ 00168 00169 /** 00170 * @brief Set HTS221 temperature sensor Initialization. 00171 * @param DeviceAddr: I2C device address 00172 * @param InitStruct: pointer to a TSENSOR_InitTypeDef structure 00173 * that contains the configuration setting for the HTS221. 00174 */ 00175 void HTS221_T_Init(uint16_t DeviceAddr, TSENSOR_InitTypeDef *pInitStruct) 00176 { 00177 uint8_t tmp; 00178 00179 /* Read CTRL_REG1 */ 00180 tmp = SENSOR_IO_Read(DeviceAddr, HTS221_CTRL_REG1); 00181 00182 /* Enable BDU */ 00183 tmp &= ~HTS221_BDU_MASK; 00184 tmp |= (1 << HTS221_BDU_BIT); 00185 00186 /* Set default ODR */ 00187 tmp &= ~HTS221_ODR_MASK; 00188 tmp |= (uint8_t)0x01; /* Set ODR to 1Hz */ 00189 00190 /* Activate the device */ 00191 tmp |= HTS221_PD_MASK; 00192 00193 /* Apply settings to CTRL_REG1 */ 00194 SENSOR_IO_Write(DeviceAddr, HTS221_CTRL_REG1, tmp); 00195 } 00196 00197 /** 00198 * @brief Read temperature value of HTS221 00199 * @param DeviceAddr: I2C device address 00200 * @retval temperature value 00201 */ 00202 float HTS221_T_ReadTemp(uint16_t DeviceAddr) 00203 { 00204 int16_t T0_out, T1_out, T_out, T0_degC_x8_u16, T1_degC_x8_u16; 00205 int16_t T0_degC, T1_degC; 00206 uint8_t buffer[4], tmp; 00207 float tmp_f; 00208 00209 SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_T0_DEGC_X8 | 0x80), buffer, 2); 00210 tmp = SENSOR_IO_Read(DeviceAddr, HTS221_T0_T1_DEGC_H2); 00211 00212 T0_degC_x8_u16 = (((uint16_t)(tmp & 0x03)) << 8) | ((uint16_t)buffer[0]); 00213 T1_degC_x8_u16 = (((uint16_t)(tmp & 0x0C)) << 6) | ((uint16_t)buffer[1]); 00214 T0_degC = T0_degC_x8_u16 >> 3; 00215 T1_degC = T1_degC_x8_u16 >> 3; 00216 00217 SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_T0_OUT_L | 0x80), buffer, 4); 00218 00219 T0_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; 00220 T1_out = (((uint16_t)buffer[3]) << 8) | (uint16_t)buffer[2]; 00221 00222 SENSOR_IO_ReadMultiple(DeviceAddr, (HTS221_TEMP_OUT_L_REG | 0x80), buffer, 2); 00223 00224 T_out = (((uint16_t)buffer[1]) << 8) | (uint16_t)buffer[0]; 00225 00226 tmp_f = (float)(T_out - T0_out) * (float)(T1_degC - T0_degC) / (float)(T1_out - T0_out) + T0_degC; 00227 00228 return tmp_f; 00229 } 00230 00231 /** 00232 * @} 00233 */ 00234 00235 /** 00236 * @} 00237 */ 00238 00239 /** 00240 * @} 00241 */ 00242 00243 /** 00244 * @} 00245 */ 00246 00247 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 15:06:31 by
1.7.2
