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.
SHT3X.cpp
00001 /** 00002 * @brief SHT3X.c 00003 * @details Humidity and Temperature Sensor. 00004 * Functions file. 00005 * 00006 * 00007 * @return N/A 00008 * 00009 * @author Manuel Caballero 00010 * @date 25/March/2021 00011 * @version 25/March/2021 The ORIGIN 00012 * @pre N/A 00013 * @warning N/A 00014 * @pre This code belongs to Nimbus Centre ( http://www.nimbus.cit.ie ). 00015 */ 00016 00017 #include "SHT3X.h" 00018 00019 00020 SHT3X::SHT3X ( PinName sda, PinName scl, uint32_t addr, uint32_t freq ) 00021 : _i2c ( sda, scl ) 00022 , _SHT3X_Addr ( addr ) 00023 { 00024 _i2c.frequency( freq ); 00025 } 00026 00027 00028 SHT3X::~SHT3X() 00029 { 00030 } 00031 00032 00033 00034 00035 /** 00036 * @brief SHT3X_OneShotTriggerAllData ( SHT3X_command_registers_single_shot_mode_t ) 00037 * @details It triggers all the raw data in single shot mode. 00038 * 00039 * @param[in] mode: Clock Stretching/No Stretching and the repeatability. 00040 * 00041 * @param[out] N/A 00042 * 00043 * 00044 * @return Status of SHT3X_OneShotTriggerAllData. 00045 * 00046 * @author Manuel Caballero 00047 * @date 25/March/2021 00048 * @version 25/March/2021 The ORIGIN 00049 * @pre N/A 00050 * @warning N/A. 00051 */ 00052 SHT3X::SHT3X_status_t SHT3X::SHT3X_OneShotTriggerAllData ( SHT3X_command_registers_single_shot_mode_t mode ) 00053 { 00054 char cmd[2] = { 0U }; 00055 uint32_t aux; 00056 00057 /* Write the register */ 00058 cmd[0] = ( mode >> 8U ); 00059 cmd[1] = ( mode & 0xFF ); 00060 aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); 00061 00062 00063 if ( aux == I2C_SUCCESS ) 00064 { 00065 return SHT3X_SUCCESS; 00066 } 00067 else 00068 { 00069 return SHT3X_FAILURE; 00070 } 00071 } 00072 00073 00074 00075 /** 00076 * @brief SHT3X_OneShotGetAllRawData ( SHT3X_raw_data_t* ) 00077 * @details It gets all the raw data in single shot mode. 00078 * 00079 * @param[in] N/A. 00080 * 00081 * @param[out] rawData: All the raw data and their CRC values. 00082 * 00083 * 00084 * @return Status of SHT3X_OneShotGetAllRawData. 00085 * 00086 * @author Manuel Caballero 00087 * @date 25/March/2021 00088 * @version 25/March/2021 The ORIGIN 00089 * @pre N/A 00090 * @warning SHT3X_OneShotTriggerAllData function must be called first. The user MUST respect the 00091 * measurement duration as indicated in the datasheet (Table4.System timing specification, p7): 00092 * 00093 * Typ. Max 00094 * Low repeatability 2.5ms 4ms 00095 * Medium repeatability 4.5ms 6ms 00096 * High repeatability 12.5ms 15ms. 00097 */ 00098 SHT3X::SHT3X_status_t SHT3X::SHT3X_OneShotGetAllRawData ( SHT3X_raw_data_t* rawData ) 00099 { 00100 char cmd[6] = { 0U }; 00101 uint32_t aux; 00102 00103 /* Read the register */ 00104 aux = _i2c.read ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); 00105 00106 /* Parse the data */ 00107 rawData->rawTemperature = cmd[0]; 00108 rawData->rawTemperature <<= 8U; 00109 rawData->rawTemperature |= cmd[1]; 00110 00111 rawData->temperatureCRC = cmd[2]; 00112 00113 rawData->rawRelativeHumidity = cmd[3]; 00114 rawData->rawRelativeHumidity <<= 8U; 00115 rawData->rawRelativeHumidity |= cmd[4]; 00116 00117 rawData->relativeHumidityCRC = cmd[5]; 00118 00119 00120 if ( aux == I2C_SUCCESS ) 00121 { 00122 return SHT3X_SUCCESS; 00123 } 00124 else 00125 { 00126 return SHT3X_FAILURE; 00127 } 00128 } 00129 00130 00131 00132 /** 00133 * @brief SHT3X_SetPeriodicMeasurementART ( void ) 00134 * @details It sets periodic measurement with ART (accelerated response time). 00135 * 00136 * @param[in] N/A. 00137 * 00138 * @param[out] N/A 00139 * 00140 * 00141 * @return Status of SHT3X_SetPeriodicMeasurementART. 00142 * 00143 * @author Manuel Caballero 00144 * @date 25/March/2021 00145 * @version 25/March/2021 The ORIGIN 00146 * @pre N/A 00147 * @warning N/A. 00148 */ 00149 SHT3X::SHT3X_status_t SHT3X::SHT3X_SetPeriodicMeasurementART ( void ) 00150 { 00151 char cmd[2] = { 0U }; 00152 uint32_t aux; 00153 00154 /* Write the register */ 00155 cmd[0] = ( SHT3X_PERIODIC_MESUREMENT_WITH_ART >> 8U ); 00156 cmd[1] = ( SHT3X_PERIODIC_MESUREMENT_WITH_ART & 0xFF ); 00157 aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); 00158 00159 00160 if ( aux == I2C_SUCCESS ) 00161 { 00162 return SHT3X_SUCCESS; 00163 } 00164 else 00165 { 00166 return SHT3X_FAILURE; 00167 } 00168 } 00169 00170 00171 00172 /** 00173 * @brief SHT3X_SetBreakCommand ( void ) 00174 * @details It sets the break command (stop periodic data acquisition mode). 00175 * 00176 * @param[in] N/A. 00177 * 00178 * @param[out] N/A 00179 * 00180 * 00181 * @return Status of SHT3X_SetBreakCommand. 00182 * 00183 * @author Manuel Caballero 00184 * @date 25/March/2021 00185 * @version 25/March/2021 The ORIGIN 00186 * @pre N/A 00187 * @warning N/A. 00188 */ 00189 SHT3X::SHT3X_status_t SHT3X::SHT3X_SetBreakCommand ( void ) 00190 { 00191 char cmd[2] = { 0U }; 00192 uint32_t aux; 00193 00194 /* Write the register */ 00195 cmd[0] = ( SHT3X_BREAK >> 8U ); 00196 cmd[1] = ( SHT3X_BREAK & 0xFF ); 00197 aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); 00198 00199 00200 if ( aux == I2C_SUCCESS ) 00201 { 00202 return SHT3X_SUCCESS; 00203 } 00204 else 00205 { 00206 return SHT3X_FAILURE; 00207 } 00208 } 00209 00210 00211 00212 /** 00213 * @brief SHT3X_SetSoftReset ( void ) 00214 * @details It performs a software reset. 00215 * 00216 * @param[in] N/A. 00217 * 00218 * @param[out] N/A 00219 * 00220 * 00221 * @return Status of SHT3X_SetSoftReset. 00222 * 00223 * @author Manuel Caballero 00224 * @date 25/March/2021 00225 * @version 25/March/2021 The ORIGIN 00226 * @pre N/A 00227 * @warning N/A. 00228 */ 00229 SHT3X::SHT3X_status_t SHT3X::SHT3X_SetSoftReset ( void ) 00230 { 00231 char cmd[2] = { 0U }; 00232 uint32_t aux; 00233 00234 /* Write the register */ 00235 cmd[0] = ( SHT3X_SOFT_RESET >> 8U ); 00236 cmd[1] = ( SHT3X_SOFT_RESET & 0xFF ); 00237 aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); 00238 00239 00240 if ( aux == I2C_SUCCESS ) 00241 { 00242 return SHT3X_SUCCESS; 00243 } 00244 else 00245 { 00246 return SHT3X_FAILURE; 00247 } 00248 } 00249 00250 00251 00252 /** 00253 * @brief SHT3X_SetGeneralCallReset ( void ) 00254 * @details It perfoms a reset through a general call address. 00255 * 00256 * @param[in] N/A. 00257 * 00258 * @param[out] N/A 00259 * 00260 * 00261 * @return Status of SHT3X_SetGeneralCallReset. 00262 * 00263 * @author Manuel Caballero 00264 * @date 25/March/2021 00265 * @version 25/March/2021 The ORIGIN 00266 * @pre N/A 00267 * @warning N/A. 00268 */ 00269 SHT3X::SHT3X_status_t SHT3X::SHT3X_SetGeneralCallReset ( void ) 00270 { 00271 char cmd; 00272 uint32_t aux; 00273 00274 /* Write the register */ 00275 cmd = SHT3X_GENERAL_CALL_RESET_SECOND_BYTE ; 00276 aux = _i2c.write ( SHT3X_GENERAL_CALL_RESET_ADDRESS_BYTE , &cmd, 1U, false ); 00277 00278 00279 00280 if ( aux == I2C_SUCCESS ) 00281 { 00282 return SHT3X_SUCCESS; 00283 } 00284 else 00285 { 00286 return SHT3X_FAILURE; 00287 } 00288 } 00289 00290 00291 00292 /** 00293 * @brief SHT3X_SetHeater ( SHT3X_command_registers_heater_t ) 00294 * @details It sets the heater. 00295 * 00296 * @param[in] heater: Enable/Disable. 00297 * 00298 * @param[out] N/A 00299 * 00300 * 00301 * @return Status of SHT3X_SetHeater. 00302 * 00303 * @author Manuel Caballero 00304 * @date 25/March/2021 00305 * @version 25/March/2021 The ORIGIN 00306 * @pre N/A 00307 * @warning N/A. 00308 */ 00309 SHT3X::SHT3X_status_t SHT3X::SHT3X_SetHeater ( SHT3X_command_registers_heater_t heater ) 00310 { 00311 char cmd[2] = { 0U }; 00312 uint32_t aux; 00313 00314 /* Write the register */ 00315 cmd[0] = ( heater >> 8U ); 00316 cmd[1] = ( heater & 0xFF ); 00317 aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); 00318 00319 00320 if ( aux == I2C_SUCCESS ) 00321 { 00322 return SHT3X_SUCCESS; 00323 } 00324 else 00325 { 00326 return SHT3X_FAILURE; 00327 } 00328 } 00329 00330 00331 00332 /** 00333 * @brief SHT3X_GetStatus ( SHT3X_status_data_t* ) 00334 * @details It gets the status register. 00335 * 00336 * @param[in] N/A. 00337 * 00338 * @param[out] status: The wole status register and its CRC. 00339 * 00340 * 00341 * @return Status of SHT3X_GetStatus. 00342 * 00343 * @author Manuel Caballero 00344 * @date 25/March/2021 00345 * @version 25/March/2021 The ORIGIN 00346 * @pre N/A 00347 * @warning N/A. 00348 */ 00349 SHT3X::SHT3X_status_t SHT3X::SHT3X_GetStatus ( SHT3X_status_data_t* status ) 00350 { 00351 char cmd[3] = { 0U }; 00352 uint32_t aux = SHT3X_SUCCESS; 00353 00354 /* Write the register */ 00355 cmd[0] = ( SHT3X_STATUS_REGISTER >> 8U ); 00356 cmd[1] = ( SHT3X_STATUS_REGISTER & 0xFF ); 00357 aux |= _i2c.write ( _SHT3X_Addr, &cmd[0], 2U, true ); 00358 00359 /* Read the register */ 00360 aux |= _i2c.read ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); 00361 00362 /* Parse the data */ 00363 status->status = cmd[0]; 00364 status->status <<= 8U; 00365 status->status |= cmd[1]; 00366 00367 status->statusCRC = cmd[2]; 00368 00369 00370 00371 if ( aux == I2C_SUCCESS ) 00372 { 00373 return SHT3X_SUCCESS; 00374 } 00375 else 00376 { 00377 return SHT3X_FAILURE; 00378 } 00379 } 00380 00381 00382 00383 /** 00384 * @brief SHT3X_ClearStatus ( void ) 00385 * @details It clears the status register. 00386 * 00387 * @param[in] N/A. 00388 * 00389 * @param[out] N/A. 00390 * 00391 * 00392 * @return Status of SHT3X_ClearStatus. 00393 * 00394 * @author Manuel Caballero 00395 * @date 25/March/2021 00396 * @version 25/March/2021 The ORIGIN 00397 * @pre N/A 00398 * @warning N/A. 00399 */ 00400 SHT3X::SHT3X_status_t SHT3X::SHT3X_ClearStatus ( void ) 00401 { 00402 char cmd[2] = { 0U }; 00403 uint32_t aux; 00404 00405 /* Write the register */ 00406 cmd[0] = ( SHT3X_CLEAR_STATUS_REGISTER >> 8U ); 00407 cmd[1] = ( SHT3X_CLEAR_STATUS_REGISTER & 0xFF ); 00408 aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); 00409 00410 00411 00412 if ( aux == I2C_SUCCESS ) 00413 { 00414 return SHT3X_SUCCESS; 00415 } 00416 else 00417 { 00418 return SHT3X_FAILURE; 00419 } 00420 } 00421 00422 00423 00424 /** 00425 * @brief SHT3X_SetPeriodicAquisitionMode ( SHT3X_command_registers_periodic_data_mode_t ) 00426 * @details It sets the periodic data aquisition mode. 00427 * 00428 * @param[in] mo: Periodic aquisition mode. 00429 * 00430 * @param[out] N/A. 00431 * 00432 * 00433 * @return Status of SHT3X_SetPeriodicAquisitionMode. 00434 * 00435 * @author Manuel Caballero 00436 * @date 25/March/2021 00437 * @version 25/March/2021 The ORIGIN 00438 * @pre N/A 00439 * @warning N/A. 00440 */ 00441 SHT3X::SHT3X_status_t SHT3X::SHT3X_SetPeriodicAquisitionMode ( SHT3X_command_registers_periodic_data_mode_t mo ) 00442 { 00443 char cmd[2] = { 0U }; 00444 uint32_t aux; 00445 00446 /* Write the register */ 00447 cmd[0] = ( mo >> 8U ); 00448 cmd[1] = ( mo & 0xFF ); 00449 aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); 00450 00451 00452 00453 if ( aux == I2C_SUCCESS ) 00454 { 00455 return SHT3X_SUCCESS; 00456 } 00457 else 00458 { 00459 return SHT3X_FAILURE; 00460 } 00461 } 00462 00463 00464 00465 /** 00466 * @brief SHT3X_GetAllRawDataFetchData ( SHT3X_raw_data_t* ) 00467 * @details It gets the all raw data (in periodic aquisition mode). 00468 * 00469 * @param[in] N/A. 00470 * 00471 * @param[out] rawData: Raw data and their CRC values. 00472 * 00473 * 00474 * @return Status of SHT3X_GetAllRawDataFetchData. 00475 * 00476 * @author Manuel Caballero 00477 * @date 25/March/2021 00478 * @version 25/March/2021 The ORIGIN 00479 * @pre N/A 00480 * @warning N/A. 00481 */ 00482 SHT3X::SHT3X_status_t SHT3X::SHT3X_GetAllRawDataFetchData ( SHT3X_raw_data_t* rawData ) 00483 { 00484 char cmd[6] = { 0U }; 00485 uint32_t aux; 00486 00487 /* Write the register */ 00488 cmd[0] = ( SHT3X_FETCH_DATA >> 8U ); 00489 cmd[1] = ( SHT3X_FETCH_DATA & 0xFF ); 00490 aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), true ); 00491 00492 /* Read the register */ 00493 aux = _i2c.read ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) ); 00494 00495 /* Parse the data */ 00496 rawData->rawTemperature = cmd[0]; 00497 rawData->rawTemperature <<= 8U; 00498 rawData->rawTemperature |= cmd[1]; 00499 00500 rawData->temperatureCRC = cmd[2]; 00501 00502 rawData->rawRelativeHumidity = cmd[3]; 00503 rawData->rawRelativeHumidity <<= 8U; 00504 rawData->rawRelativeHumidity |= cmd[4]; 00505 00506 rawData->relativeHumidityCRC = cmd[5]; 00507 00508 00509 00510 if ( aux == I2C_SUCCESS ) 00511 { 00512 return SHT3X_SUCCESS; 00513 } 00514 else 00515 { 00516 return SHT3X_FAILURE; 00517 } 00518 } 00519 00520 00521 00522 /** 00523 * @brief SHT3X_ProccessData ( SHT3X_raw_data_t , SHT3X_final_data_t* ) 00524 * @details It clears the status register. 00525 * 00526 * @param[in] rawData: Data to be processed. 00527 * 00528 * @param[out] data: Data result. 00529 * 00530 * 00531 * @return N/A. 00532 * 00533 * @author Manuel Caballero 00534 * @date 25/March/2021 00535 * @version 25/March/2021 The ORIGIN 00536 * @pre N/A 00537 * @warning N/A. 00538 */ 00539 void SHT3X::SHT3X_ProccessData ( SHT3X_raw_data_t rawData, SHT3X_final_data_t* data ) 00540 { 00541 /* Process the temperature value */ 00542 data->temperature = ( -45.0 + ( 175.0 * ( rawData.rawTemperature / ( 65536.0 - 1.0 ) ) ) ); 00543 00544 /* Process the relative humidity value */ 00545 data->relativeHumidity = 100.0 * ( rawData.rawRelativeHumidity / ( 65536.0 - 1.0 ) ); 00546 }
Generated on Wed Jul 13 2022 08:49:41 by
1.7.2