MEMS pressure sensor: 260-1260 hPa absolute digital output barometer

Dependents:   GB9662

Files at this revision

API Documentation at this revision

Comitter:
mcm
Date:
Fri Jun 14 12:48:53 2019 +0000
Parent:
3:c35a7ddc6772
Commit message:
An example of use for this driver was added into the header file.

Changed in this revision

LPS25HB.h Show annotated file Show diff for this revision Revisions of this file
diff -r c35a7ddc6772 -r c94efb61cefe LPS25HB.h
--- a/LPS25HB.h	Fri Jun 14 12:45:08 2019 +0000
+++ b/LPS25HB.h	Fri Jun 14 12:48:53 2019 +0000
@@ -22,6 +22,156 @@
 /**
     Example:
 @code
+#include "mbed.h"
+#include "LPS25HB.h"
+
+LPS25HB myLPS25HB ( I2C_SDA, I2C_SCL, LPS25HB::LPS25HB_ADDRESS_1, 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()
+{
+    LPS25HB::LPS25HB_status_t aux;
+    LPS25HB::LPS25HB_data_t   myLPS25HB_Data;
+
+    pc.baud ( 115200 );
+
+
+    myled   =   1;
+    wait(3);
+    myled   =   0;
+
+    // Perform a software reset  
+    aux  =   myLPS25HB.LPS25HB_SetSoftwareReset ();
+
+    do {
+        aux  =   myLPS25HB.LPS25HB_GetSoftwareReset ( &myLPS25HB_Data );        // Dangerous!!! The uC may get stuck here...
+        // [WORKAROUND] Insert a counter
+    } while ( myLPS25HB_Data.swreset == LPS25HB::CTRL_REG2_SWRESET_SW_RESET );
+
+    // Reboot memory content  
+    aux  =   myLPS25HB.LPS25HB_SetRebootMemoryContent ();
+
+    do {
+        aux  =   myLPS25HB.LPS25HB_GetRebootMemoryContent ( &myLPS25HB_Data );  // Dangerous!!! The uC may get stuck here...
+        // [WORKAROUND] Insert a counter
+    } while ( myLPS25HB_Data.boot == LPS25HB::CTRL_REG2_BOOT_REBOOT_MODE );
+
+    // Set device in lOW-POWER mode  
+    aux  =   myLPS25HB.LPS25HB_SetPowerMode ( LPS25HB::CTRL_REG1_PD_POWER_DOWN_MODE );
+
+    // Get device ID  
+    aux  =   myLPS25HB.LPS25HB_GetDeviceID ( &myLPS25HB_Data );
+    pc.printf ( "Device ID: %x\r\n", myLPS25HB_Data.deviceID );
+
+    // Set temperature resolution: 64 internal average 
+    myLPS25HB_Data.avgt  =   LPS25HB::RES_CONF_AVGT_64;
+    aux  =   myLPS25HB.LPS25HB_SetTemperatureResolution ( myLPS25HB_Data );
+
+    // Set pressure resolution: 512 internal average 
+    myLPS25HB_Data.avgp  =   LPS25HB::RES_CONF_AVGP_512;
+    aux  =   myLPS25HB.LPS25HB_SetPressureResolution ( myLPS25HB_Data );
+
+    // Set ODR: One-shot mode enabled  
+    myLPS25HB_Data.odr   =   LPS25HB::CTRL_REG1_ODR_ONE_SHOT_MODE;
+    aux  =   myLPS25HB.LPS25HB_SetOutputDataRate ( myLPS25HB_Data );
+
+    // Interrupt generation disabled  
+    aux  =   myLPS25HB.LPS25HB_SetInterruptGeneration ( LPS25HB::CTRL_REG1_DIFF_EN_ENABLED );
+
+    // Block data update: output registers not updated until MSB and LSB have been read  
+    aux  =   myLPS25HB.LPS25HB_SetBlockDataUpdate ( LPS25HB::CTRL_REG1_BDU_1 );
+
+    // FIFO disabled 
+    myLPS25HB_Data.fifo_en   =   LPS25HB::CTRL_REG2_FIFO_EN_DISABLED;
+    aux  =   myLPS25HB.LPS25HB_SetFIFOEnable ( myLPS25HB_Data );
+
+    // Autozero: Normal mode 
+    myLPS25HB_Data.autozero  =   LPS25HB::CTRL_REG2_AUTOZERO_NORMAL_MODE;
+    aux  =   myLPS25HB.LPS25HB_SetAutozero ( myLPS25HB_Data );
+
+    // Set device in ACTIVE mode  
+    aux  =   myLPS25HB.LPS25HB_SetPowerMode ( LPS25HB::CTRL_REG1_PD_ACTIVE_MODE );
+
+
+    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 value  
+            aux  =   myLPS25HB.LPS25HB_SetOneShot ();
+
+            // Wait until the conversion is done  
+            do {
+                aux  =   myLPS25HB.LPS25HB_GetOneShot ( &myLPS25HB_Data );
+            } while( myLPS25HB_Data.one_shot == LPS25HB::CTRL_REG2_ONE_SHOT_NEW_DATASET );                       // Dangerous!!! The uC may get stuck here...
+            // [WORKAROUND] Insert a counter
+
+            // Wait until there is a new data ( both pressure and temperature )  
+            do {
+                aux  =   myLPS25HB.LPS25HB_GetStatusRegister ( &myLPS25HB_Data );
+            } while( ( myLPS25HB_Data.status_reg & ( LPS25HB::STATUS_REG_P_DA_MASK | LPS25HB::STATUS_REG_T_DA_MASK ) ) != ( LPS25HB::STATUS_REG_P_DA_NEW_DATA | LPS25HB::STATUS_REG_T_DA_NEW_DATA ) ); // Dangerous!!! The uC may get stuck here...
+            // [WORKAROUND] Insert a counter
+
+            // Get pressure  
+            aux  =   myLPS25HB.LPS25HB_GetPressure ( &myLPS25HB_Data );
+
+            // Get temperature 
+            aux  =   myLPS25HB.LPS25HB_GetTemperature ( &myLPS25HB_Data );
+
+            // Send data through the UART    
+            pc.printf ( "T: %0.1f C, P: %0.1f mbar\r\n", myLPS25HB_Data.temperature, myLPS25HB_Data.pressure );
+
+
+            // 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        31/May/2019
+// @version     31/May/2019   The ORIGIN
+// @pre         N/A
+// @warning     N/A.
+void changeDATA ( void )
+{
+    myState  =   1UL;
+}
 @endcode
 */