Class for using BMP180 Bosch Pressure sensor

Dependents:   ILI9341_Clock_Nucleo IoT-Polytech-Upmc

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BMP180.h Source File

BMP180.h

00001 #ifndef BMP180_H
00002 #define BMP180_H
00003 
00004 /* BMP180 Digital Pressure Sensor Class for use with Mbed LPC1768 and other platforms
00005 *  BMP180 from Bosch Sensortec
00006 *  Copyright (c) 2013 Philip King Smith
00007 *
00008 * Permission is hereby granted, free of charge, to any person obtaining a copy
00009 * of this software and associated documentation files (the "Software"), to deal
00010 * in the Software without restriction, including without limitation the rights
00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 * copies of the Software, and to permit persons to whom the Software is
00013 * furnished to do so, subject to the following conditions:
00014 *
00015 * The above copyright notice and this permission notice shall be included in
00016 * all copies or substantial portions of the Software.
00017 *
00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 * THE SOFTWARE.
00025 *
00026 * Bosch data sheet at: http://ae-bst.resource.bosch.com/media/products/dokumente/bmp180/BST-BMP180-DS000-09.pdf
00027 * Some parts of the calculations used are infact from Rom Clement published Mbed code here: https://mbed.org/users/Rom/code/Barometer_bmp085/
00028 *   I only used snippets of the Rom's code because i was making this into a class and so this is structured totaly different then his code example is.
00029 * I also used the Bosch data sheet showing the calculations and adjusted everything accordingly!
00030 */
00031 
00032 #include "mbed.h"
00033 
00034 #define EEprom 22            // The EEPROM has 176bits of calibration data (176/8 = 22 Bytes)
00035 #define BMP180ADDR 0xEF      // I2C address of BMP180 device
00036 #define BMP180FREQ 1000000   // Data sheet says 3.4 MHz is max but not sure what mbed can do here!
00037 #define CMD_READ_VALUE 0xF6
00038 #define CMD_READ_CALIBRATION 0xAA
00039 #define OVERSAMPLING_ULTRA_LOW_POWER 0  // these are the constants used in the oversample variable in the below code!
00040 #define OVERSAMPLING_STANDARD 1
00041 #define OVERSAMPLING_HIGH_RESOLUTION 2
00042 #define OVERSAMPLING_ULTRA_HIGH_RESOLUTION 3
00043 
00044 
00045 /** BMP180 Digital Pressure Sensor class using mbed's i2c class
00046  *
00047  * Example:
00048  * @code
00049  * // show how the BMP180 class works
00050  * #include "mbed.h"
00051  * #include "BMP180.h"
00052  *
00053  * int main()
00054  * {
00055  *
00056  *    long temp ;
00057  *    long pressure;
00058  *    int error ;
00059  *    BMP180 mybmp180(p9,p10);
00060  *    while(1) {
00061  *        error = mybmp180.readTP(&temp,&pressure,OVERSAMPLING_ULTRA_HIGH_RESOLUTION);
00062  *        printf("Temp is %ld\r\n",temp);
00063  *        printf("Pressure is %ld\r\n",pressure);
00064  *        printf("Error is %d\r\n\r\n",error);
00065  *        wait(2);
00066  *    }
00067  * }
00068  * @endcode
00069  */
00070 
00071 class BMP180
00072 {
00073 public:
00074     /** Create object connected to BMP180 pins ( remember both pins need pull up resisters)
00075         *
00076         * Ensure the pull up resistors are used on these pins.  Also note there is no checking on
00077         *  if you use these pins p9, p10, p27, p28 so ensure you only use these ones on the LPC1768 device
00078         *
00079         * @param sda pin that BMP180 connected to (p9 or p28 as defined on LPC1768)
00080         * @param slc pin that BMP180 connected to (p10 or p27 ad defined on LPC1768)
00081         */
00082     BMP180(PinName sda, PinName slc);   // Constructor
00083 
00084     ~BMP180();                          // Destructor
00085 
00086     /** Read Temperature and Pressure at the same time
00087     *
00088     * This function will only return when it has readings.  This means that the time will vary depending on oversample setting!
00089     *   Note if your code can not wait for these readings to be taken and calculated you should use the functions below.
00090     *     These other functions are designed to allow your code to do other things then get the final readings.
00091     *     This function is only designed as a one shot give me the answer function.
00092     *
00093     * @param t the temperature fully compensated value is returned in this variable. Degrees celsius with one decimal so 253 is 25.3 C.
00094     * @param p the barometric pressure fully compensated value is returned in this variable. Pressure is in Pa so 88007 is 88.007 kPa.
00095     * @param oversample is the method method for reading sensor.  OVERSAMPLING_ULTRA_HIGH_RESOLUTION is used if an incorrect value is passed to this function.
00096     * @param returns 0 for no errors during i2c communication.  Any other number is just a i2c communication failure of some kind!
00097     */
00098     int readTP(long *t, long *p, int oversample);    // get both temperature and pressure fully compensated values! Note this only returns when measurements are complete
00099 
00100     /** Start the temperature reading process but return after the commands are issued to BMP180
00101     *
00102     * This function is ment to start the temperature reading process but will return to allow other code to run then a reading could be made at a later time.
00103     *   Note the maximum time needed for this measurment is 4.5 ms.
00104     *
00105     * @param returns 0 for no errors during i2c communication.  Any other number is just a i2c communication failure of some kind!
00106     */
00107     int startTemperature();             // Start temperature measurement
00108 
00109     /** Reads the last temperature reading that was started with startTemperature() function
00110     *
00111     * This function will return the fully compensated value of the temperature in Degrees celsius with one decimal so 253 is 25.3 C.
00112     *   Note this function should normaly follow the startTemperature() function and should also preceed the startPressure() and readPressure() commands!
00113     *   Note this function should follow startTemperature() after 4.5 ms minimum has elapsed or reading will be incorrect.
00114     *
00115     * @param t the temperature fully compensated value is returned in this variable.
00116     * @param returns 0 for no errors during i2c communication.  Any other number is just a i2c communication failure of some kind!
00117     */
00118     int readTemperature(long *t);       // Get the temperature reading that was taken in startTemperature() but ensure 4.5 ms time has elapsed
00119 
00120     /** Start the pressure reading process but return after the commands are issued to BMP180
00121     *
00122     * This function is ment to start the pressure reading process but will return to allow other code to run then a reading could be made at a later time.
00123     *   Note the time needed for this reading pressure process will depend on oversample setting.  The maximum time is 25.5 ms and minimum time is 4.5 ms.
00124     *
00125     * @param oversample is the method for reading sensor.  OVERSAMPLING_ULTRA_HIGH_RESOLUTION is used if an incorrect value is passed to this function.
00126     * @param returns 0 for no errors during i2c communication.  Any other number is just a i2c communication failure of some kind!
00127     */
00128     int startPressure(int oversample);  // Start pressure measurement!  Note oversample will vary the time to complete this measurement. See defines above for oversampling constants to use!
00129 
00130     /** Reads the last barometric pressure reading that was started with startPressure() function
00131     *
00132     * This function will return the fully compensated value of the barometric pressure in Pa.
00133     *   Note this function should follow startPressure() after the time needed to read the pressure.  This time will vary but maximum time is 25.5 ms and minimum time is 4.5 ms.
00134     *   Note that this reading is dependent on temperature so the startTemperature() and readTemperature() functions should proceed this function or the pressure value will be incorrect!
00135     *
00136     * @param p the barometric pressure fully compensated value is returned in this variable. Pressure is in Pa so 88007 is 88.007 kPa.
00137     * @param returns 0 for no errors during i2c communication.  Any other number is just a i2c communication failure of some kind!
00138     */
00139     int readPressure(long *p);          // Get the pressure reading that was taken in startPressure() but ensure time for the measurement to complete
00140 
00141 protected:
00142     long x1;
00143     long x2;
00144     long x3;
00145     short ac1;
00146     short ac2;
00147     short ac3;
00148     unsigned short ac4;
00149     unsigned short ac5;
00150     unsigned short ac6;
00151     short b1;
00152     short b2;
00153     long b3;
00154     unsigned long b4;
00155     long b5;
00156     long b6;
00157     unsigned long b7;
00158     short mb;
00159     short mc;
00160     short md;
00161     int oversampling_setting;
00162     char rReg[3];
00163     char wReg[2];
00164     char cmd;
00165     char data[EEprom];
00166     char w[2];
00167 
00168     I2C bmp180i2c;              // the I2C class for this bmp180 communication.
00169     int oversampleCheck(int oversample);
00170 };
00171 
00172 #endif