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.
Diff: AHT20.h
- Revision:
- 2:ec96fb27c02d
- Parent:
- 0:a7e98be43098
--- 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;