class IMU nucleo
Dependents: Coupe-Robotique-FIP-Main
Fork of IMU_FIP by
Components/HTS221/hts221.cpp@5:e2e603447679, 2015-05-21 (annotated)
- Committer:
- quentin9696
- Date:
- Thu May 21 11:21:58 2015 +0000
- Revision:
- 5:e2e603447679
- Parent:
- 0:528e23a13fb7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
quentin9696 | 0:528e23a13fb7 | 1 | /** |
quentin9696 | 0:528e23a13fb7 | 2 | ****************************************************************************** |
quentin9696 | 0:528e23a13fb7 | 3 | * @file x_cube_mems_hts221.h |
quentin9696 | 0:528e23a13fb7 | 4 | * @author AST / EST |
quentin9696 | 0:528e23a13fb7 | 5 | * @version V0.0.1 |
quentin9696 | 0:528e23a13fb7 | 6 | * @date 1-December-2014 |
quentin9696 | 0:528e23a13fb7 | 7 | * @brief Header file for component HTS221 |
quentin9696 | 0:528e23a13fb7 | 8 | ****************************************************************************** |
quentin9696 | 0:528e23a13fb7 | 9 | * @attention |
quentin9696 | 0:528e23a13fb7 | 10 | * |
quentin9696 | 0:528e23a13fb7 | 11 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
quentin9696 | 0:528e23a13fb7 | 12 | * |
quentin9696 | 0:528e23a13fb7 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
quentin9696 | 0:528e23a13fb7 | 14 | * are permitted provided that the following conditions are met: |
quentin9696 | 0:528e23a13fb7 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
quentin9696 | 0:528e23a13fb7 | 16 | * this list of conditions and the following disclaimer. |
quentin9696 | 0:528e23a13fb7 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
quentin9696 | 0:528e23a13fb7 | 18 | * this list of conditions and the following disclaimer in the documentation |
quentin9696 | 0:528e23a13fb7 | 19 | * and/or other materials provided with the distribution. |
quentin9696 | 0:528e23a13fb7 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
quentin9696 | 0:528e23a13fb7 | 21 | * may be used to endorse or promote products derived from this software |
quentin9696 | 0:528e23a13fb7 | 22 | * without specific prior written permission. |
quentin9696 | 0:528e23a13fb7 | 23 | * |
quentin9696 | 0:528e23a13fb7 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
quentin9696 | 0:528e23a13fb7 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
quentin9696 | 0:528e23a13fb7 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
quentin9696 | 0:528e23a13fb7 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
quentin9696 | 0:528e23a13fb7 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
quentin9696 | 0:528e23a13fb7 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
quentin9696 | 0:528e23a13fb7 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
quentin9696 | 0:528e23a13fb7 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
quentin9696 | 0:528e23a13fb7 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
quentin9696 | 0:528e23a13fb7 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
quentin9696 | 0:528e23a13fb7 | 34 | * |
quentin9696 | 0:528e23a13fb7 | 35 | ****************************************************************************** |
quentin9696 | 0:528e23a13fb7 | 36 | */ |
quentin9696 | 0:528e23a13fb7 | 37 | |
quentin9696 | 0:528e23a13fb7 | 38 | /* Includes ------------------------------------------------------------------*/ |
quentin9696 | 0:528e23a13fb7 | 39 | #include "mbed.h" |
quentin9696 | 0:528e23a13fb7 | 40 | #include "hts221.h" |
quentin9696 | 0:528e23a13fb7 | 41 | #include "hts221_platform.h" |
quentin9696 | 0:528e23a13fb7 | 42 | |
quentin9696 | 0:528e23a13fb7 | 43 | |
quentin9696 | 0:528e23a13fb7 | 44 | /* Temperature in degree for calibration */ |
quentin9696 | 0:528e23a13fb7 | 45 | float T0_degC, T1_degC; |
quentin9696 | 0:528e23a13fb7 | 46 | |
quentin9696 | 0:528e23a13fb7 | 47 | /* Output temperature value for calibration */ |
quentin9696 | 0:528e23a13fb7 | 48 | int16_t T0_out, T1_out; |
quentin9696 | 0:528e23a13fb7 | 49 | |
quentin9696 | 0:528e23a13fb7 | 50 | |
quentin9696 | 0:528e23a13fb7 | 51 | /* Humidity for calibration */ |
quentin9696 | 0:528e23a13fb7 | 52 | float H0_rh, H1_rh; |
quentin9696 | 0:528e23a13fb7 | 53 | |
quentin9696 | 0:528e23a13fb7 | 54 | /* Output Humidity value for calibration */ |
quentin9696 | 0:528e23a13fb7 | 55 | int16_t H0_T0_out, H1_T0_out; |
quentin9696 | 0:528e23a13fb7 | 56 | |
quentin9696 | 0:528e23a13fb7 | 57 | /* Methods -------------------------------------------------------------------*/ |
quentin9696 | 0:528e23a13fb7 | 58 | |
quentin9696 | 0:528e23a13fb7 | 59 | /** |
quentin9696 | 0:528e23a13fb7 | 60 | * @brief Read HTS221 output register, and calculate the temperature. |
quentin9696 | 0:528e23a13fb7 | 61 | * @param pfData : Data out pointer |
quentin9696 | 0:528e23a13fb7 | 62 | * @retval None |
quentin9696 | 0:528e23a13fb7 | 63 | */ |
quentin9696 | 0:528e23a13fb7 | 64 | int HTS221::GetTemperature(float* pfData) |
quentin9696 | 0:528e23a13fb7 | 65 | { |
quentin9696 | 0:528e23a13fb7 | 66 | int16_t T_out, temperature_t; |
quentin9696 | 0:528e23a13fb7 | 67 | uint8_t tempReg[2] = {0,0}; |
quentin9696 | 0:528e23a13fb7 | 68 | uint8_t tmp = 0x00; |
quentin9696 | 0:528e23a13fb7 | 69 | float T_degC; |
quentin9696 | 0:528e23a13fb7 | 70 | int ret=-1;//TODO:Define Error types? |
quentin9696 | 0:528e23a13fb7 | 71 | |
quentin9696 | 0:528e23a13fb7 | 72 | if(isInitialized()==0) |
quentin9696 | 0:528e23a13fb7 | 73 | { |
quentin9696 | 0:528e23a13fb7 | 74 | return ret; |
quentin9696 | 0:528e23a13fb7 | 75 | } |
quentin9696 | 0:528e23a13fb7 | 76 | |
quentin9696 | 0:528e23a13fb7 | 77 | ret = dev_i2c.i2c_read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 78 | |
quentin9696 | 0:528e23a13fb7 | 79 | /* Output Data Rate selection */ |
quentin9696 | 0:528e23a13fb7 | 80 | tmp &= (HTS221_ODR_MASK); |
quentin9696 | 0:528e23a13fb7 | 81 | |
quentin9696 | 0:528e23a13fb7 | 82 | if(tmp == 0x00){ |
quentin9696 | 0:528e23a13fb7 | 83 | |
quentin9696 | 0:528e23a13fb7 | 84 | ret = dev_i2c.i2c_read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 85 | |
quentin9696 | 0:528e23a13fb7 | 86 | /* Serial Interface Mode selection */ |
quentin9696 | 0:528e23a13fb7 | 87 | tmp &= ~(HTS221_ONE_SHOT_MASK); |
quentin9696 | 0:528e23a13fb7 | 88 | tmp |= HTS221_ONE_SHOT_START; |
quentin9696 | 0:528e23a13fb7 | 89 | |
quentin9696 | 0:528e23a13fb7 | 90 | ret = dev_i2c.i2c_write(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 91 | |
quentin9696 | 0:528e23a13fb7 | 92 | do{ |
quentin9696 | 0:528e23a13fb7 | 93 | |
quentin9696 | 0:528e23a13fb7 | 94 | ret = dev_i2c.i2c_read(&tmp, HTS221_ADDRESS, HTS221_STATUS_REG_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 95 | }while(!(tmp&&0x01)); |
quentin9696 | 0:528e23a13fb7 | 96 | |
quentin9696 | 0:528e23a13fb7 | 97 | } |
quentin9696 | 0:528e23a13fb7 | 98 | |
quentin9696 | 0:528e23a13fb7 | 99 | ret = dev_i2c.i2c_read(&tempReg[0], HTS221_ADDRESS , HTS221_TEMP_OUT_L_ADDR + 0x80, 2); |
quentin9696 | 0:528e23a13fb7 | 100 | T_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]); |
quentin9696 | 0:528e23a13fb7 | 101 | |
quentin9696 | 0:528e23a13fb7 | 102 | T_degC = ((float)(T_out - T0_out))/(T1_out - T0_out) * (T1_degC - T0_degC) + T0_degC; |
quentin9696 | 0:528e23a13fb7 | 103 | |
quentin9696 | 0:528e23a13fb7 | 104 | temperature_t = (int16_t)(T_degC * pow((double)10,(double)TEMP_DECIMAL_DIGITS)); |
quentin9696 | 0:528e23a13fb7 | 105 | |
quentin9696 | 0:528e23a13fb7 | 106 | *pfData = ((float)temperature_t)/pow((double)10,(double)TEMP_DECIMAL_DIGITS); |
quentin9696 | 0:528e23a13fb7 | 107 | |
quentin9696 | 0:528e23a13fb7 | 108 | return ret; |
quentin9696 | 0:528e23a13fb7 | 109 | } |
quentin9696 | 0:528e23a13fb7 | 110 | |
quentin9696 | 0:528e23a13fb7 | 111 | |
quentin9696 | 0:528e23a13fb7 | 112 | /** |
quentin9696 | 0:528e23a13fb7 | 113 | * @brief Read HTS221 output register, and calculate the humidity. |
quentin9696 | 0:528e23a13fb7 | 114 | * @param pfData : Data out pointer |
quentin9696 | 0:528e23a13fb7 | 115 | * @retval None |
quentin9696 | 0:528e23a13fb7 | 116 | */ |
quentin9696 | 0:528e23a13fb7 | 117 | int HTS221::GetHumidity(float* pfData) |
quentin9696 | 0:528e23a13fb7 | 118 | { |
quentin9696 | 0:528e23a13fb7 | 119 | int16_t H_T_out, humidity_t; |
quentin9696 | 0:528e23a13fb7 | 120 | uint8_t tempReg[2] = {0,0}; |
quentin9696 | 0:528e23a13fb7 | 121 | uint8_t tmp = 0x00; |
quentin9696 | 0:528e23a13fb7 | 122 | float H_rh; |
quentin9696 | 0:528e23a13fb7 | 123 | int ret; |
quentin9696 | 0:528e23a13fb7 | 124 | |
quentin9696 | 0:528e23a13fb7 | 125 | if(isInitialized()==0) |
quentin9696 | 0:528e23a13fb7 | 126 | { |
quentin9696 | 0:528e23a13fb7 | 127 | pfData = 0; |
quentin9696 | 0:528e23a13fb7 | 128 | return -1; |
quentin9696 | 0:528e23a13fb7 | 129 | } |
quentin9696 | 0:528e23a13fb7 | 130 | |
quentin9696 | 0:528e23a13fb7 | 131 | //HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 132 | ret = dev_i2c.i2c_read(&tmp, HTS221_ADDRESS , HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 133 | |
quentin9696 | 0:528e23a13fb7 | 134 | /* Output Data Rate selection */ |
quentin9696 | 0:528e23a13fb7 | 135 | tmp &= (HTS221_ODR_MASK); |
quentin9696 | 0:528e23a13fb7 | 136 | |
quentin9696 | 0:528e23a13fb7 | 137 | if(tmp == 0x00){ |
quentin9696 | 0:528e23a13fb7 | 138 | |
quentin9696 | 0:528e23a13fb7 | 139 | //HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 140 | ret = dev_i2c.i2c_read(&tmp, HTS221_ADDRESS , HTS221_CTRL_REG2_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 141 | |
quentin9696 | 0:528e23a13fb7 | 142 | /* Serial Interface Mode selection */ |
quentin9696 | 0:528e23a13fb7 | 143 | tmp &= ~(HTS221_ONE_SHOT_MASK); |
quentin9696 | 0:528e23a13fb7 | 144 | tmp |= HTS221_ONE_SHOT_START; |
quentin9696 | 0:528e23a13fb7 | 145 | |
quentin9696 | 0:528e23a13fb7 | 146 | //HUM_TEMP_IO_Write(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG2_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 147 | ret = dev_i2c.i2c_write(&tmp, HTS221_ADDRESS , HTS221_CTRL_REG2_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 148 | |
quentin9696 | 0:528e23a13fb7 | 149 | do{ |
quentin9696 | 0:528e23a13fb7 | 150 | |
quentin9696 | 0:528e23a13fb7 | 151 | //HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_STATUS_REG_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 152 | ret = dev_i2c.i2c_read(&tmp, HTS221_ADDRESS , HTS221_STATUS_REG_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 153 | |
quentin9696 | 0:528e23a13fb7 | 154 | }while(!(tmp&&0x02)); |
quentin9696 | 0:528e23a13fb7 | 155 | |
quentin9696 | 0:528e23a13fb7 | 156 | } |
quentin9696 | 0:528e23a13fb7 | 157 | |
quentin9696 | 0:528e23a13fb7 | 158 | |
quentin9696 | 0:528e23a13fb7 | 159 | //HUM_TEMP_IO_Read(&tempReg[0], HTS221_ADDRESS, HTS221_HUMIDITY_OUT_L_ADDR + 0x80, 2); |
quentin9696 | 0:528e23a13fb7 | 160 | ret = dev_i2c.i2c_read(&tempReg[0], HTS221_ADDRESS , HTS221_HUMIDITY_OUT_L_ADDR + 0x80, 2); |
quentin9696 | 0:528e23a13fb7 | 161 | H_T_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]); |
quentin9696 | 0:528e23a13fb7 | 162 | |
quentin9696 | 0:528e23a13fb7 | 163 | H_rh = ((float)(H_T_out - H0_T0_out))/(H1_T0_out - H0_T0_out) * (H1_rh - H0_rh) + H0_rh; |
quentin9696 | 0:528e23a13fb7 | 164 | |
quentin9696 | 0:528e23a13fb7 | 165 | humidity_t = (uint16_t)(H_rh * pow((double)10,(double)HUM_DECIMAL_DIGITS)); |
quentin9696 | 0:528e23a13fb7 | 166 | |
quentin9696 | 0:528e23a13fb7 | 167 | *pfData = ((float)humidity_t)/pow((double)10,(double)HUM_DECIMAL_DIGITS); |
quentin9696 | 0:528e23a13fb7 | 168 | |
quentin9696 | 0:528e23a13fb7 | 169 | return ret; |
quentin9696 | 0:528e23a13fb7 | 170 | } |
quentin9696 | 0:528e23a13fb7 | 171 | |
quentin9696 | 0:528e23a13fb7 | 172 | |
quentin9696 | 0:528e23a13fb7 | 173 | /** |
quentin9696 | 0:528e23a13fb7 | 174 | * @brief Read ID address of HTS221 |
quentin9696 | 0:528e23a13fb7 | 175 | * @param Device ID address |
quentin9696 | 0:528e23a13fb7 | 176 | * @retval ID name |
quentin9696 | 0:528e23a13fb7 | 177 | */ |
quentin9696 | 0:528e23a13fb7 | 178 | uint8_t HTS221::ReadID(void) |
quentin9696 | 0:528e23a13fb7 | 179 | { |
quentin9696 | 0:528e23a13fb7 | 180 | uint8_t tmp; |
quentin9696 | 0:528e23a13fb7 | 181 | int ret; |
quentin9696 | 0:528e23a13fb7 | 182 | |
quentin9696 | 0:528e23a13fb7 | 183 | /* Read WHO I AM register */ |
quentin9696 | 0:528e23a13fb7 | 184 | //HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_WHO_AM_I_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 185 | ret = dev_i2c.i2c_read(&tmp, HTS221_ADDRESS, HTS221_WHO_AM_I_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 186 | |
quentin9696 | 0:528e23a13fb7 | 187 | /* Return the ID */ |
quentin9696 | 0:528e23a13fb7 | 188 | return ((ret == 0) ? (uint8_t)tmp : 0); |
quentin9696 | 0:528e23a13fb7 | 189 | } |
quentin9696 | 0:528e23a13fb7 | 190 | |
quentin9696 | 0:528e23a13fb7 | 191 | /** |
quentin9696 | 0:528e23a13fb7 | 192 | * @brief Set HTS221 Initialization. |
quentin9696 | 0:528e23a13fb7 | 193 | * @param InitStruct: it contains the configuration setting for the HTS221. |
quentin9696 | 0:528e23a13fb7 | 194 | * @retval None |
quentin9696 | 0:528e23a13fb7 | 195 | */ |
quentin9696 | 0:528e23a13fb7 | 196 | void HTS221::Init() { |
quentin9696 | 0:528e23a13fb7 | 197 | |
quentin9696 | 0:528e23a13fb7 | 198 | uint8_t tmp = 0x00; |
quentin9696 | 0:528e23a13fb7 | 199 | int ret; |
quentin9696 | 0:528e23a13fb7 | 200 | |
quentin9696 | 0:528e23a13fb7 | 201 | Power_ON(); |
quentin9696 | 0:528e23a13fb7 | 202 | |
quentin9696 | 0:528e23a13fb7 | 203 | HTS221_Calibration(); |
quentin9696 | 0:528e23a13fb7 | 204 | |
quentin9696 | 0:528e23a13fb7 | 205 | //HUM_TEMP_IO_Read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 206 | ret = dev_i2c.i2c_read(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 207 | |
quentin9696 | 0:528e23a13fb7 | 208 | if (ret == 0) |
quentin9696 | 0:528e23a13fb7 | 209 | { |
quentin9696 | 0:528e23a13fb7 | 210 | /* Output Data Rate selection */ |
quentin9696 | 0:528e23a13fb7 | 211 | tmp &= ~(HTS221_ODR_MASK); |
quentin9696 | 0:528e23a13fb7 | 212 | tmp |= HTS221_ODR_12_5Hz; |
quentin9696 | 0:528e23a13fb7 | 213 | |
quentin9696 | 0:528e23a13fb7 | 214 | //HUM_TEMP_IO_Write(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 215 | ret = dev_i2c.i2c_write(&tmp, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 216 | } |
quentin9696 | 0:528e23a13fb7 | 217 | |
quentin9696 | 0:528e23a13fb7 | 218 | if (ret == 0) |
quentin9696 | 0:528e23a13fb7 | 219 | { |
quentin9696 | 0:528e23a13fb7 | 220 | if(ReadID() == I_AM_HTS221) |
quentin9696 | 0:528e23a13fb7 | 221 | { |
quentin9696 | 0:528e23a13fb7 | 222 | HumTempInitialized = 1; |
quentin9696 | 0:528e23a13fb7 | 223 | //ret = HUM_TEMP_OK; |
quentin9696 | 0:528e23a13fb7 | 224 | } |
quentin9696 | 0:528e23a13fb7 | 225 | } |
quentin9696 | 0:528e23a13fb7 | 226 | |
quentin9696 | 0:528e23a13fb7 | 227 | return; |
quentin9696 | 0:528e23a13fb7 | 228 | } |
quentin9696 | 0:528e23a13fb7 | 229 | |
quentin9696 | 0:528e23a13fb7 | 230 | int HTS221::Power_ON() { |
quentin9696 | 0:528e23a13fb7 | 231 | |
quentin9696 | 0:528e23a13fb7 | 232 | uint8_t tmpReg; |
quentin9696 | 0:528e23a13fb7 | 233 | |
quentin9696 | 0:528e23a13fb7 | 234 | /* Read the register content */ |
quentin9696 | 0:528e23a13fb7 | 235 | int ret; |
quentin9696 | 0:528e23a13fb7 | 236 | ret = dev_i2c.i2c_read(&tmpReg, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 237 | if(ret) return ret; |
quentin9696 | 0:528e23a13fb7 | 238 | |
quentin9696 | 0:528e23a13fb7 | 239 | /* Set the power down bit */ |
quentin9696 | 0:528e23a13fb7 | 240 | tmpReg |= HTS221_MODE_ACTIVE; |
quentin9696 | 0:528e23a13fb7 | 241 | |
quentin9696 | 0:528e23a13fb7 | 242 | /* Write register */ |
quentin9696 | 0:528e23a13fb7 | 243 | ret = dev_i2c.i2c_write(&tmpReg, HTS221_ADDRESS, HTS221_CTRL_REG1_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 244 | if(ret) return ret; |
quentin9696 | 0:528e23a13fb7 | 245 | return ret; |
quentin9696 | 0:528e23a13fb7 | 246 | } |
quentin9696 | 0:528e23a13fb7 | 247 | |
quentin9696 | 0:528e23a13fb7 | 248 | int HTS221::HTS221_Calibration() { |
quentin9696 | 0:528e23a13fb7 | 249 | |
quentin9696 | 0:528e23a13fb7 | 250 | if(HumTempInitialized == 1) |
quentin9696 | 0:528e23a13fb7 | 251 | { |
quentin9696 | 0:528e23a13fb7 | 252 | return 1; //TODO: Error Codes definitions |
quentin9696 | 0:528e23a13fb7 | 253 | } |
quentin9696 | 0:528e23a13fb7 | 254 | |
quentin9696 | 0:528e23a13fb7 | 255 | /* Temperature Calibration */ |
quentin9696 | 0:528e23a13fb7 | 256 | /* Temperature in degree for calibration ( "/8" to obtain float) */ |
quentin9696 | 0:528e23a13fb7 | 257 | uint16_t T0_degC_x8_L, T0_degC_x8_H, T1_degC_x8_L, T1_degC_x8_H; |
quentin9696 | 0:528e23a13fb7 | 258 | uint8_t H0_rh_x2, H1_rh_x2; |
quentin9696 | 0:528e23a13fb7 | 259 | uint8_t tempReg[2] = {0,0}; |
quentin9696 | 0:528e23a13fb7 | 260 | |
quentin9696 | 0:528e23a13fb7 | 261 | int ret; |
quentin9696 | 0:528e23a13fb7 | 262 | ret = dev_i2c.i2c_read(tempReg, HTS221_ADDRESS, HTS221_T0_degC_X8_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 263 | if(ret) return ret; |
quentin9696 | 0:528e23a13fb7 | 264 | |
quentin9696 | 0:528e23a13fb7 | 265 | T0_degC_x8_L = (uint16_t)tempReg[0]; |
quentin9696 | 0:528e23a13fb7 | 266 | |
quentin9696 | 0:528e23a13fb7 | 267 | ret = dev_i2c.i2c_read(tempReg, HTS221_ADDRESS, HTS221_T1_T0_MSB_X8_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 268 | if(ret) return ret; |
quentin9696 | 0:528e23a13fb7 | 269 | T0_degC_x8_H = (uint16_t) (tempReg[0] & 0x03); |
quentin9696 | 0:528e23a13fb7 | 270 | |
quentin9696 | 0:528e23a13fb7 | 271 | T0_degC = ((float)((T0_degC_x8_H<<8) | (T0_degC_x8_L)))/8; |
quentin9696 | 0:528e23a13fb7 | 272 | |
quentin9696 | 0:528e23a13fb7 | 273 | ret = dev_i2c.i2c_read(tempReg, HTS221_ADDRESS, HTS221_T1_degC_X8_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 274 | T1_degC_x8_L = (uint16_t)tempReg[0]; |
quentin9696 | 0:528e23a13fb7 | 275 | |
quentin9696 | 0:528e23a13fb7 | 276 | ret = dev_i2c.i2c_read(tempReg, HTS221_ADDRESS, HTS221_T1_T0_MSB_X8_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 277 | T1_degC_x8_H = (uint16_t) (tempReg[0] & 0x0C); |
quentin9696 | 0:528e23a13fb7 | 278 | T1_degC_x8_H = T1_degC_x8_H >> 2; |
quentin9696 | 0:528e23a13fb7 | 279 | |
quentin9696 | 0:528e23a13fb7 | 280 | T1_degC = ((float)((T1_degC_x8_H<<8) | (T1_degC_x8_L)))/8; |
quentin9696 | 0:528e23a13fb7 | 281 | |
quentin9696 | 0:528e23a13fb7 | 282 | ret = dev_i2c.i2c_read(tempReg, HTS221_ADDRESS, HTS221_T0_OUT_L_ADDR + 0x80, 2); |
quentin9696 | 0:528e23a13fb7 | 283 | T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]); |
quentin9696 | 0:528e23a13fb7 | 284 | |
quentin9696 | 0:528e23a13fb7 | 285 | ret = dev_i2c.i2c_read(tempReg, HTS221_ADDRESS, HTS221_T1_OUT_L_ADDR + 0x80, 2); |
quentin9696 | 0:528e23a13fb7 | 286 | T1_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]); |
quentin9696 | 0:528e23a13fb7 | 287 | |
quentin9696 | 0:528e23a13fb7 | 288 | /* Humidity Calibration */ |
quentin9696 | 0:528e23a13fb7 | 289 | /* Humidity in degree for calibration ( "/2" to obtain float) */ |
quentin9696 | 0:528e23a13fb7 | 290 | |
quentin9696 | 0:528e23a13fb7 | 291 | ret = dev_i2c.i2c_read(&H0_rh_x2, HTS221_ADDRESS, HTS221_H0_RH_X2_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 292 | |
quentin9696 | 0:528e23a13fb7 | 293 | ret = dev_i2c.i2c_read(&H1_rh_x2, HTS221_ADDRESS, HTS221_H1_RH_X2_ADDR, 1); |
quentin9696 | 0:528e23a13fb7 | 294 | |
quentin9696 | 0:528e23a13fb7 | 295 | ret = dev_i2c.i2c_read(&tempReg[0], HTS221_ADDRESS, HTS221_H0_T0_OUT_L_ADDR + 0x80, 2); |
quentin9696 | 0:528e23a13fb7 | 296 | H0_T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]); |
quentin9696 | 0:528e23a13fb7 | 297 | |
quentin9696 | 0:528e23a13fb7 | 298 | ret = dev_i2c.i2c_read(&tempReg[0], HTS221_ADDRESS, HTS221_H1_T0_OUT_L_ADDR + 0x80, 2); |
quentin9696 | 0:528e23a13fb7 | 299 | H1_T0_out = ((((int16_t)tempReg[1]) << 8)+(int16_t)tempReg[0]); |
quentin9696 | 0:528e23a13fb7 | 300 | |
quentin9696 | 0:528e23a13fb7 | 301 | H0_rh = ((float)H0_rh_x2)/2; |
quentin9696 | 0:528e23a13fb7 | 302 | H1_rh = ((float)H1_rh_x2)/2; |
quentin9696 | 0:528e23a13fb7 | 303 | |
quentin9696 | 0:528e23a13fb7 | 304 | return ret; |
quentin9696 | 0:528e23a13fb7 | 305 | } |