Digital Humidity Sensor
SHT3X.cpp@2:870701ee67c4, 2021-03-25 (annotated)
- Committer:
- mcm
- Date:
- Thu Mar 25 20:24:25 2021 +0000
- Revision:
- 2:870701ee67c4
- Parent:
- 0:5e2a8080b276
- Child:
- 3:42e11b9135b5
The driver was completed and ready to test.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mcm | 2:870701ee67c4 | 1 | /** |
mcm | 2:870701ee67c4 | 2 | * @brief SHT3X.c |
mcm | 2:870701ee67c4 | 3 | * @details Humidity and Temperature Sensor. |
mcm | 2:870701ee67c4 | 4 | * Functions file. |
mcm | 2:870701ee67c4 | 5 | * |
mcm | 2:870701ee67c4 | 6 | * |
mcm | 2:870701ee67c4 | 7 | * @return N/A |
mcm | 2:870701ee67c4 | 8 | * |
mcm | 2:870701ee67c4 | 9 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 10 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 11 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 12 | * @pre N/A |
mcm | 2:870701ee67c4 | 13 | * @warning N/A |
mcm | 2:870701ee67c4 | 14 | * @pre This code belongs to Nimbus Centre ( http://www.nimbus.cit.ie ). |
mcm | 2:870701ee67c4 | 15 | */ |
mcm | 2:870701ee67c4 | 16 | |
mcm | 2:870701ee67c4 | 17 | #include "SHT3X.h" |
mcm | 2:870701ee67c4 | 18 | |
mcm | 2:870701ee67c4 | 19 | |
mcm | 2:870701ee67c4 | 20 | SHT3X::SHT3X ( PinName sda, PinName scl, uint32_t addr, uint32_t freq ) |
mcm | 2:870701ee67c4 | 21 | : _i2c ( sda, scl ) |
mcm | 2:870701ee67c4 | 22 | , _SHT3X_Addr ( addr ) |
mcm | 2:870701ee67c4 | 23 | { |
mcm | 2:870701ee67c4 | 24 | _i2c.frequency( freq ); |
mcm | 2:870701ee67c4 | 25 | } |
mcm | 2:870701ee67c4 | 26 | |
mcm | 2:870701ee67c4 | 27 | |
mcm | 2:870701ee67c4 | 28 | SHT3X::~SHT3X() |
mcm | 2:870701ee67c4 | 29 | { |
mcm | 2:870701ee67c4 | 30 | } |
mcm | 2:870701ee67c4 | 31 | |
mcm | 2:870701ee67c4 | 32 | |
mcm | 2:870701ee67c4 | 33 | |
mcm | 2:870701ee67c4 | 34 | |
mcm | 2:870701ee67c4 | 35 | /** |
mcm | 2:870701ee67c4 | 36 | * @brief SHT3X_OneShotTriggerAllData ( SHT3X_command_registers_single_shot_mode_t ) |
mcm | 2:870701ee67c4 | 37 | * @details It triggers all the raw data in single shot mode. |
mcm | 2:870701ee67c4 | 38 | * |
mcm | 2:870701ee67c4 | 39 | * @param[in] mode: Clock Stretching/No Stretching and the repeatability. |
mcm | 2:870701ee67c4 | 40 | * |
mcm | 2:870701ee67c4 | 41 | * @param[out] N/A |
mcm | 2:870701ee67c4 | 42 | * |
mcm | 2:870701ee67c4 | 43 | * |
mcm | 2:870701ee67c4 | 44 | * @return Status of SHT3X_OneShotTriggerAllData. |
mcm | 2:870701ee67c4 | 45 | * |
mcm | 2:870701ee67c4 | 46 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 47 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 48 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 49 | * @pre N/A |
mcm | 2:870701ee67c4 | 50 | * @warning N/A. |
mcm | 2:870701ee67c4 | 51 | */ |
mcm | 2:870701ee67c4 | 52 | SHT3X::SHT3X_status_t SHT3X::SHT3X_OneShotTriggerAllData ( SHT3X_command_registers_single_shot_mode_t mode ) |
mcm | 2:870701ee67c4 | 53 | { |
mcm | 2:870701ee67c4 | 54 | char cmd[2] = { 0U }; |
mcm | 2:870701ee67c4 | 55 | uint32_t aux; |
mcm | 2:870701ee67c4 | 56 | |
mcm | 2:870701ee67c4 | 57 | /* Write the register */ |
mcm | 2:870701ee67c4 | 58 | cmd[0] = (uint8_t)( mode >> 8U ); |
mcm | 2:870701ee67c4 | 59 | cmd[1] = (uint8_t)( mode & 0xFF ); |
mcm | 2:870701ee67c4 | 60 | aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); |
mcm | 2:870701ee67c4 | 61 | |
mcm | 2:870701ee67c4 | 62 | |
mcm | 2:870701ee67c4 | 63 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 64 | { |
mcm | 2:870701ee67c4 | 65 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 66 | } |
mcm | 2:870701ee67c4 | 67 | else |
mcm | 2:870701ee67c4 | 68 | { |
mcm | 2:870701ee67c4 | 69 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 70 | } |
mcm | 2:870701ee67c4 | 71 | } |
mcm | 2:870701ee67c4 | 72 | |
mcm | 2:870701ee67c4 | 73 | |
mcm | 2:870701ee67c4 | 74 | |
mcm | 2:870701ee67c4 | 75 | /** |
mcm | 2:870701ee67c4 | 76 | * @brief SHT3X_OneShotGetAllRawData ( SHT3X_raw_data_t* ) |
mcm | 2:870701ee67c4 | 77 | * @details It gets all the raw data in single shot mode. |
mcm | 2:870701ee67c4 | 78 | * |
mcm | 2:870701ee67c4 | 79 | * @param[in] N/A. |
mcm | 2:870701ee67c4 | 80 | * |
mcm | 2:870701ee67c4 | 81 | * @param[out] rawData: All the raw data and their CRC values. |
mcm | 2:870701ee67c4 | 82 | * |
mcm | 2:870701ee67c4 | 83 | * |
mcm | 2:870701ee67c4 | 84 | * @return Status of SHT3X_OneShotGetAllRawData. |
mcm | 2:870701ee67c4 | 85 | * |
mcm | 2:870701ee67c4 | 86 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 87 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 88 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 89 | * @pre N/A |
mcm | 2:870701ee67c4 | 90 | * @warning SHT3X_OneShotTriggerAllData function must be called first. The user MUST respect the |
mcm | 2:870701ee67c4 | 91 | * measurement duration as indicated in the datasheet (Table4.System timing specification, p7): |
mcm | 2:870701ee67c4 | 92 | * |
mcm | 2:870701ee67c4 | 93 | * Typ. Max |
mcm | 2:870701ee67c4 | 94 | * Low repeatability 2.5ms 4ms |
mcm | 2:870701ee67c4 | 95 | * Medium repeatability 4.5ms 6ms |
mcm | 2:870701ee67c4 | 96 | * High repeatability 12.5ms 15ms. |
mcm | 2:870701ee67c4 | 97 | */ |
mcm | 2:870701ee67c4 | 98 | SHT3X::SHT3X_status_t SHT3X::SHT3X_OneShotGetAllRawData ( SHT3X_raw_data_t* rawData ) |
mcm | 2:870701ee67c4 | 99 | { |
mcm | 2:870701ee67c4 | 100 | char cmd[6] = { 0U }; |
mcm | 2:870701ee67c4 | 101 | uint32_t aux; |
mcm | 2:870701ee67c4 | 102 | |
mcm | 2:870701ee67c4 | 103 | /* Read the register */ |
mcm | 2:870701ee67c4 | 104 | aux = _i2c.read ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); |
mcm | 2:870701ee67c4 | 105 | |
mcm | 2:870701ee67c4 | 106 | /* Parse the data */ |
mcm | 2:870701ee67c4 | 107 | rawData->rawTemperature = cmd[0]; |
mcm | 2:870701ee67c4 | 108 | rawData->rawTemperature <<= 8U; |
mcm | 2:870701ee67c4 | 109 | rawData->rawTemperature |= cmd[1]; |
mcm | 2:870701ee67c4 | 110 | |
mcm | 2:870701ee67c4 | 111 | rawData->temperatureCRC = cmd[2]; |
mcm | 2:870701ee67c4 | 112 | |
mcm | 2:870701ee67c4 | 113 | rawData->rawRelativeHumidity = cmd[3]; |
mcm | 2:870701ee67c4 | 114 | rawData->rawRelativeHumidity <<= 8U; |
mcm | 2:870701ee67c4 | 115 | rawData->rawRelativeHumidity |= cmd[4]; |
mcm | 2:870701ee67c4 | 116 | |
mcm | 2:870701ee67c4 | 117 | rawData->relativeHumidityCRC = cmd[5]; |
mcm | 2:870701ee67c4 | 118 | |
mcm | 2:870701ee67c4 | 119 | |
mcm | 2:870701ee67c4 | 120 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 121 | { |
mcm | 2:870701ee67c4 | 122 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 123 | } |
mcm | 2:870701ee67c4 | 124 | else |
mcm | 2:870701ee67c4 | 125 | { |
mcm | 2:870701ee67c4 | 126 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 127 | } |
mcm | 2:870701ee67c4 | 128 | } |
mcm | 2:870701ee67c4 | 129 | |
mcm | 2:870701ee67c4 | 130 | |
mcm | 2:870701ee67c4 | 131 | |
mcm | 2:870701ee67c4 | 132 | /** |
mcm | 2:870701ee67c4 | 133 | * @brief SHT3X_SetPeriodicMeasurementART ( void ) |
mcm | 2:870701ee67c4 | 134 | * @details It sets periodic measurement with ART (accelerated response time). |
mcm | 2:870701ee67c4 | 135 | * |
mcm | 2:870701ee67c4 | 136 | * @param[in] N/A. |
mcm | 2:870701ee67c4 | 137 | * |
mcm | 2:870701ee67c4 | 138 | * @param[out] N/A |
mcm | 2:870701ee67c4 | 139 | * |
mcm | 2:870701ee67c4 | 140 | * |
mcm | 2:870701ee67c4 | 141 | * @return Status of SHT3X_SetPeriodicMeasurementART. |
mcm | 2:870701ee67c4 | 142 | * |
mcm | 2:870701ee67c4 | 143 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 144 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 145 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 146 | * @pre N/A |
mcm | 2:870701ee67c4 | 147 | * @warning N/A. |
mcm | 2:870701ee67c4 | 148 | */ |
mcm | 2:870701ee67c4 | 149 | SHT3X::SHT3X_status_t SHT3X::SHT3X_SetPeriodicMeasurementART ( void ) |
mcm | 2:870701ee67c4 | 150 | { |
mcm | 2:870701ee67c4 | 151 | char cmd[2] = { 0U }; |
mcm | 2:870701ee67c4 | 152 | uint32_t aux; |
mcm | 2:870701ee67c4 | 153 | |
mcm | 2:870701ee67c4 | 154 | /* Write the register */ |
mcm | 2:870701ee67c4 | 155 | cmd[0] = (uint8_t)( SHT3X_PERIODIC_MESUREMENT_WITH_ART >> 8U ); |
mcm | 2:870701ee67c4 | 156 | cmd[1] = (uint8_t)( SHT3X_PERIODIC_MESUREMENT_WITH_ART & 0xFF ); |
mcm | 2:870701ee67c4 | 157 | aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); |
mcm | 2:870701ee67c4 | 158 | |
mcm | 2:870701ee67c4 | 159 | |
mcm | 2:870701ee67c4 | 160 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 161 | { |
mcm | 2:870701ee67c4 | 162 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 163 | } |
mcm | 2:870701ee67c4 | 164 | else |
mcm | 2:870701ee67c4 | 165 | { |
mcm | 2:870701ee67c4 | 166 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 167 | } |
mcm | 2:870701ee67c4 | 168 | } |
mcm | 2:870701ee67c4 | 169 | |
mcm | 2:870701ee67c4 | 170 | |
mcm | 2:870701ee67c4 | 171 | |
mcm | 2:870701ee67c4 | 172 | /** |
mcm | 2:870701ee67c4 | 173 | * @brief SHT3X_SetBreakCommand ( void ) |
mcm | 2:870701ee67c4 | 174 | * @details It sets the break command (stop periodic data acquisition mode). |
mcm | 2:870701ee67c4 | 175 | * |
mcm | 2:870701ee67c4 | 176 | * @param[in] N/A. |
mcm | 2:870701ee67c4 | 177 | * |
mcm | 2:870701ee67c4 | 178 | * @param[out] N/A |
mcm | 2:870701ee67c4 | 179 | * |
mcm | 2:870701ee67c4 | 180 | * |
mcm | 2:870701ee67c4 | 181 | * @return Status of SHT3X_SetBreakCommand. |
mcm | 2:870701ee67c4 | 182 | * |
mcm | 2:870701ee67c4 | 183 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 184 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 185 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 186 | * @pre N/A |
mcm | 2:870701ee67c4 | 187 | * @warning N/A. |
mcm | 2:870701ee67c4 | 188 | */ |
mcm | 2:870701ee67c4 | 189 | SHT3X::SHT3X_status_t SHT3X::SHT3X_SetBreakCommand ( void ) |
mcm | 2:870701ee67c4 | 190 | { |
mcm | 2:870701ee67c4 | 191 | char cmd[2] = { 0U }; |
mcm | 2:870701ee67c4 | 192 | uint32_t aux; |
mcm | 2:870701ee67c4 | 193 | |
mcm | 2:870701ee67c4 | 194 | /* Write the register */ |
mcm | 2:870701ee67c4 | 195 | cmd[0] = (uint8_t)( SHT3X_BREAK >> 8U ); |
mcm | 2:870701ee67c4 | 196 | cmd[1] = (uint8_t)( SHT3X_BREAK & 0xFF ); |
mcm | 2:870701ee67c4 | 197 | aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); |
mcm | 2:870701ee67c4 | 198 | |
mcm | 2:870701ee67c4 | 199 | |
mcm | 2:870701ee67c4 | 200 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 201 | { |
mcm | 2:870701ee67c4 | 202 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 203 | } |
mcm | 2:870701ee67c4 | 204 | else |
mcm | 2:870701ee67c4 | 205 | { |
mcm | 2:870701ee67c4 | 206 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 207 | } |
mcm | 2:870701ee67c4 | 208 | } |
mcm | 2:870701ee67c4 | 209 | |
mcm | 2:870701ee67c4 | 210 | |
mcm | 2:870701ee67c4 | 211 | |
mcm | 2:870701ee67c4 | 212 | /** |
mcm | 2:870701ee67c4 | 213 | * @brief SHT3X_SetSoftReset ( void ) |
mcm | 2:870701ee67c4 | 214 | * @details It performs a software reset. |
mcm | 2:870701ee67c4 | 215 | * |
mcm | 2:870701ee67c4 | 216 | * @param[in] N/A. |
mcm | 2:870701ee67c4 | 217 | * |
mcm | 2:870701ee67c4 | 218 | * @param[out] N/A |
mcm | 2:870701ee67c4 | 219 | * |
mcm | 2:870701ee67c4 | 220 | * |
mcm | 2:870701ee67c4 | 221 | * @return Status of SHT3X_SetSoftReset. |
mcm | 2:870701ee67c4 | 222 | * |
mcm | 2:870701ee67c4 | 223 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 224 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 225 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 226 | * @pre N/A |
mcm | 2:870701ee67c4 | 227 | * @warning N/A. |
mcm | 2:870701ee67c4 | 228 | */ |
mcm | 2:870701ee67c4 | 229 | SHT3X::SHT3X_status_t SHT3X::SHT3X_SetSoftReset ( void ) |
mcm | 2:870701ee67c4 | 230 | { |
mcm | 2:870701ee67c4 | 231 | char cmd[2] = { 0U }; |
mcm | 2:870701ee67c4 | 232 | uint32_t aux; |
mcm | 2:870701ee67c4 | 233 | |
mcm | 2:870701ee67c4 | 234 | /* Write the register */ |
mcm | 2:870701ee67c4 | 235 | cmd[0] = (uint8_t)( SHT3X_SOFT_RESET >> 8U ); |
mcm | 2:870701ee67c4 | 236 | cmd[1] = (uint8_t)( SHT3X_SOFT_RESET & 0xFF ); |
mcm | 2:870701ee67c4 | 237 | aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); |
mcm | 2:870701ee67c4 | 238 | |
mcm | 2:870701ee67c4 | 239 | |
mcm | 2:870701ee67c4 | 240 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 241 | { |
mcm | 2:870701ee67c4 | 242 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 243 | } |
mcm | 2:870701ee67c4 | 244 | else |
mcm | 2:870701ee67c4 | 245 | { |
mcm | 2:870701ee67c4 | 246 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 247 | } |
mcm | 2:870701ee67c4 | 248 | } |
mcm | 2:870701ee67c4 | 249 | |
mcm | 2:870701ee67c4 | 250 | |
mcm | 2:870701ee67c4 | 251 | |
mcm | 2:870701ee67c4 | 252 | /** |
mcm | 2:870701ee67c4 | 253 | * @brief SHT3X_SetGeneralCallReset ( void ) |
mcm | 2:870701ee67c4 | 254 | * @details It perfoms a reset through a general call address. |
mcm | 2:870701ee67c4 | 255 | * |
mcm | 2:870701ee67c4 | 256 | * @param[in] N/A. |
mcm | 2:870701ee67c4 | 257 | * |
mcm | 2:870701ee67c4 | 258 | * @param[out] N/A |
mcm | 2:870701ee67c4 | 259 | * |
mcm | 2:870701ee67c4 | 260 | * |
mcm | 2:870701ee67c4 | 261 | * @return Status of SHT3X_SetGeneralCallReset. |
mcm | 2:870701ee67c4 | 262 | * |
mcm | 2:870701ee67c4 | 263 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 264 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 265 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 266 | * @pre N/A |
mcm | 2:870701ee67c4 | 267 | * @warning N/A. |
mcm | 2:870701ee67c4 | 268 | */ |
mcm | 2:870701ee67c4 | 269 | SHT3X::SHT3X_status_t SHT3X::SHT3X_SetGeneralCallReset ( void ) |
mcm | 2:870701ee67c4 | 270 | { |
mcm | 2:870701ee67c4 | 271 | char cmd; |
mcm | 2:870701ee67c4 | 272 | uint32_t aux; |
mcm | 2:870701ee67c4 | 273 | |
mcm | 2:870701ee67c4 | 274 | /* Write the register */ |
mcm | 2:870701ee67c4 | 275 | cmd = SHT3X_GENERAL_CALL_RESET_SECOND_BYTE; |
mcm | 2:870701ee67c4 | 276 | aux = _i2c.write ( SHT3X_GENERAL_CALL_RESET_ADDRESS_BYTE, &cmd, 1U, false ); |
mcm | 2:870701ee67c4 | 277 | |
mcm | 2:870701ee67c4 | 278 | |
mcm | 2:870701ee67c4 | 279 | |
mcm | 2:870701ee67c4 | 280 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 281 | { |
mcm | 2:870701ee67c4 | 282 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 283 | } |
mcm | 2:870701ee67c4 | 284 | else |
mcm | 2:870701ee67c4 | 285 | { |
mcm | 2:870701ee67c4 | 286 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 287 | } |
mcm | 2:870701ee67c4 | 288 | } |
mcm | 2:870701ee67c4 | 289 | |
mcm | 2:870701ee67c4 | 290 | |
mcm | 2:870701ee67c4 | 291 | |
mcm | 2:870701ee67c4 | 292 | /** |
mcm | 2:870701ee67c4 | 293 | * @brief SHT3X_SetHeater ( SHT3X_command_registers_heater_t ) |
mcm | 2:870701ee67c4 | 294 | * @details It sets the heater. |
mcm | 2:870701ee67c4 | 295 | * |
mcm | 2:870701ee67c4 | 296 | * @param[in] heater: Enable/Disable. |
mcm | 2:870701ee67c4 | 297 | * |
mcm | 2:870701ee67c4 | 298 | * @param[out] N/A |
mcm | 2:870701ee67c4 | 299 | * |
mcm | 2:870701ee67c4 | 300 | * |
mcm | 2:870701ee67c4 | 301 | * @return Status of SHT3X_SetHeater. |
mcm | 2:870701ee67c4 | 302 | * |
mcm | 2:870701ee67c4 | 303 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 304 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 305 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 306 | * @pre N/A |
mcm | 2:870701ee67c4 | 307 | * @warning N/A. |
mcm | 2:870701ee67c4 | 308 | */ |
mcm | 2:870701ee67c4 | 309 | SHT3X::SHT3X_status_t SHT3X::SHT3X_SetHeater ( SHT3X_command_registers_heater_t heater ) |
mcm | 2:870701ee67c4 | 310 | { |
mcm | 2:870701ee67c4 | 311 | char cmd[2] = { 0U }; |
mcm | 2:870701ee67c4 | 312 | uint32_t aux; |
mcm | 2:870701ee67c4 | 313 | |
mcm | 2:870701ee67c4 | 314 | /* Write the register */ |
mcm | 2:870701ee67c4 | 315 | cmd[0] = (uint8_t)( heater >> 8U ); |
mcm | 2:870701ee67c4 | 316 | cmd[1] = (uint8_t)( heater & 0xFF ); |
mcm | 2:870701ee67c4 | 317 | aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); |
mcm | 2:870701ee67c4 | 318 | |
mcm | 2:870701ee67c4 | 319 | |
mcm | 2:870701ee67c4 | 320 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 321 | { |
mcm | 2:870701ee67c4 | 322 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 323 | } |
mcm | 2:870701ee67c4 | 324 | else |
mcm | 2:870701ee67c4 | 325 | { |
mcm | 2:870701ee67c4 | 326 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 327 | } |
mcm | 2:870701ee67c4 | 328 | } |
mcm | 2:870701ee67c4 | 329 | |
mcm | 2:870701ee67c4 | 330 | |
mcm | 2:870701ee67c4 | 331 | |
mcm | 2:870701ee67c4 | 332 | /** |
mcm | 2:870701ee67c4 | 333 | * @brief SHT3X_GetStatus ( SHT3X_status_data_t* ) |
mcm | 2:870701ee67c4 | 334 | * @details It gets the status register. |
mcm | 2:870701ee67c4 | 335 | * |
mcm | 2:870701ee67c4 | 336 | * @param[in] N/A. |
mcm | 2:870701ee67c4 | 337 | * |
mcm | 2:870701ee67c4 | 338 | * @param[out] status: The wole status register and its CRC. |
mcm | 2:870701ee67c4 | 339 | * |
mcm | 2:870701ee67c4 | 340 | * |
mcm | 2:870701ee67c4 | 341 | * @return Status of SHT3X_GetStatus. |
mcm | 2:870701ee67c4 | 342 | * |
mcm | 2:870701ee67c4 | 343 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 344 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 345 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 346 | * @pre N/A |
mcm | 2:870701ee67c4 | 347 | * @warning N/A. |
mcm | 2:870701ee67c4 | 348 | */ |
mcm | 2:870701ee67c4 | 349 | SHT3X::SHT3X_status_t SHT3X::SHT3X_GetStatus ( SHT3X_status_data_t* status ) |
mcm | 2:870701ee67c4 | 350 | { |
mcm | 2:870701ee67c4 | 351 | char cmd[3] = { 0U }; |
mcm | 2:870701ee67c4 | 352 | uint32_t aux = SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 353 | |
mcm | 2:870701ee67c4 | 354 | /* Write the register */ |
mcm | 2:870701ee67c4 | 355 | cmd[0] = (uint8_t)( SHT3X_STATUS_REGISTER >> 8U ); |
mcm | 2:870701ee67c4 | 356 | cmd[1] = (uint8_t)( SHT3X_STATUS_REGISTER & 0xFF ); |
mcm | 2:870701ee67c4 | 357 | aux |= _i2c.write ( _SHT3X_Addr, &cmd[0], 2U, true ); |
mcm | 2:870701ee67c4 | 358 | |
mcm | 2:870701ee67c4 | 359 | /* Read the register */ |
mcm | 2:870701ee67c4 | 360 | aux |= _i2c.read ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); |
mcm | 2:870701ee67c4 | 361 | |
mcm | 2:870701ee67c4 | 362 | /* Parse the data */ |
mcm | 2:870701ee67c4 | 363 | status->status = cmd[0]; |
mcm | 2:870701ee67c4 | 364 | status->status <<= 8U; |
mcm | 2:870701ee67c4 | 365 | status->status |= cmd[1]; |
mcm | 2:870701ee67c4 | 366 | |
mcm | 2:870701ee67c4 | 367 | status->statusCRC = cmd[2]; |
mcm | 2:870701ee67c4 | 368 | |
mcm | 2:870701ee67c4 | 369 | |
mcm | 2:870701ee67c4 | 370 | |
mcm | 2:870701ee67c4 | 371 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 372 | { |
mcm | 2:870701ee67c4 | 373 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 374 | } |
mcm | 2:870701ee67c4 | 375 | else |
mcm | 2:870701ee67c4 | 376 | { |
mcm | 2:870701ee67c4 | 377 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 378 | } |
mcm | 2:870701ee67c4 | 379 | } |
mcm | 2:870701ee67c4 | 380 | |
mcm | 2:870701ee67c4 | 381 | |
mcm | 2:870701ee67c4 | 382 | |
mcm | 2:870701ee67c4 | 383 | /** |
mcm | 2:870701ee67c4 | 384 | * @brief SHT3X_ClearStatus ( void ) |
mcm | 2:870701ee67c4 | 385 | * @details It clears the status register. |
mcm | 2:870701ee67c4 | 386 | * |
mcm | 2:870701ee67c4 | 387 | * @param[in] N/A. |
mcm | 2:870701ee67c4 | 388 | * |
mcm | 2:870701ee67c4 | 389 | * @param[out] N/A. |
mcm | 2:870701ee67c4 | 390 | * |
mcm | 2:870701ee67c4 | 391 | * |
mcm | 2:870701ee67c4 | 392 | * @return Status of SHT3X_ClearStatus. |
mcm | 2:870701ee67c4 | 393 | * |
mcm | 2:870701ee67c4 | 394 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 395 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 396 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 397 | * @pre N/A |
mcm | 2:870701ee67c4 | 398 | * @warning N/A. |
mcm | 2:870701ee67c4 | 399 | */ |
mcm | 2:870701ee67c4 | 400 | SHT3X::SHT3X_status_t SHT3X::SHT3X_ClearStatus ( void ) |
mcm | 2:870701ee67c4 | 401 | { |
mcm | 2:870701ee67c4 | 402 | char cmd[2] = { 0U }; |
mcm | 2:870701ee67c4 | 403 | uint32_t aux; |
mcm | 2:870701ee67c4 | 404 | |
mcm | 2:870701ee67c4 | 405 | /* Write the register */ |
mcm | 2:870701ee67c4 | 406 | cmd[0] = (uint8_t)( SHT3X_CLEAR_STATUS_REGISTER >> 8U ); |
mcm | 2:870701ee67c4 | 407 | cmd[1] = (uint8_t)( SHT3X_CLEAR_STATUS_REGISTER & 0xFF ); |
mcm | 2:870701ee67c4 | 408 | aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); |
mcm | 2:870701ee67c4 | 409 | |
mcm | 2:870701ee67c4 | 410 | |
mcm | 2:870701ee67c4 | 411 | |
mcm | 2:870701ee67c4 | 412 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 413 | { |
mcm | 2:870701ee67c4 | 414 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 415 | } |
mcm | 2:870701ee67c4 | 416 | else |
mcm | 2:870701ee67c4 | 417 | { |
mcm | 2:870701ee67c4 | 418 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 419 | } |
mcm | 2:870701ee67c4 | 420 | } |
mcm | 2:870701ee67c4 | 421 | |
mcm | 2:870701ee67c4 | 422 | |
mcm | 2:870701ee67c4 | 423 | |
mcm | 2:870701ee67c4 | 424 | /** |
mcm | 2:870701ee67c4 | 425 | * @brief SHT3X_SetPeriodicAquisitionMode ( SHT3X_command_registers_periodic_data_mode_t ) |
mcm | 2:870701ee67c4 | 426 | * @details It sets the periodic data aquisition mode. |
mcm | 2:870701ee67c4 | 427 | * |
mcm | 2:870701ee67c4 | 428 | * @param[in] mo: Periodic aquisition mode. |
mcm | 2:870701ee67c4 | 429 | * |
mcm | 2:870701ee67c4 | 430 | * @param[out] N/A. |
mcm | 2:870701ee67c4 | 431 | * |
mcm | 2:870701ee67c4 | 432 | * |
mcm | 2:870701ee67c4 | 433 | * @return Status of SHT3X_SetPeriodicAquisitionMode. |
mcm | 2:870701ee67c4 | 434 | * |
mcm | 2:870701ee67c4 | 435 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 436 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 437 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 438 | * @pre N/A |
mcm | 2:870701ee67c4 | 439 | * @warning N/A. |
mcm | 2:870701ee67c4 | 440 | */ |
mcm | 2:870701ee67c4 | 441 | SHT3X::SHT3X_status_t SHT3X::SHT3X_SetPeriodicAquisitionMode ( SHT3X_command_registers_periodic_data_mode_t mo ) |
mcm | 2:870701ee67c4 | 442 | { |
mcm | 2:870701ee67c4 | 443 | char cmd[2] = { 0U }; |
mcm | 2:870701ee67c4 | 444 | uint32_t aux; |
mcm | 2:870701ee67c4 | 445 | |
mcm | 2:870701ee67c4 | 446 | /* Write the register */ |
mcm | 2:870701ee67c4 | 447 | cmd[0] = (uint8_t)( mo >> 8U ); |
mcm | 2:870701ee67c4 | 448 | cmd[1] = (uint8_t)( mo & 0xFF ); |
mcm | 2:870701ee67c4 | 449 | aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); |
mcm | 2:870701ee67c4 | 450 | |
mcm | 2:870701ee67c4 | 451 | |
mcm | 2:870701ee67c4 | 452 | |
mcm | 2:870701ee67c4 | 453 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 454 | { |
mcm | 2:870701ee67c4 | 455 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 456 | } |
mcm | 2:870701ee67c4 | 457 | else |
mcm | 2:870701ee67c4 | 458 | { |
mcm | 2:870701ee67c4 | 459 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 460 | } |
mcm | 2:870701ee67c4 | 461 | } |
mcm | 2:870701ee67c4 | 462 | |
mcm | 2:870701ee67c4 | 463 | |
mcm | 2:870701ee67c4 | 464 | |
mcm | 2:870701ee67c4 | 465 | /** |
mcm | 2:870701ee67c4 | 466 | * @brief SHT3X_GetAllRawDataFetchData ( SHT3X_raw_data_t* ) |
mcm | 2:870701ee67c4 | 467 | * @details It gets the all raw data (in periodic aquisition mode). |
mcm | 2:870701ee67c4 | 468 | * |
mcm | 2:870701ee67c4 | 469 | * @param[in] N/A. |
mcm | 2:870701ee67c4 | 470 | * |
mcm | 2:870701ee67c4 | 471 | * @param[out] rawData: Raw data and their CRC values. |
mcm | 2:870701ee67c4 | 472 | * |
mcm | 2:870701ee67c4 | 473 | * |
mcm | 2:870701ee67c4 | 474 | * @return Status of SHT3X_GetAllRawDataFetchData. |
mcm | 2:870701ee67c4 | 475 | * |
mcm | 2:870701ee67c4 | 476 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 477 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 478 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 479 | * @pre N/A |
mcm | 2:870701ee67c4 | 480 | * @warning N/A. |
mcm | 2:870701ee67c4 | 481 | */ |
mcm | 2:870701ee67c4 | 482 | SHT3X::SHT3X_status_t SHT3X::SHT3X_GetAllRawDataFetchData ( SHT3X_raw_data_t* rawData ) |
mcm | 2:870701ee67c4 | 483 | { |
mcm | 2:870701ee67c4 | 484 | char cmd[6] = { 0U }; |
mcm | 2:870701ee67c4 | 485 | uint32_t aux; |
mcm | 2:870701ee67c4 | 486 | |
mcm | 2:870701ee67c4 | 487 | /* Write the register */ |
mcm | 2:870701ee67c4 | 488 | cmd[0] = (uint8_t)( SHT3X_FETCH_DATA >> 8U ); |
mcm | 2:870701ee67c4 | 489 | cmd[1] = (uint8_t)( SHT3X_FETCH_DATA & 0xFF ); |
mcm | 2:870701ee67c4 | 490 | aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), true ); |
mcm | 2:870701ee67c4 | 491 | |
mcm | 2:870701ee67c4 | 492 | /* Read the register */ |
mcm | 2:870701ee67c4 | 493 | aux = _i2c.read ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); |
mcm | 2:870701ee67c4 | 494 | |
mcm | 2:870701ee67c4 | 495 | /* Parse the data */ |
mcm | 2:870701ee67c4 | 496 | rawData->rawTemperature = cmd[0]; |
mcm | 2:870701ee67c4 | 497 | rawData->rawTemperature <<= 8U; |
mcm | 2:870701ee67c4 | 498 | rawData->rawTemperature |= cmd[1]; |
mcm | 2:870701ee67c4 | 499 | |
mcm | 2:870701ee67c4 | 500 | rawData->temperatureCRC = cmd[2]; |
mcm | 2:870701ee67c4 | 501 | |
mcm | 2:870701ee67c4 | 502 | rawData->rawRelativeHumidity = cmd[3]; |
mcm | 2:870701ee67c4 | 503 | rawData->rawRelativeHumidity <<= 8U; |
mcm | 2:870701ee67c4 | 504 | rawData->rawRelativeHumidity |= cmd[4]; |
mcm | 2:870701ee67c4 | 505 | |
mcm | 2:870701ee67c4 | 506 | rawData->relativeHumidityCRC = cmd[5]; |
mcm | 2:870701ee67c4 | 507 | |
mcm | 2:870701ee67c4 | 508 | |
mcm | 2:870701ee67c4 | 509 | |
mcm | 2:870701ee67c4 | 510 | if ( aux == I2C_SUCCESS ) |
mcm | 2:870701ee67c4 | 511 | { |
mcm | 2:870701ee67c4 | 512 | return SHT3X_SUCCESS; |
mcm | 2:870701ee67c4 | 513 | } |
mcm | 2:870701ee67c4 | 514 | else |
mcm | 2:870701ee67c4 | 515 | { |
mcm | 2:870701ee67c4 | 516 | return SHT3X_FAILURE; |
mcm | 2:870701ee67c4 | 517 | } |
mcm | 2:870701ee67c4 | 518 | } |
mcm | 2:870701ee67c4 | 519 | |
mcm | 2:870701ee67c4 | 520 | |
mcm | 2:870701ee67c4 | 521 | |
mcm | 2:870701ee67c4 | 522 | /** |
mcm | 2:870701ee67c4 | 523 | * @brief SHT3X_ProccessData ( SHT3X_raw_data_t , SHT3X_final_data_t* ) |
mcm | 2:870701ee67c4 | 524 | * @details It clears the status register. |
mcm | 2:870701ee67c4 | 525 | * |
mcm | 2:870701ee67c4 | 526 | * @param[in] rawData: Data to be processed. |
mcm | 2:870701ee67c4 | 527 | * |
mcm | 2:870701ee67c4 | 528 | * @param[out] data: Data result. |
mcm | 2:870701ee67c4 | 529 | * |
mcm | 2:870701ee67c4 | 530 | * |
mcm | 2:870701ee67c4 | 531 | * @return N/A. |
mcm | 2:870701ee67c4 | 532 | * |
mcm | 2:870701ee67c4 | 533 | * @author Manuel Caballero |
mcm | 2:870701ee67c4 | 534 | * @date 25/March/2021 |
mcm | 2:870701ee67c4 | 535 | * @version 25/March/2021 The ORIGIN |
mcm | 2:870701ee67c4 | 536 | * @pre N/A |
mcm | 2:870701ee67c4 | 537 | * @warning N/A. |
mcm | 2:870701ee67c4 | 538 | */ |
mcm | 2:870701ee67c4 | 539 | void SHT3X::SHT3X_ProccessData ( SHT3X_raw_data_t rawData, SHT3X_final_data_t* data ) |
mcm | 2:870701ee67c4 | 540 | { |
mcm | 2:870701ee67c4 | 541 | /* Process the temperature value */ |
mcm | 2:870701ee67c4 | 542 | data->temperature = ( -45.0 + ( 175.0 * ( rawData.rawTemperature / ( 65536.0 - 1.0 ) ) ) ); |
mcm | 2:870701ee67c4 | 543 | |
mcm | 2:870701ee67c4 | 544 | /* Process the relative humidity value */ |
mcm | 2:870701ee67c4 | 545 | data->relativeHumidity = 100.0 * ( rawData.rawRelativeHumidity / ( 65536.0 - 1.0 ) ); |
mcm | 2:870701ee67c4 | 546 | } |