Digital Humidity Sensor SHT2x (RH/T)

Files at this revision

API Documentation at this revision

Comitter:
mcm
Date:
Tue Sep 04 11:07:22 2018 +0000
Parent:
1:9f1fcd1e4c9c
Commit message:
This driver was completed and tested ( NUCLEO-L152RE ), it works as expected.

Changed in this revision

SHT2X.cpp Show annotated file Show diff for this revision Revisions of this file
SHT2X.h Show annotated file Show diff for this revision Revisions of this file
diff -r 9f1fcd1e4c9c -r 5893455b8c6c SHT2X.cpp
--- a/SHT2X.cpp	Mon Sep 03 14:30:18 2018 +0000
+++ b/SHT2X.cpp	Tue Sep 04 11:07:22 2018 +0000
@@ -1,5 +1,5 @@
 /**
- * @brief       SHT2X.c
+ * @brief       SHT2X.cpp
  * @details     Humidity and Temperature Sensor IC.
  *              Functions file.
  *
diff -r 9f1fcd1e4c9c -r 5893455b8c6c SHT2X.h
--- a/SHT2X.h	Mon Sep 03 14:30:18 2018 +0000
+++ b/SHT2X.h	Tue Sep 04 11:07:22 2018 +0000
@@ -23,7 +23,132 @@
     Example:
 
 @code
+#include "mbed.h"
+#include "SHT2X.h"
 
+SHT2X mySHT2X ( I2C_SDA, I2C_SCL, SHT2X::SHT2X_ADDRESS, 400000 );
+Serial pc     ( USBTX, USBRX );                                                 // tx, rx
+
+DigitalOut  myled       ( LED1 );
+Ticker      newAction;
+
+
+//@brief Constants.
+//
+#define TX_BUFF_SIZE  64                                                        //   UART buffer size
+
+
+//@brief Variables.
+//
+volatile uint32_t myState;                                                      //   State that indicates when to perform an ADC sample
+
+
+//
+// @brief   FUNCTION PROTOTYPES
+//
+void    changeDATA     ( void );
+
+
+//@brief FUNCTION FOR APPLICATION MAIN ENTRY.
+//
+int main()
+{
+    char     myBatteryMessage[5]  =  "";
+    uint32_t aux;
+    SHT2X::SHT2X_vector_data_t    mySHT2X_Data;
+
+    pc.baud ( 115200 );
+
+
+    myled   =   1;
+    wait(3);
+    myled   =   0;
+
+    // Perform a software reset
+    aux  =   mySHT2X.SHT2X_SoftReset ();
+    wait_ms ( 15 );
+
+    // Configure the device: Resolution Temperature: 14-bit and Humidity: 12-bit, Heater is disabled
+    aux  =   mySHT2X.SHT2X_Conf            ( SHT2X::USER_REGISTER_RESOLUTION_12RH_14TEMP, SHT2X::USER_REGISTER_HEATER_DISABLED );
+
+    // Get the serial number
+    aux  =   mySHT2X.SHT2X_GetSerialNumber ( &mySHT2X_Data );
+
+    // Transmit result through the UART
+    pc.printf( "ID: %ld\r\n", mySHT2X_Data.SerialNumber );
+
+    myState  =   0UL;                                                           // Reset the variable
+    newAction.attach( &changeDATA, 0.5f );                                      // the address of the function to be attached ( changeDATA ) and the interval ( 0.5s )
+
+    // Let the callbacks take care of everything
+    while(1) {
+        sleep();
+
+        switch ( myState ) {
+            default:
+            case 1:
+                myled = 1;
+
+                // Trigger a new temperature measurement
+                aux  =   mySHT2X.SHT2X_TriggerTemperature ( SHT2X::SHT2X_NO_HOLD_MASTER_MODE );
+                break;
+
+            case 2:
+                // Get the temperature
+                aux  =   mySHT2X.SHT2X_ReadTemperature    ( &mySHT2X_Data );
+
+                // Trigger a new relative humidity measurement
+                aux  =   mySHT2X.SHT2X_TriggerHumidity    ( SHT2X::SHT2X_NO_HOLD_MASTER_MODE );
+                break;
+
+            case 3:
+                // Get the relative humidity
+                aux  =   mySHT2X.SHT2X_ReadHumidity       ( &mySHT2X_Data );
+
+                // Get the battery status
+                aux  =   mySHT2X.SHT2X_BatteryStatus      ( &mySHT2X_Data );
+
+                // Prepare the message for the battery status
+                if ( ( mySHT2X_Data.BatteryStatus & SHT2X::USER_REGISTER_STATUS_END_BATTERY_MASK ) == SHT2X::USER_REGISTER_STATUS_END_BATTERY_HIGH_2V25 ) {
+                    strcpy( myBatteryMessage, "GOOD" );
+                } else {
+                    strcpy( myBatteryMessage, "BAD" );
+                }
+
+                // Transmit result through the UART
+                pc.printf ( "Temperature: %0.2f C | RH: %0.2f | Battery: %s\r\n", mySHT2X_Data.Temperature, mySHT2X_Data.RelativeHumidity, myBatteryMessage );
+
+
+                // Reset the variables
+                myState  =   0UL;
+                myled    =   0;
+                break;
+        }
+    }
+}
+
+
+// @brief       changeDATA ( void  )
+//
+// @details     It changes myState variable
+//
+// @param[in]    N/A
+//
+// @param[out]   N/A.
+//
+//
+// @return       N/A..
+//
+//
+// @author      Manuel Caballero
+// @date        3/September/2018
+// @version     3/September/2018   The ORIGIN
+// @pre         N/A
+// @warning     N/A.
+void changeDATA ( void )
+{
+    myState++;
+}
 @endcode
 */