Library to wrapper most of the functions on the MPL3115A2 pressure and temperature sensor.

Dependents:   WeatherBalloon4180 WeatherBalloon4180 mbed_rifletool Smart_Watch_4180_Final_Design ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPL3115A2.h Source File

MPL3115A2.h

00001 /*
00002     MPL3115A2 Barometric Pressure and Tempurature Sensor Library
00003     By: Michael Lange
00004     Date: March 31, 2014
00005     License: This code is public domain.
00006  
00007     This class wraps the functions of the MPL3115A2 sensor into 
00008     a usable class that exposes most functions to your project.
00009     Functions that are not exposed can easily be added using the
00010     existing functions in this library. Specifically, I did not
00011     add any funtions to handle FIFO logging or the use of either
00012     of the pin interrupts. This should not be too difficult to
00013     add if you need those features.
00014     
00015     The motivation here was to get a set of support classes 
00016     together that supported the chip and could be expanded on.
00017     With this library you can extract all relevant data from the
00018     sensor.
00019     
00020     Be sure to download the DATASHEET and the App Note AN4519.
00021     
00022     This library was created using the mbed NXP LPC11U24. Pins
00023     p27 and p28 were used for the I2C functions. Be sure to install
00024     1K pull-up resistors on both lines. Also, if you're not using
00025     the SparkFun breakout board, be sure to use the right caps on 
00026     the power pin. If you don't, the jitter can cause problems.
00027     
00028     This library was inspired by the similar library available for
00029     the Arduino written by Nathan Seidle at SparkFun. I copied
00030     some of the number crunching routines and tried to follow his
00031     style of coding for the library. That way users of Arduinos
00032     could step into this library a little easier.
00033  
00034  */
00035  
00036  
00037 #ifndef MPL3115A2_H
00038 #define MPL3115A2_H
00039 
00040 #include "mbed.h"
00041 
00042 #include "Altitude.h"               // Our classes to handle compressed data from the sensor.
00043 #include "Temperature.h"
00044 #include "Pressure.h"
00045 
00046 #define MPL3115A2_ADDRESS   0xC0    // Shifted 7-bit I2C address for sensor
00047 
00048 #define READ_ACK 1                  // For mbed I2C Read method.
00049 #define READ_NAK 0 
00050 
00051 #define MAX_DATA_READY_ATTEMPTS 512 // How many times we loop waiting for data before giving up.
00052 
00053 //      DEFINE             |REGISTER |RESET |RESET    |TYPE |AUTO-INC      |NAME/COMMENT
00054 //                         |         |      |STBY2ACT |     |ADDRESS       |
00055 #define STATUS              0x00 //  | 0x00 | Yes     | R   | 0x01         | Sensor Status Register (Alias for DR_STATUS or F_STATUS)
00056 #define OUT_P_MSB           0x01 //  | 0x00 | Yes     | R   | 0x02 | 0x01  | Pressure Data Out MSB (Bits 12-19 of 20-bit real-time Pressure sample | Root pointer t oPressure and Tempurature FIFO data)
00057 #define OUT_P_CSB           0x02 //  | 0x00 | Yes     | R   | 0x03         | Pressure Data out CSB (Bits 0-3 of 20-bit real-time Pressure sample)
00058 #define OUT_P_LSB           0x03 //  | 0x00 |         |     | 0x           |
00059 #define OUT_T_MSB           0x04 //  | 0x00 |         |     | 0x           |
00060 #define OUT_T_LSB           0x05 //  | 0x00 |         |     | 0x           |
00061 #define DR_STATUS           0x06 //  | 0x00 |         |     | 0x           |
00062 #define OUT_P_DELTA_MSB     0x07 //  | 0x00 |         |     | 0x           |
00063 #define OUT_P_DELTA_CSB     0x08 //  | 0x00 |         |     | 0x           |
00064 #define OUT_P_DELTA_LSB     0x09 //  | 0x00 |         |     | 0x           |
00065 #define OUT_T_DELTA_MSB     0x0A //  | 0x00 |         |     | 0x           |
00066 #define OUT_T_DELTA_LSB     0x0B //  | 0x00 |         |     | 0x           |
00067 #define WHO_AM_I            0x0C //  | 0xC4 |         |     | 0x           |
00068 #define F_STATUS            0x0D //  | 0x00 |         |     | 0x           |
00069 #define F_DATA              0x0E //  | 0x00 |         |     | 0x           |
00070 #define F_SETUP             0x0F //  | 0x00 |         |     | 0x           |
00071 #define TIME_DLY            0x10 //  | 0x00 |         |     | 0x           |
00072 #define SYSMOD              0x11 //  | 0x00 |         |     | 0x           |
00073 #define INT_SOURCE          0x12 //  | 0x00 |         |     | 0x           |
00074 #define PT_DATA_CFG         0x13 //  | 0x00 |         |     | 0x           |
00075 #define BAR_IN_MSB          0x14 //  | 0xC5 |         |     | 0x           |
00076 #define BAR_IN_LSB          0x15 //  | 0xE7 |         |     | 0x           |
00077 #define P_TGT_MSB           0x16 //  | 0x00 |         |     | 0x           |
00078 #define P_TGT_LSB           0x17 //  | 0x00 |         |     | 0x           |
00079 #define T_TGT               0x18 //  | 0x00 |         |     | 0x           |
00080 #define P_WND_MSB           0x19 //  | 0x00 |         |     | 0x           |
00081 #define P_WND_LSB           0x1A //  | 0x00 |         |     | 0x           |
00082 #define T_WND               0x1B //  | 0x00 |         |     | 0x           |
00083 #define P_MIN_MSB           0x1C //  | 0x00 |         |     | 0x           |
00084 #define P_MIN_CSB           0x1D //  | 0x00 |         |     | 0x           |
00085 #define P_MIN_LSB           0x1E //  | 0x00 |         |     | 0x           |
00086 #define T_MIN_MSB           0x1F //  | 0x00 |         |     | 0x           |
00087 #define T_MIN_LSB           0x20 //  | 0x00 |         |     | 0x           |
00088 #define P_MAX_MSB           0x21 //  | 0x00 |         |     | 0x           |
00089 #define P_MAX_CSB           0x22 //  | 0x00 |         |     | 0x           |
00090 #define P_MAX_LSB           0x23 //  | 0x00 |         |     | 0x           |
00091 #define T_MAX_MSB           0x24 //  | 0x00 |         |     | 0x           |
00092 #define T_MAX_LSB           0x25 //  | 0x00 |         |     | 0x           |
00093 #define CTRL_REG1           0x26 //  | 0x00 |         |     | 0x           |
00094 #define CTRL_REG2           0x27 //  | 0x00 |         |     | 0x           |
00095 #define CTRL_REG3           0x28 //  | 0x00 |         |     | 0x           |
00096 #define CTRL_REG4           0x29 //  | 0x00 |         |     | 0x           |
00097 #define CTRL_REG5           0x2A //  | 0x00 |         |     | 0x           |
00098 #define OFF_P               0x2B //  | 0x00 |         |     | 0x           |
00099 #define OFF_T               0x2C //  | 0x00 |         |     | 0x           |
00100 #define OFF_H               0x2D //  | 0x00 |         |     | 0x           |
00101 
00102 //! MPL3115A2 I2C Barometric Pressure and Tempurature Sensor Library
00103 //! This class wraps most of the function in the MPL3115A2 sensor leaving out the FIFO and interrupt system.
00104 class MPL3115A2
00105 {
00106 public:
00107     //! Constructs an MPL3115A2 object and associates an I2C and optional Serial debug object.
00108     //! @param *i2c The I2C object to use for the sensor.
00109     //! @param *pc An optional serial debug connection object.
00110     MPL3115A2(I2C *i2c, Serial *pc = NULL);
00111 
00112     //! Initializes the sensor, defaulting to Altitude mode. This should be called before using
00113     //! the sensor for the first time.
00114     void init();
00115 
00116     //! Queries the value from the WHO_AM_I register (usually equal to 0xC4).
00117     //! @return The fixed device ID from the sensor.
00118     char  whoAmI() { return i2cRead(WHO_AM_I); } 
00119     
00120     //! Reads Altitude data from the sensor and returns it in the Altitude object passed in. If
00121     //! no data could be read, the Altitude object is left as is.
00122     //! @param a A pointer to an Altitude object that will receive the sensor data.
00123     //! @returns The Altitude pointer that was passed in.
00124     Altitude* readAltitude(Altitude* a);            
00125     //! Reads Pressure data from the sensor and returns it in the Pressure object passed in. If
00126     //! no data could be read, the Pressure object is left as is.
00127     //! @param a A pointer to a Pressure object that will receive the sensor data.
00128     //! @returns The Pressure pointer that was passed in.
00129     Pressure* readPressure(Pressure* p);            
00130     //! Reads Temperature data from the sensor and returns it in the Temperature object passed in. If
00131     //! no data could be read, the Temperature object is left as is.
00132     //! @param a A pointer to an Temperature object that will receive the sensor data.
00133     //! @returns The Temperature pointer that was passed in.
00134     Temperature* readTemperature(Temperature* t);
00135     
00136     // Use these methods to set the sensor's offsets to increase its accuracy. You can generally
00137     // find your current altitude with a smart phone or GPS. Same goes for the temperature. For
00138     // the current pressure where you are, you may need an accurate weather station. Getting the
00139     // pressure from the web for your area is generally not close enough to help with calibration.
00140     // You may need to play with the setting to achieve good accuracy. I found the offset steps
00141     // were not 100% accurate to the datasheet and had to adjust accordingly. 
00142     
00143     //! Returns the altitude offset stored in the sensor.
00144     char offsetAltitude() { return i2cRead(OFF_H); }
00145     //! Sets the altitude offset stored in the sensor. The allowed offset range is from -128 to 127 meters.
00146     void setOffsetAltitude(const char offset) { i2cWrite(OFF_H, offset); } 
00147     //! Returns the pressure offset stored in the sensor.
00148     char offsetPressure() { return i2cRead(OFF_P); }
00149     //! Sets the pressure offset stored in the sensor. The allowed offset range is from -128 to 127 where each LSB represents 4 Pa.
00150     void setOffsetPressure(const char offset) { i2cWrite(OFF_P, offset); }
00151     //! Returns the temperature offset stored in the sensor.
00152     char offsetTemperature() { return i2cRead(OFF_T); }
00153     //! Sets the temperature offset stored in the sensor. The allowed offset range is from -128 to 127 where each LSB represents 0.0625ºC.
00154     void setOffsetTemperature(const char offset) { i2cWrite(OFF_T, offset); } 
00155     
00156     //! Puts the sensor into Standby mode. Required when using methods below.
00157     void  setModeStandby(); 
00158     //! Activates the sensor to start taking measurements.
00159     void  setModeActive();  
00160     
00161     //! Puts the sensor into barometric mode, be sure to put the sensor in standby mode first.
00162     void  setModeBarometer();           
00163     //! Puts the sensor into altimeter mode, be sure to put the sensor in standby mode first.
00164     void  setModeAltimeter();           
00165     //! Sets the number of samples from 1 to 128, be sure to put the sensor in standby mode first.
00166     void  setOversampleRate(char rate); 
00167     //! Sets all the event flags, be sure to put the sensor in standby mode first.
00168     void  enableEventFlags();           
00169 
00170 private:
00171     //! The I2C object we use to communicate with the sensor. It is not part
00172     //! of the class so that it can be shared with other peripherals on the 
00173     //! bus.
00174     I2C *_i2c;          
00175                         
00176     //! Set this in the constructor if you want the class to output debug messages.                        
00177     //! If you need to pair down your code, you can remove this and all the
00178     //! references to it in the code.
00179     Serial *_debug;     
00180  
00181     //! Debug method that mimics the printf function, but will output nothing if _debug has not
00182     //! been set. This means you can safely us it in your code and nothing will happen if you don't
00183     //! assign the _debug object.
00184     void debugOut(const char * format, ...);
00185 
00186     //! This helper function is used to CLEAR bits. The mask should contain 1s in the position
00187     //! where the bits need to be cleared. One or more bits can be cleared this way.
00188     void clearRegisterBit(const char regAddr, const char bitMask);
00189     //! This helper function is used to SET bits. The mask should contain 1s in the position
00190     //! where the bits need to be set. One or more bits can be set this way.
00191     void setRegisterBit(const char regAddr, const char bitMask);
00192 
00193     //! Helper functions to check if data is ready in a register for either temperature or pressure.
00194     //! The mask passed in determines which register bit to look at.
00195     int dataReady(const char mask);
00196     //! Blocks for about 1/2 a second while checking the data ready bit. Returns 0 on sucees.
00197     //! This function works for both altitude and barometric pressure depending on the mode the sensor is in.
00198     int pressureDataReady() { return dataReady(0x04); }
00199     //! Blocks for about 1/2 a second while checking the data ready bit. Returns 0 on sucees.
00200     int temperatureDataReady() { return dataReady(0x02); }
00201  
00202     //! Called to force the sensor to take another sample
00203     void toggleOneShot();                       
00204     
00205     //! Helper functions to read one value from the I2C bus using the sensor's address.
00206     char i2cRead(char regAddr);
00207     //! Helper functions to write one value from the I2C bus using the sensor's address.
00208     void i2cWrite(char regAddr, char value);
00209 };
00210 
00211 #endif // MPL3115A2_H