Manuel Caballero / AHT20

Files at this revision

API Documentation at this revision

Comitter:
mcm
Date:
Tue Feb 08 19:39:29 2022 +0000
Parent:
1:9593a4080e4c
Commit message:
The driver was completed and tested (NUCLEO-F103RB), it works as expected.

Changed in this revision

AHT20.cpp Show annotated file Show diff for this revision Revisions of this file
AHT20.h Show annotated file Show diff for this revision Revisions of this file
--- a/AHT20.cpp	Tue Feb 08 17:59:29 2022 +0000
+++ b/AHT20.cpp	Tue Feb 08 19:39:29 2022 +0000
@@ -50,7 +50,7 @@
  */
 AHT20::AHT20_status_t  AHT20::AHT20_Calibrate ( void )
 {
-    char     cmd[]  =   { 0, 0, 0 };
+    char     cmd[3]  =   { 0 };
     uint32_t aux;
 
 
@@ -58,7 +58,7 @@
     cmd[0]   =   AHT20_INITIALIZATION;
     cmd[1]   =   INITIALIZATION_DATA_1;
     cmd[2]   =   INITIALIZATION_DATA_2;
-    aux      =   _i2c.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false );
+    aux      =   _i2c.write ( _AHT20_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false );
 
 
 
@@ -100,7 +100,7 @@
 
     /* Update the register  */
     cmd  =   AHT20_SOFTRESET;
-    aux  =   _i2c.write ( (char*)&cmd, 1U, false );
+    aux  =   _i2c.write ( _AHT20_Addr, (char*)&cmd, 1U, false );
 
 
 
@@ -136,7 +136,7 @@
  */
 AHT20::AHT20_status_t  AHT20::AHT20_TriggerMeasurement ( void )
 {
-    char     cmd[]  =   {0U, 0U, 0U};
+    char     cmd[3]  =   {0U};
     uint32_t aux;
 
 
@@ -144,7 +144,7 @@
     cmd[0]   =   AHT20_TRIGGER_MEASUREMENT;
     cmd[1]   =   TRIGGER_MEASUREMENT_DATA_1;
     cmd[2]   =   TRIGGER_MEASUREMENT_DATA_2;
-    aux      =   _i2c.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), true );
+    aux      =   _i2c.write ( _AHT20_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false );
 
 
 
@@ -184,7 +184,7 @@
 
     /* Update the register   */
     cmd  =   AHT20_STATUS;
-    aux  =   _i2c.write ( (uint8_t*)&cmd, 1U, true );
+    aux  =   _i2c.write ( _AHT20_Addr, (char*)&cmd, 1U, false );
 
 
 
@@ -219,11 +219,10 @@
  */
 AHT20::AHT20_status_t  AHT20::AHT20_GetStatus ( uint8_t* myState )
 {
-    char     cmd    =   0U;
     uint32_t aux;
 
     /* Read the register     */
-    aux  =   _i2c.write ( &(*myState), 1U );
+    aux  =   _i2c.read ( _AHT20_Addr, (char*)&myState, 1U );
 
 
     if ( aux == I2C_SUCCESS )
@@ -262,7 +261,7 @@
 
 
     /* Read the register     */
-    aux  =   _i2c.write ( &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) );
+    aux  =   _i2c.read ( _AHT20_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ) );
 
     /* Parse the data    */
     myAllData->state                         =   cmd[0];
--- a/AHT20.h	Tue Feb 08 17:59:29 2022 +0000
+++ b/AHT20.h	Tue Feb 08 19:39:29 2022 +0000
@@ -22,7 +22,106 @@
 /**
     Example:
 @code
+#include "mbed.h"
+#include "AHT20.h"
 
+AHT20 myAHT20   ( I2C_SDA, I2C_SCL, AHT20::AHT20_ADDRESS, 100000 );
+Serial pc       ( USBTX, USBRX );
+
+DigitalOut  myled ( LED1 );
+Ticker      newReading;
+
+
+//@brief Variables.
+uint32_t    myState = 0UL;
+
+//@brief   FUNCTION PROTOTYPES
+void    changeDATA     ( void );
+
+
+
+//@brief FUNCTION FOR APPLICATION MAIN ENTRY.
+int main()
+{
+    AHT20::AHT20_user_data_t    myAHT20_Data;
+    AHT20::AHT20_status_t       aux;
+
+    pc.baud ( 115200 );
+
+    myled   =   1;
+    wait(3);
+    myled   =   0;
+
+    // Reset the device
+    aux  =   myAHT20.AHT20_SoftReset ();
+    wait_ms(80);
+
+    // Get the state byte
+    aux  =   myAHT20.AHT20_TriggerStatus ();
+    wait_ms(80);
+
+    aux  =   myAHT20.AHT20_GetStatus ( (uint8_t*)&myAHT20_Data.state );
+
+    // Check if the device is calibrated
+    if ( ( myAHT20_Data.state & AHT20::STATUS_CAL_MASK ) == AHT20::STATUS_CAL_UNCALIBRATED ) {
+        // Calibrate the sensor
+        aux  =   myAHT20.AHT20_Calibrate ();
+    }
+
+    newReading.attach( &changeDATA, 1 );                                        // the address of the function to be attached ( changeDATA ) and the interval ( 1s )
+
+    // Let the callbacks take care of everything
+    while(1) {
+        sleep();
+
+        myled = 1;
+
+        if ( myState == 1UL ) {
+            // Trigger a new measurement data
+            aux  =  myAHT20.AHT20_TriggerMeasurement ();
+            wait_ms(80);
+
+            // Get the temperature value
+            aux  =  myAHT20.AHT20_GetAllData ( (AHT20::AHT20_user_data_t*)&myAHT20_Data );
+
+            // Process the temperature data
+            myAHT20_Data.temperature.temperature = myAHT20.AHT20_ProcessTemperature ( myAHT20_Data.temperature.raw_temperature );
+
+            // Process the humidity data
+            myAHT20_Data.humidity.humidity = myAHT20.AHT20_ProcessHumidity ( myAHT20_Data.humidity.raw_humidity );
+
+            // Transmit result over the UART
+            pc.printf( "Temp: %0.2f C | RH: %0.3f %%\r\n", myAHT20_Data.temperature.temperature, myAHT20_Data.humidity.humidity  );
+
+            myState  =   0UL;                                                   // Reset the variable
+        }
+
+        myled = 0;
+    }
+}
+
+
+// @brief       changeDATA ( void  )
+//
+// @details     It changes myState variable
+//
+// @param[in]    N/A
+//
+// @param[out]   N/A.
+//
+//
+// @return       N/A.
+//
+//
+// @author      Manuel Caballero
+// @date        08/February/2022
+// @version     08/February/2022   The ORIGIN
+// @pre         N/A
+// @warning     N/A.
+void changeDATA ( void )
+{
+    myState = 1UL;
+}
 @endcode
 */
 
@@ -37,7 +136,7 @@
     * @brief   DEFAULT ADDRESS
     */
     typedef enum {
-        AHT20_ADDRESS     =   0x38                      /*!<   AHT20 I2C Address                    */
+        AHT20_ADDRESS     =   ( 0x38 << 1U )           /*!<   AHT20 I2C Address                    */
     } AHT20_address_t;