Digital Humidity Sensor
Revision 3:42e11b9135b5, committed 2021-04-26
- Comitter:
- mcm
- Date:
- Mon Apr 26 11:22:46 2021 +0000
- Parent:
- 2:870701ee67c4
- Commit message:
- The driver was completed and tested (NUCLEO-L152RE), it works as expected.
Changed in this revision
SHT3X.cpp | Show annotated file Show diff for this revision Revisions of this file |
SHT3X.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/SHT3X.cpp Thu Mar 25 20:24:25 2021 +0000 +++ b/SHT3X.cpp Mon Apr 26 11:22:46 2021 +0000 @@ -55,8 +55,8 @@ uint32_t aux; /* Write the register */ - cmd[0] = (uint8_t)( mode >> 8U ); - cmd[1] = (uint8_t)( mode & 0xFF ); + cmd[0] = ( mode >> 8U ); + cmd[1] = ( mode & 0xFF ); aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); @@ -152,8 +152,8 @@ uint32_t aux; /* Write the register */ - cmd[0] = (uint8_t)( SHT3X_PERIODIC_MESUREMENT_WITH_ART >> 8U ); - cmd[1] = (uint8_t)( SHT3X_PERIODIC_MESUREMENT_WITH_ART & 0xFF ); + cmd[0] = ( SHT3X_PERIODIC_MESUREMENT_WITH_ART >> 8U ); + cmd[1] = ( SHT3X_PERIODIC_MESUREMENT_WITH_ART & 0xFF ); aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); @@ -192,8 +192,8 @@ uint32_t aux; /* Write the register */ - cmd[0] = (uint8_t)( SHT3X_BREAK >> 8U ); - cmd[1] = (uint8_t)( SHT3X_BREAK & 0xFF ); + cmd[0] = ( SHT3X_BREAK >> 8U ); + cmd[1] = ( SHT3X_BREAK & 0xFF ); aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); @@ -232,8 +232,8 @@ uint32_t aux; /* Write the register */ - cmd[0] = (uint8_t)( SHT3X_SOFT_RESET >> 8U ); - cmd[1] = (uint8_t)( SHT3X_SOFT_RESET & 0xFF ); + cmd[0] = ( SHT3X_SOFT_RESET >> 8U ); + cmd[1] = ( SHT3X_SOFT_RESET & 0xFF ); aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); @@ -272,8 +272,8 @@ uint32_t aux; /* Write the register */ - cmd = SHT3X_GENERAL_CALL_RESET_SECOND_BYTE; - aux = _i2c.write ( SHT3X_GENERAL_CALL_RESET_ADDRESS_BYTE, &cmd, 1U, false ); + cmd = SHT3X_GENERAL_CALL_RESET_SECOND_BYTE; + aux = _i2c.write ( SHT3X_GENERAL_CALL_RESET_ADDRESS_BYTE, &cmd, 1U, false ); @@ -312,8 +312,8 @@ uint32_t aux; /* Write the register */ - cmd[0] = (uint8_t)( heater >> 8U ); - cmd[1] = (uint8_t)( heater & 0xFF ); + cmd[0] = ( heater >> 8U ); + cmd[1] = ( heater & 0xFF ); aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); @@ -352,8 +352,8 @@ uint32_t aux = SHT3X_SUCCESS; /* Write the register */ - cmd[0] = (uint8_t)( SHT3X_STATUS_REGISTER >> 8U ); - cmd[1] = (uint8_t)( SHT3X_STATUS_REGISTER & 0xFF ); + cmd[0] = ( SHT3X_STATUS_REGISTER >> 8U ); + cmd[1] = ( SHT3X_STATUS_REGISTER & 0xFF ); aux |= _i2c.write ( _SHT3X_Addr, &cmd[0], 2U, true ); /* Read the register */ @@ -403,8 +403,8 @@ uint32_t aux; /* Write the register */ - cmd[0] = (uint8_t)( SHT3X_CLEAR_STATUS_REGISTER >> 8U ); - cmd[1] = (uint8_t)( SHT3X_CLEAR_STATUS_REGISTER & 0xFF ); + cmd[0] = ( SHT3X_CLEAR_STATUS_REGISTER >> 8U ); + cmd[1] = ( SHT3X_CLEAR_STATUS_REGISTER & 0xFF ); aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); @@ -444,8 +444,8 @@ uint32_t aux; /* Write the register */ - cmd[0] = (uint8_t)( mo >> 8U ); - cmd[1] = (uint8_t)( mo & 0xFF ); + cmd[0] = ( mo >> 8U ); + cmd[1] = ( mo & 0xFF ); aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false ); @@ -485,8 +485,8 @@ uint32_t aux; /* Write the register */ - cmd[0] = (uint8_t)( SHT3X_FETCH_DATA >> 8U ); - cmd[1] = (uint8_t)( SHT3X_FETCH_DATA & 0xFF ); + cmd[0] = ( SHT3X_FETCH_DATA >> 8U ); + cmd[1] = ( SHT3X_FETCH_DATA & 0xFF ); aux = _i2c.write ( _SHT3X_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), true ); /* Read the register */
--- a/SHT3X.h Thu Mar 25 20:24:25 2021 +0000 +++ b/SHT3X.h Mon Apr 26 11:22:46 2021 +0000 @@ -23,7 +23,104 @@ Example: @code +#include "mbed.h" +#include "SHT3X.h" +SHT3X mySHT3X ( I2C_SDA, I2C_SCL, SHT3X::SHT3X_ADDRESS_B, 400000 ); // I2C_SDA | I2C_SCL +Serial pc ( USBTX, USBRX ); // tx, rx + +DigitalOut myled ( LED1 ); +Ticker newAction; + + +//@brief Constants. + + +//@brief Variables. +volatile uint32_t myState; // State that indicates when to perform a new sample + + +//@brief FUNCTION PROTOTYPES +void changeDATA ( void ); + + +//@brief FUNCTION FOR APPLICATION MAIN ENTRY. +int main() +{ + SHT3X::SHT3X_status_t aux; + SHT3X::SHT3X_data_t mySHT3X_Data; + + pc.baud ( 115200 ); + + myled = 1; + wait(3); + myled = 0; + + // Set a software reset + aux = mySHT3X.SHT3X_SetSoftReset (); + wait_ms(2U); + + // Clear the STATUS register + aux = mySHT3X.SHT3X_ClearStatus (); + + // Disable the Heater + aux = mySHT3X.SHT3X_SetHeater ( SHT3X::SHT3X_HEATER_DISABLE ); + + // Do not perform periodic measurement + aux = mySHT3X.SHT3X_SetBreakCommand (); + + myState = 0UL; // Reset the variable + newAction.attach( &changeDATA, 1U ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s ) + + // Let the callbacks take care of everything + while(1) { + sleep(); + + if ( myState == 1UL ) { + myled = 1U; + + // Trigger to get a new data set, no clock stretching and repeatibility high + aux = mySHT3X.SHT3X_OneShotTriggerAllData ( SHT3X::SHT3X_CLOCK_STRETCHING_DISABLED_REPEATABILITY_HIGH ); + wait_ms(15U); + + // Get all the data + aux = mySHT3X.SHT3X_OneShotGetAllRawData ( &mySHT3X_Data.rawData ); + + + // Process all the data + mySHT3X.SHT3X_ProccessData ( mySHT3X_Data.rawData, &mySHT3X_Data.data ); + + // Send data through the UART + pc.printf ( "T: %0.2f C, RH: %0.2f %%\r\n", mySHT3X_Data.data.temperature, mySHT3X_Data.data.relativeHumidity ); + + + // Reset the variables + myState = 0UL; + myled = 0U; + } + } +} + + +// @brief changeDATA ( void ) +// +// @details It changes myState variable +// +// @param[in] N/A +// +// @param[out] N/A. +// +// @return N/A. +// +// @author Manuel Caballero +// @date 25/March/2021 +// @version 25/March/2021 The ORIGIN +// @pre N/A +// @warning N/A. +void changeDATA ( void ) +{ + myState = 1UL; +} @endcode */