Lib for HP03SA atmospheric pressure sensor. Provides pressure, temperature and altitude data.

Dependents:   mbed_HP03SA_LM77

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HP03SA.h Source File

HP03SA.h

00001 /* mbed HP03SA Library, for an I2C Pressure and Temperature sensor which provides derived Altitude values
00002  * Copyright (c) 2015, v01: WH, Initial version, ported in part from Elektor weatherstation (Sept 2011), Author: W. Waetzig
00003  *                          See http://www.elektor-labs.com/project/usb-long-term-weather-logger-100888.12037.html
00004  *                          Code also based on several other public sources: 
00005  *                          See http://en.wikipedia.org/wiki/Atmospheric_pressure
00006  *                          See https://learn.sparkfun.com/tutorials/bmp180-barometric-pressure-sensor-hookup-?_ga=1.94307604.888266135.1310146152
00007  *
00008  * The HP03S pressure sensor from Hope Microelectronics contains a piezoresistive transducer and
00009  * integrated 16-bit A/D converter (ADC), along with control logic and an I2C interface. 
00010  * The transducer outputs one voltage that depends on pressure and one that depends on temperature.
00011  * These analogue values are alternately converted by the ADC and the results made available on the I2C interface. 
00012  * During the manufacturing process eleven sensor-specific calibration values with a length of two bytes are stored 
00013  * in the device’s EEPROM, and these can also be retrieved by the microcontroller.
00014  * The correction-parameters are read out from the device in used in the calculation.
00015  * A 32 kHz clock with an amplitude of 3 V is required to drive the ADC. This could be derived from the mbed PwmOut.
00016  * The HP03SA has an absolute pressure range between 300-1100 hPa. Operating temp is -20 to +60 degrees Celsius.
00017  * Note: 1 atm = 1013.25 hPa = 1013.25 mbar
00018  *
00019  * Unit conversions see http://www.hoperf.com/docs/guide/160.htm
00020  *
00021  *   bar Bar                                          convert to 100,000 Pascals exactly
00022  *   psi Pounds per Square Inch                       convert to 6894.76 Pascals
00023  *   mb or mbar Millibar                              convert to 100 Pascals (1 hPa) exactly
00024  *   hPa Hectopascal                                  convert to 100 Pascals exactly
00025  *   kPa Kilopascal                                   convert to 1,000 Pascals
00026  *   MPa Megapascal                                   convert to 1,000,000 Pascals
00027  *   Kgf/cm² or kg/cm² Kilogram force per Square Centimetre convert to 98,066.5 Pascals
00028  *   N/m² Newton per Square Meter                     convert to 1 Pascal (SI unit for pressure)
00029  *   torr Torr                                        convert to 133.322 Pascals
00030  *   mtorr Millitorr                                  convert to 0.133322 Pascals
00031  *   mm Hg                                            convert to 133.322387415 Pascals
00032  *   1 atm 1 Atmosphere                               convert to 101325 Pascals exactly (760 mm Hg)
00033  *   1 atm 1 Atmosphere                               convert to 1013.25 hPa exactly 
00034  *   Dynes/cm² or D/cm² Dynes per Square Centimetre   convert to 0.1 Pascals exactly
00035  *   oz/in² Ounces per Square Inch                    convert to 430.922 Pascals
00036  *   tsi (UK) or tfsi (UK) Tonnes Force per Square Inch (Long, UK) convert to 15444300 Pascals
00037  *   tsi (USA) or tfsi (USA) Ton Force per Square Inch (Short, USA) convert to 13789500 Pascals
00038  *   tsf (USA) or tfsf (USA) Tons Force per Square Foot (Short, USA) convert to 95760.5 Pascals
00039  *   psf Pounds per Square Foot                       convert to 47.8803 Pascals
00040  *   g/cm² or gf/cm² Grammes Force per Square Centimetre convert to 98.0665 Pascals
00041  *
00042  * The Altitude calculation is based on the ICAO Standard Atmosphere model ISA. The code has 
00043  * certainly not been thoroughly validated! Use at own risk.
00044  *
00045  * QNH is the barometric altimeter setting that causes an altimeter to read 
00046  * airfield elevation above mean sea level when on the airfield. In ISA temperature
00047  * conditions the altimeter will read altitude above mean sea level in the vicinity of the airfield.
00048  * Note: Remember QNH as mnemonic for "nautical height"
00049  * Average sea-level pressure is 101.325 kPa (1013.25 hPa or mbar) or 29.92 inches (inHg) or 
00050  * 760 millimetres of mercury (mmHg). In aviation weather reports (METAR), QNH is transmitted 
00051  * around the world in hectopascals or millibars (1 hectopascal = 1 millibar), except in the 
00052  * United States, Canada, and Colombia where it is reported in inches (to two decimal places) of mercury. 
00053  *
00054  * 
00055  * Permission is hereby granted, free of charge, to any person obtaining a copy
00056  * of this software and associated documentation files (the "Software"), to deal
00057  * in the Software without restriction, including without limitation the rights
00058  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00059  * copies of the Software, and to permit persons to whom the Software is
00060  * furnished to do so, subject to the following conditions:
00061  *
00062  * The above copyright notice and this permission notice shall be included in
00063  * all copies or substantial portions of the Software.
00064  *
00065  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00066  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00067  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00068  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00069  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00070  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00071  * THE SOFTWARE.
00072  */
00073 #ifndef MBED_HP03SA_H
00074 #define MBED_HP03SA_H
00075 
00076 #include "mbed.h"
00077 
00078 /** An interface for the HP03S I2C pressure and temperature sensor
00079  *
00080  * @code
00081  * #include "mbed.h"
00082  * #include "HP03S.h"
00083  * 
00084  * // I2C Communication
00085  * I2C i2c(p28,p27); // SDA, SCL
00086  *
00087  * // Serial Communication*
00088  * Serial pc(USBTX,USBRX);
00089  *
00090  * // Instantiate HP03SA
00091  * HP03SA hp03sa(&i2c, D_XCLR); // I2C bus, XCLK Pin 
00092  * // Generate the 32kHz master clock for HP03SA
00093  * PwmOut mclk(p21);
00094  *
00095  * int main() {
00096  *   pc.printf("Hello World!\n");
00097  * 
00098  *   //Init MCLK for HP03SA
00099  *   mclk.period_us(30);       //32768 KHz
00100  *   mclk.pulsewidth_us(15);   //32768 KHz
00101  *
00102  *   while(1) {
00103  *     // Take reading from sensor            
00104  *     hp03sa.sample();
00105  *    
00106  *     // Show Pressure    
00107  *     pc.printf("Absolute atmospheric pressure is %.1f [hPa]\r\n", (float) hp03sa.getAbsPressure() / 10.0f);
00108  *     pc.printf("Sealevel atmospheric pressure is %.1f [hPa]\r\n", (float) hp03sa.getSeaPressure(20) / 10.0f); // About right where I live..
00109  *   
00110  *     // Show Temperature
00111  *     pc.printf("Ambient temperature is %.1f [C]\r\n", (float) hp03sa.getTemperature() / 10.0f);       
00112  *
00113  *     // Show Altitude meters
00114  *     pc.printf("Altitude estimated at %d [m]\r\n", hp03sa.getAltitude());       
00115  * 
00116  *     // Show Altitude Ft
00117  *     pc.printf("Altitude estimated at %d [ft]\r\n", hp03sa.getAltitudeFt());
00118  *
00119  *     wait(1.0);
00120  *   }
00121  * }
00122  * @endcode
00123  */
00124 
00125 // Device I2C Slave addresses
00126 #define HP03SA_BARO_SA   0xEE 
00127 #define HP03SA_EEPROM_SA 0xA0
00128 
00129 //ISA computation constants
00130 #define RLGM        0.19026674f          // ISA Exponent (R * L) / (g * M)
00131 #define GMRL        5.25578009f          // ISA Exponent (g * M) / (R * L) 
00132 #define LBASE_X10   0.065f               // ISA Temp Lapse (i.e. drop) in K per meter x10
00133 #define TBASE_X10   (150.0f + 2731.5f)   // ISA Ref Temp in K x10
00134 #define PBASE_X10   (1013.25f * 10.0f)   // ISA Ref Pres in hPa x10
00135   
00136 /** Create an HP03SA Class instance
00137   *
00138   */ 
00139 class HP03SA {
00140 
00141 public:
00142 
00143   /** Create an HP03SA device instance
00144     *
00145     * @param i2c    I2C Bus 
00146     * @param XCLR   Clock enable control line
00147     */ 
00148    HP03SA(I2C *i2c, PinName XCLR);
00149 
00150   /** Get Status
00151     *
00152     * @return bool Sensor ready 
00153     */ 
00154    bool getStatus(void);
00155 
00156   /** Get Pressure and Temperature sample
00157     *
00158     * @return none 
00159     */ 
00160    void sample(void);
00161 
00162   /** Get Absolute Atmospheric Pressure in hPa x 10
00163     * Note: sample() must be called before getAbsPressure()
00164     *
00165     * @return int Pressure in hPa x 10
00166     */ 
00167    int getAbsPressure(void);
00168 
00169   /** Get Sealevel Atmospheric Pressure in hPa x 10
00170     * Note: sample() must be called before getSeaPressure()
00171     *
00172     * @param  int alt_meter Altitude above Mean Sea Level where measurement is taken
00173     * @return int Pressure at sea level in hPa x 10   
00174     */ 
00175    int getSeaPressure(int alt_meter);
00176 
00177   /** Get Temperature as int in °Celsius x 10
00178     * Note: sample() must be called before getTemperature()  
00179     *
00180     * @return int Temperature in °Celsius x 10
00181     */ 
00182    int getTemperatureInt(void);
00183 
00184   /** Get Temperature as float in °Celsius
00185     * Note: sample() must be called before getTemperatureFl()  
00186     *
00187     * @return float Temperature in °Celsius
00188     */ 
00189    float getTemperature(void);
00190 
00191   /** Get Altitude in meters using ISA
00192     * Note: sample() must be called before getAltitude()  
00193     *
00194     * @return int Altitude in meters above Mean Sea Level (MSL)
00195     */ 
00196    int getAltitude(void);
00197 
00198   /** Get Altitude in feet using ISA
00199     * Note: sample() must be called before getAltitudeFt()  
00200     *
00201     * @return int Altitude in feet above Mean Sea Level (MSL)
00202     */ 
00203    int getAltitudeFt();
00204 
00205   /** Set QNH Pressure to calibrate sensor
00206     *
00207     * @param int pressure_x10 at Mean Sea Level in hPa x 10
00208     *            The getAltitude() reading will be 0 m for the set pressure.
00209     */ 
00210    void setPressure(int pressure_x10 = PBASE_X10);
00211 
00212   /** Set QNH Altitude in meter to calibrate sensor
00213     *
00214     * @param int alt_meter Altitude in meters above Mean Sea Level (MSL) for current pressure
00215     *            The getAltitude() reading will be 'alt_meter' m for the current pressure.   
00216     */ 
00217    void setAltitude(int alt_meter = 0);
00218 
00219 
00220   /** Set QNH Altitude in feet to calibrate sensor
00221     *
00222     * @param int alt_feet Altitude in meters above Mean Sea Level (MSL) for current pressure
00223     *            The getAltitudeFt() reading will be 'alt_feet' ft for the current pressure.   
00224     */ 
00225    void setAltitudeFt(int alt_feet = 0);
00226 
00227 
00228    /** Convert Temperature from °Celsius into °Fahrenheit
00229      *
00230      * @param  float celsius in °Celsius  
00231      * @return float temperature in °Fahrenheit
00232      */ 
00233     float celsiusToFahrenheit(float celsius);
00234  
00235 private: 
00236 
00237   /** Init HP03SA device
00238     *
00239     * @return none    
00240     */ 
00241    void _init();
00242 
00243   /** Read ADC value (either pressure or temperature depending on Control Register setting)
00244     *
00245     * @return int   pressure or temperature 
00246     */ 
00247    int _readADC (void);
00248 
00249   /** Read Pressure and Temperature
00250     *
00251     * @return none 
00252     */ 
00253    void _readTempPres(void);
00254   
00255   /** Calculate Pressure and Temperature
00256     *
00257     * @return none 
00258     */ 
00259    void _calcTempPres(void);
00260  
00261   /** Calculate Altitude
00262     *
00263     * @return none 
00264     */ 
00265    void _calcAlt(void);
00266 
00267   //I2C interface and ADC control
00268   I2C *_i2c;
00269   DigitalOut _xclr;
00270 
00271   //Raw values, computed values
00272   int  _D1,  _D2;        // measured values: pressure,temperature.  
00273   int  _dUT, _off;       // auxiliary variables
00274   int  _sens, _x;        // auxiliary variables  
00275   int  _temp_x10;        // temperature in Celsius x 10
00276   int  _pres_x10;        // pressure in hPa x 10  
00277   int  _alt;             // altitude in m  
00278   float _pres_base_x10;
00279   float _temp_base_x10;
00280     
00281   //Calibration coefficients
00282   int _C1,  //Sensitivity coefficient  
00283       _C2,  //Offset coefficient
00284       _C3,  //Temp coefficient of sensitivity
00285       _C4,  //Temp coefficient of offset
00286       _C5,  //Reference temperature
00287       _C6,  //Temp coefficient of temperrature
00288       _C7;  //Offset fine tuning
00289   //Sensor correction-parameters A, B, C, D    
00290   int _A, _B, _C, _D;  
00291 };
00292    
00293 #endif