Manuel Caballero / HTU21D
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTU21D.cpp Source File

HTU21D.cpp

00001 /**
00002  * @brief       HTU21D.c
00003  * @details     Digital humidity sensor with temperature output.
00004  *              Functions file.
00005  *
00006  *
00007  * @return      NA
00008  *
00009  * @author      Manuel Caballero
00010  * @date        4/September/2017
00011  * @version     4/September/2017    The ORIGIN
00012  * @pre         NaN.
00013  * @warning     NaN
00014  * @pre         This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ).
00015  */
00016 
00017 #include "HTU21D.h"
00018 
00019 
00020 HTU21D::HTU21D ( PinName sda, PinName scl, uint32_t addr, uint32_t freq )
00021     : i2c          ( sda, scl )
00022     , HTU21D_Addr  ( addr )
00023 {
00024     i2c.frequency( freq );
00025 }
00026 
00027 
00028 HTU21D::~HTU21D()
00029 {
00030 }
00031 
00032 
00033 
00034 /**
00035  * @brief       HTU21D_Init   ( HTU21D_master_mode_t , HTU21D_user_register_resolution_t , HTU21D_user_register_otp_t )
00036  *
00037  * @details     It initializes the device.
00038  *
00039  * @param[in]    myMode:        Hold/No Hold master mode.
00040  * @param[in]    myResolution:  Resolution of the sensor.
00041  * @param[in]    myHeater:      Heater enabled or disabled.
00042  *
00043  * @param[out]   NaN.
00044  *
00045  *
00046  * @return       Status of HTU21D_Init.
00047  *
00048  *
00049  * @author      Manuel Caballero
00050  * @date        4/September/2017
00051  * @version     4/September/2017   The ORIGIN
00052  * @pre         NaN
00053  * @warning     NaN.
00054  */
00055 HTU21D::HTU21D_status_t  HTU21D::HTU21D_Init    ( HTU21D_master_mode_t myMode, HTU21D_user_register_resolution_t myResolution, HTU21D_user_register_heater_t myHeater )
00056 {
00057     char        cmd[]             =   { HTU21D_READ_REGISTER, 0 };
00058     uint32_t    aux               =    0;
00059 
00060 
00061     HTU21D_Mode         =   myMode;
00062     HTU21D_Resolution   =   myResolution;
00063 
00064 
00065     // Reserved bits must not be changed. Therefore, for any writing to user register, default values of reserved bits must be read first
00066     // Datasheet: User register p.13.
00067     aux = i2c.write ( HTU21D_Addr, &cmd[0], 1 );
00068     aux = i2c.read  ( HTU21D_Addr, &cmd[0], 1 );
00069 
00070 
00071     cmd[0]          &=   0x38;          // Mask reserved bits
00072 
00073     // On-chip heater
00074     if ( myHeater == HEATER_ENABLED  )
00075         cmd[0]   |=  0x04;              
00076 
00077     // Resolution
00078     switch ( HTU21D_Resolution ) {
00079         default:
00080         case RESOLUTION_12RH_14TEMP :
00081             cmd[0]   &=  0x7E;          
00082             break;
00083 
00084         case RESOLUTION_8RH_12TEMP :
00085             cmd[0]   |=  0x01;          
00086             break;
00087 
00088         case RESOLUTION_10RH_13TEMP :
00089             cmd[0]   |=  0x80;          
00090             break;
00091 
00092         case RESOLUTION_11RH_11TEMP :
00093             cmd[0]   |=  0x81;          
00094             break;
00095     }
00096 
00097 
00098     cmd[1]           =    ( cmd[0] | 0x02 );            //  OTP reload disabled
00099     cmd[0]           =    HTU21D_WRITE_REGISTER;
00100 
00101     aux = i2c.write  ( HTU21D_Addr, &cmd[0], 2 );
00102 
00103 
00104 
00105     if ( aux == I2C_SUCCESS  )
00106         return   HTU21D_SUCCESS;
00107     else
00108         return   HTU21D_FAILURE;
00109 }
00110 
00111 
00112 
00113 /**
00114  * @brief       HTU21D_SoftReset   ( void )
00115  * @details     Rebooting the HTU21D sensor switching the power off and on again.
00116  *
00117  * @param[in]    NaN.
00118  *
00119  * @param[out]   Status of HTU21D_SoftReset.
00120  *
00121  *
00122  * @return      NA
00123  *
00124  * @author      Manuel Caballero
00125  * @date        5/September/2017
00126  * @version     5/September/2017   The ORIGIN
00127  * @pre         NaN
00128  * @warning     The soft reset takes less than 15ms.
00129  */
00130 HTU21D::HTU21D_status_t  HTU21D::HTU21D_SoftReset   ( void )
00131 {
00132     char        cmd                 =    HTU21D_SOFT_RESET;
00133     uint32_t    aux                 =    0;
00134 
00135     
00136     aux = i2c.write  ( HTU21D_Addr, &cmd, 1 );
00137     wait ( 0.015 );
00138     
00139 
00140     if ( aux == HTU21D_SUCCESS )
00141        return   HTU21D_SUCCESS;
00142     else
00143        return   HTU21D_FAILURE;
00144 }
00145 
00146 
00147 
00148  /**
00149  * @brief       HTU21D_TriggerTemperature   ( void )
00150  * @details     Trigger a new temperature measurement.
00151  *
00152  * @param[in]    NaN.
00153  *
00154  * @param[out]   Status of HTU21D_TriggerTemperature.
00155  *
00156  *
00157  * @return      NA
00158  *
00159  * @author      Manuel Caballero
00160  * @date        5/September/2017
00161  * @version     5/September/2017   The ORIGIN
00162  * @pre         NaN
00163  * @warning     The measuring time depends on the chosen resolution.
00164  */
00165 HTU21D::HTU21D_status_t  HTU21D::HTU21D_TriggerTemperature    ( void )
00166 {
00167     char        cmd                 =    0;
00168     uint32_t    aux                 =    0;
00169     float       myDelay             =    0;
00170     
00171     
00172     // Check if the event is by Hold Master Mode
00173     if ( HTU21D_Mode == MODE_HOLD_MASTER  )
00174         cmd   =   HTU21D_TRIGGER_TEMPERATURE_MEASUREMENT_HOLD_MASTER;
00175     else
00176         cmd   =   HTU21D_TRIGGER_TEMPERATURE_MEASUREMENT_NO_HOLD_MASTER;
00177     
00178     
00179     aux = i2c.write  ( HTU21D_Addr, &cmd, 1 );
00180     
00181     // Measuring time
00182     switch ( HTU21D_Resolution ){
00183         default:
00184         case RESOLUTION_12RH_14TEMP :
00185             myDelay  =  0.05;          
00186             break;
00187 
00188         case RESOLUTION_8RH_12TEMP :
00189             myDelay  =  0.013;          
00190             break;
00191 
00192         case RESOLUTION_10RH_13TEMP :
00193             myDelay  =  0.025;          
00194             break;
00195 
00196         case RESOLUTION_11RH_11TEMP :
00197             myDelay  =  0.007;          
00198             break;
00199     }
00200     
00201     wait ( myDelay );
00202 
00203 
00204     if ( aux == HTU21D_SUCCESS )
00205        return   HTU21D_SUCCESS;
00206     else
00207        return   HTU21D_FAILURE;
00208 }
00209 
00210 
00211 
00212 /**
00213  * @brief       HTU21D_ReadTemperature   ( Vector_temperature_f* )
00214  * @details     Read a new temperature measurement.
00215  *
00216  * @param[in]    mytemperature:  Variable to store the temperature.
00217  *
00218  * @param[out]   Status of HTU21D_ReadTemperature.
00219  *
00220  *
00221  * @return      NA
00222  *
00223  * @author      Manuel Caballero
00224  * @date        5/September/2017
00225  * @version     5/September/2017   The ORIGIN
00226  * @pre         NaN
00227  * @warning     HTU21D_TriggerTemperature MUST be called before.
00228  */
00229 HTU21D::HTU21D_status_t  HTU21D::HTU21D_ReadTemperature    ( Vector_temperature_f* mytemperature )
00230 {
00231     char        aux                 =    0;
00232     char        myRawTemp[]         =   { 0, 0, 0};
00233 
00234     
00235     aux = i2c.read  ( HTU21D_Addr, &myRawTemp[0], 3 );
00236 
00237 
00238     mytemperature->Temperature    =   ( myRawTemp[0] << 8 ) | myRawTemp[1];
00239     mytemperature->Temperature   /=   65536.0;
00240     mytemperature->Temperature   *=   175.72;
00241     mytemperature->Temperature   -=   46.85;
00242 
00243 
00244 
00245     if ( aux == HTU21D_SUCCESS )
00246        return   HTU21D_SUCCESS;
00247     else
00248        return   HTU21D_FAILURE;
00249 }
00250 
00251 
00252 
00253 /**
00254  * @brief       HTU21D_ReadRawTemperature   ( Vector_raw_temperature_t* )
00255  * @details     Read a new raw temperature measurement.
00256  *
00257  * @param[in]    myRawtemperature:  Variable to store the temperature.
00258  *
00259  * @param[out]   Status of HTU21D_ReadTemperature.
00260  *
00261  *
00262  * @return      NA
00263  *
00264  * @author      Manuel Caballero
00265  * @date        5/September/2017
00266  * @version     5/September/2017   The ORIGIN
00267  * @pre         NaN
00268  * @warning     HTU21D_TriggerTemperature MUST be called before.
00269  */
00270 HTU21D::HTU21D_status_t  HTU21D::HTU21D_ReadRawTemperature    ( Vector_raw_temperature_t* myRawtemperature )
00271 {
00272     uint32_t    aux                 =    0;
00273 
00274     
00275     aux = i2c.read  ( HTU21D_Addr, &myRawtemperature->RawTemperature[0], 3 );
00276 
00277 
00278     if ( aux == HTU21D_SUCCESS )
00279        return   HTU21D_SUCCESS;
00280     else
00281        return   HTU21D_FAILURE;
00282 }
00283 
00284 
00285 /**
00286  * @brief       HTU21D_TriggerHumidity   ( void  )
00287  * @details     Trigger a new humidity measurement.
00288  *
00289  * @param[in]    NaN.
00290  *
00291  * @param[out]   Status of HTU21D_TriggerHumidity.
00292  *
00293  *
00294  * @return      NA
00295  *
00296  * @author      Manuel Caballero
00297  * @date        5/September/2017
00298  * @version     5/September/2017   The ORIGIN
00299  * @pre         NaN
00300  * @warning     The measuring time depends on the chosen resolution.
00301  */
00302 HTU21D::HTU21D_status_t  HTU21D::HTU21D_TriggerHumidity    ( void )
00303 {
00304     char        cmd                 =    0;
00305     uint32_t    aux                 =    0;
00306     float       myDelay             =    0;
00307     
00308     
00309     // Check if the event is by Hold Master Mode
00310     if ( HTU21D_Mode == MODE_HOLD_MASTER  )
00311         cmd   =   HTU21D_TRIGGER_HUMIDITY_MEASUREMENT_HOLD_MASTER;
00312     else
00313         cmd   =   HTU21D_TRIGGER_HUMIDITY_MEASUREMENT_NO_HOLD_MASTER;
00314     
00315     
00316     aux = i2c.write  ( HTU21D_Addr, &cmd, 1 );
00317     
00318     // Measuring time
00319     switch ( HTU21D_Resolution ){
00320         default:
00321         case RESOLUTION_12RH_14TEMP :
00322             myDelay  =  0.05;          
00323             break;
00324 
00325         case RESOLUTION_8RH_12TEMP :
00326             myDelay  =  0.013;          
00327             break;
00328 
00329         case RESOLUTION_10RH_13TEMP :
00330             myDelay  =  0.025;          
00331             break;
00332 
00333         case RESOLUTION_11RH_11TEMP :
00334             myDelay  =  0.007;          
00335             break;
00336     }
00337     
00338     wait ( myDelay );
00339 
00340 
00341     if ( aux == HTU21D_SUCCESS )
00342        return   HTU21D_SUCCESS;
00343     else
00344        return   HTU21D_FAILURE;
00345 }
00346 
00347 
00348 
00349 /**
00350  * @brief       HTU21D_ReadHumidity   ( Vector_humidity_f* )
00351  * @details     Read a new humidity measurement.
00352  *
00353  * @param[in]    myhumidity:    Variable to store the humidity.
00354  *
00355  * @param[out]   Status of HTU21D_ReadHumidity.
00356  *
00357  *
00358  * @return      NA
00359  *
00360  * @author      Manuel Caballero
00361  * @date        5/September/2017
00362  * @version     5/September/2017   The ORIGIN
00363  * @pre         NaN
00364  * @warning     HTU21D_TriggerHumidity MUST be called before.
00365  */
00366 HTU21D::HTU21D_status_t  HTU21D::HTU21D_ReadHumidity    ( Vector_humidity_f* myhumidity )
00367 {
00368     uint32_t    aux                 =    0;
00369     char        myRawRH[]           =    { 0, 0, 0};
00370 
00371     
00372     aux = i2c.read  ( HTU21D_Addr, &myRawRH[0], 3 );
00373 
00374 
00375     myhumidity->Humidity    =   ( myRawRH[0] << 8 ) | myRawRH[1];
00376     myhumidity->Humidity   /=   65536.0;
00377     myhumidity->Humidity   *=   125.0;
00378     myhumidity->Humidity   -=   6.0;
00379 
00380 
00381 
00382     if ( aux == HTU21D_SUCCESS )
00383        return   HTU21D_SUCCESS;
00384     else
00385        return   HTU21D_FAILURE;
00386 }
00387 
00388 
00389 
00390 /**
00391  * @brief       HTU21D_ReadRawHumidity   ( Vector_raw_humidity_t* )
00392  * @details     Read a new raw humidity measurement.
00393  *
00394  * @param[in]    myRawhumidity:    Variable to store the raw humidity.
00395  *
00396  * @param[out]   Status of HTU21D_ReadHumidity.
00397  *
00398  *
00399  * @return      NA
00400  *
00401  * @author      Manuel Caballero
00402  * @date        5/September/2017
00403  * @version     5/September/2017   The ORIGIN
00404  * @pre         NaN
00405  * @warning     The measuring time depends on the chosen resolution.
00406  * @warning     HTU21D_TriggerHumidity MUST be called before.
00407  */
00408 HTU21D::HTU21D_status_t  HTU21D::HTU21D_ReadRawHumidity    ( Vector_raw_humidity_t* myRawhumidity )
00409 {
00410     uint32_t    aux                 =    0;
00411 
00412     
00413     aux = i2c.read  ( HTU21D_Addr, &myRawhumidity->RawHumidity[0], 3 );
00414 
00415 
00416 
00417     if ( aux == HTU21D_SUCCESS )
00418        return   HTU21D_SUCCESS;
00419     else
00420        return   HTU21D_FAILURE;
00421 }
00422 
00423 
00424 
00425 /**
00426  * @brief       HTU21D_BatteryStatus   ( HTU21D_user_register_status_t* )
00427  * @details     Read the user register to check the battery status.
00428  *
00429  * @param[in]    battStatus:    Variable to store the battery status.
00430  *
00431  * @param[out]   Status of HTU21D_BatteryStatus.
00432  *
00433  *
00434  * @return      NA
00435  *
00436  * @author      Manuel Caballero
00437  * @date        5/September/2017
00438  * @version     5/September/2017   The ORIGIN
00439  * @pre         NaN
00440  * @warning     NaN.
00441  */
00442 HTU21D::HTU21D_status_t  HTU21D::HTU21D_BatteryStatus      ( HTU21D_user_register_status_t* battStatus )
00443 {
00444     char        cmd                 =    HTU21D_READ_REGISTER;
00445     uint32_t    aux                 =    0;
00446 
00447     
00448     aux = i2c.write ( HTU21D_Addr, &cmd, 1 );
00449     aux = i2c.read  ( HTU21D_Addr, &cmd, 1 );
00450     
00451     
00452     cmd         &=   0x40;          // Mask 'Status: End of Batter' bit
00453     
00454     if ( cmd == 0x40 )
00455         *battStatus  =   STATUS_END_BATTERY_LOW_2V25 ;
00456     else
00457         *battStatus  =   STATUS_END_BATTERY_HIGH_2V25 ;
00458 
00459 
00460     if ( aux == HTU21D_SUCCESS )
00461        return   HTU21D_SUCCESS;
00462     else
00463        return   HTU21D_FAILURE;
00464 }