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

MPL3115A2 Precision Altimeter

This class wraps the functions of the MPL3115A2 sensor into a usable class that exposes most functions to your project. Functions that are not exposed can easily be added using the existing functions in this library. Specifically, I did not add any functions to handle FIFO logging or the use of either of the pin interrupts. This should not be too difficult to add if you need those features.

The motivation here was to get a set of support classes together that supported the chip and could be expanded on. With this library you can extract all relevant data from the sensor.

Be sure to download the DATASHEET and the App Note AN4519.

This library was created using the mbed NXP LPC11U24. Pins p27 and p28 were used for the I2C functions. Be sure to install 1K pull-up resistors on both lines. Also, if you're not using the SparkFun breakout board, be sure to use the right caps on the power pin. If you don't, the jitter can cause problems.

This library was inspired by the similar library available for the Arduino written by Nathan Seidle at SparkFun. I copied some of the number crunching routines and tried to follow his style of coding for the library. That way users of Arduinos could step into this library a little easier.

Below is some sample code that outputs the sensor data to a serial debug terminal window. If you have a logic analyzer, like the Saleae Logic, then you might not need the debug terminal window. If you still need the serial driver for your mbed, you can get it here.

Sample Code

#include "mbed.h"
#include "MPL3115A2.h"

I2C i2c(p28, p27);       // sda, scl

// Comment out all of the references to 'pc' on this page if you don't have the 
// serial debug driver for your mbed board installed on your computer. If you do,
// I personally like to use Putty as the terminal window to capture debug messages.
Serial pc(USBTX, USBRX); // tx, rx

// Again, remove the '&pc' parameter is you're not debugging.
MPL3115A2 sensor(&i2c, &pc);

DigitalOut myled(LED1);     // Sanity check to make sure the program is working.
DigitalOut powerPin(p21);   // <-- I powered the sensor from a pin. You don't have to.

int main() {
    
    powerPin = 1;
    wait_ms(300);

    pc.printf("** MPL3115A2 SENSOR **\r\n");

    sensor.init();

    pc.printf("Who Am I: 0x%X\r\n", sensor.whoAmI());

    Altitude a;
    Temperature t;
    Pressure p;
    
    // Offsets for Dacula, GA
    sensor.setOffsetAltitude(83);
    sensor.setOffsetTemperature(20);
    sensor.setOffsetPressure(-32);
    
    while(1) 
    {
        sensor.readAltitude(&a);
        sensor.readTemperature(&t);
        
        sensor.setModeStandby();
        sensor.setModeBarometer();
        sensor.setModeActive();
        sensor.readPressure(&p);
        
        pc.printf("Altitude: %sft, Temp: %sºF, Pressure: %sPa\r\n", a.print(), t.print(), p.print());
        pc.printf("OFF_H: 0x%X, OFF_T: 0x%X, OFF_P: 0x%X\r\n", sensor.offsetAltitude(), sensor.offsetTemperature(), sensor.offsetPressure());
    
        myled = 1;
        wait(5);
        myled = 0;
        wait(5);

        sensor.setModeStandby();
        sensor.setModeAltimeter();
        sensor.setModeActive();
    }
}
Committer:
sophtware
Date:
Wed Apr 02 12:22:45 2014 +0000
Revision:
2:2ebc9c0d4a54
Parent:
1:a011ae93a350
Child:
3:7c7c1ea6fc33
Updated documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sophtware 0:beb43bc3d6d4 1 /*
sophtware 0:beb43bc3d6d4 2 MPL3115A2 Barometric Pressure and Tempurature Sensor Library
sophtware 0:beb43bc3d6d4 3 By: Michael Lange
sophtware 0:beb43bc3d6d4 4 Date: March 31, 2014
sophtware 0:beb43bc3d6d4 5 License: This code is public domain.
sophtware 0:beb43bc3d6d4 6
sophtware 0:beb43bc3d6d4 7 This class wraps the functions of the MPL3115A2 sensor into
sophtware 0:beb43bc3d6d4 8 a usable class that exposes most functions to your project.
sophtware 0:beb43bc3d6d4 9 Functions that are not exposed can easily be added using the
sophtware 0:beb43bc3d6d4 10 existing functions in this library. Specifically, I did not
sophtware 0:beb43bc3d6d4 11 add any funtions to handle FIFO logging or the use of either
sophtware 0:beb43bc3d6d4 12 of the pin interrupts. This should not be too difficult to
sophtware 0:beb43bc3d6d4 13 add if you need those features.
sophtware 0:beb43bc3d6d4 14
sophtware 0:beb43bc3d6d4 15 The motivation here was to get a set of support classes
sophtware 0:beb43bc3d6d4 16 together that supported the chip and could be expanded on.
sophtware 0:beb43bc3d6d4 17 With this library you can extract all relevant data from the
sophtware 0:beb43bc3d6d4 18 sensor.
sophtware 0:beb43bc3d6d4 19
sophtware 0:beb43bc3d6d4 20 Be sure to download the DATASHEET and the App Note AN4519.
sophtware 0:beb43bc3d6d4 21
sophtware 1:a011ae93a350 22 This library was created using the mbed NXP LPC11U24. Pins
sophtware 0:beb43bc3d6d4 23 p27 and p28 were used for the I2C functions. Be sure to install
sophtware 0:beb43bc3d6d4 24 1K pull-up resistors on both lines. Also, if you're not using
sophtware 0:beb43bc3d6d4 25 the SparkFun breakout board, be sure to use the right caps on
sophtware 0:beb43bc3d6d4 26 the power pin. If you don't, the jitter can cause problems.
sophtware 0:beb43bc3d6d4 27
sophtware 0:beb43bc3d6d4 28 This library was inspired by the similar library available for
sophtware 0:beb43bc3d6d4 29 the Arduino written by Nathan Seidle at SparkFun. I copied
sophtware 0:beb43bc3d6d4 30 some of the number crunching routines and tried to follow his
sophtware 0:beb43bc3d6d4 31 style of coding for the library. That way users of Arduinos
sophtware 1:a011ae93a350 32 could step into this library a little easier.
sophtware 0:beb43bc3d6d4 33
sophtware 0:beb43bc3d6d4 34 */
sophtware 0:beb43bc3d6d4 35
sophtware 0:beb43bc3d6d4 36
sophtware 0:beb43bc3d6d4 37 #ifndef MPL3115A2_H
sophtware 0:beb43bc3d6d4 38 #define MPL3115A2_H
sophtware 0:beb43bc3d6d4 39
sophtware 0:beb43bc3d6d4 40 #include "mbed.h"
sophtware 0:beb43bc3d6d4 41
sophtware 0:beb43bc3d6d4 42 #include "Altitude.h" // Our classes to handle compressed data from the sensor.
sophtware 0:beb43bc3d6d4 43 #include "Temperature.h"
sophtware 0:beb43bc3d6d4 44 #include "Pressure.h"
sophtware 0:beb43bc3d6d4 45
sophtware 0:beb43bc3d6d4 46 #define MPL3115A2_ADDRESS 0xC0 // Shifted 7-bit I2C address for sensor
sophtware 0:beb43bc3d6d4 47
sophtware 0:beb43bc3d6d4 48 #define READ_ACK 1 // For mbed I2C Read method.
sophtware 0:beb43bc3d6d4 49 #define READ_NAK 0
sophtware 0:beb43bc3d6d4 50
sophtware 0:beb43bc3d6d4 51 #define MAX_DATA_READY_ATTEMPTS 512 // How many times we loop waiting for data before giving up.
sophtware 0:beb43bc3d6d4 52
sophtware 0:beb43bc3d6d4 53 // DEFINE |REGISTER |RESET |RESET |TYPE |AUTO-INC |NAME/COMMENT
sophtware 0:beb43bc3d6d4 54 // | | |STBY2ACT | |ADDRESS |
sophtware 0:beb43bc3d6d4 55 #define STATUS 0x00 // | 0x00 | Yes | R | 0x01 | Sensor Status Register (Alias for DR_STATUS or F_STATUS)
sophtware 0:beb43bc3d6d4 56 #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)
sophtware 0:beb43bc3d6d4 57 #define OUT_P_CSB 0x02 // | 0x00 | Yes | R | 0x03 | Pressure Data out CSB (Bits 0-3 of 20-bit real-time Pressure sample)
sophtware 0:beb43bc3d6d4 58 #define OUT_P_LSB 0x03 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 59 #define OUT_T_MSB 0x04 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 60 #define OUT_T_LSB 0x05 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 61 #define DR_STATUS 0x06 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 62 #define OUT_P_DELTA_MSB 0x07 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 63 #define OUT_P_DELTA_CSB 0x08 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 64 #define OUT_P_DELTA_LSB 0x09 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 65 #define OUT_T_DELTA_MSB 0x0A // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 66 #define OUT_T_DELTA_LSB 0x0B // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 67 #define WHO_AM_I 0x0C // | 0xC4 | | | 0x |
sophtware 0:beb43bc3d6d4 68 #define F_STATUS 0x0D // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 69 #define F_DATA 0x0E // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 70 #define F_SETUP 0x0F // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 71 #define TIME_DLY 0x10 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 72 #define SYSMOD 0x11 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 73 #define INT_SOURCE 0x12 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 74 #define PT_DATA_CFG 0x13 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 75 #define BAR_IN_MSB 0x14 // | 0xC5 | | | 0x |
sophtware 0:beb43bc3d6d4 76 #define BAR_IN_LSB 0x15 // | 0xE7 | | | 0x |
sophtware 0:beb43bc3d6d4 77 #define P_TGT_MSB 0x16 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 78 #define P_TGT_LSB 0x17 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 79 #define T_TGT 0x18 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 80 #define P_WND_MSB 0x19 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 81 #define P_WND_LSB 0x1A // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 82 #define T_WND 0x1B // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 83 #define P_MIN_MSB 0x1C // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 84 #define P_MIN_CSB 0x1D // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 85 #define P_MIN_LSB 0x1E // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 86 #define T_MIN_MSB 0x1F // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 87 #define T_MIN_LSB 0x20 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 88 #define P_MAX_MSB 0x21 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 89 #define P_MAX_CSB 0x22 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 90 #define P_MAX_LSB 0x23 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 91 #define T_MAX_MSB 0x24 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 92 #define T_MAX_LSB 0x25 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 93 #define CTRL_REG1 0x26 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 94 #define CTRL_REG2 0x27 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 95 #define CTRL_REG3 0x28 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 96 #define CTRL_REG4 0x29 // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 97 #define CTRL_REG5 0x2A // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 98 #define OFF_P 0x2B // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 99 #define OFF_T 0x2C // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 100 #define OFF_H 0x2D // | 0x00 | | | 0x |
sophtware 0:beb43bc3d6d4 101
sophtware 2:2ebc9c0d4a54 102 //! MPL3115A2 I2C Barometric Pressure and Tempurature Sensor Library
sophtware 2:2ebc9c0d4a54 103 //! This class wraps most of the function in the MPL3115A2 sensor leaving out the FIFO and interrupt system.
sophtware 0:beb43bc3d6d4 104 class MPL3115A2
sophtware 0:beb43bc3d6d4 105 {
sophtware 0:beb43bc3d6d4 106 public:
sophtware 2:2ebc9c0d4a54 107 //! Constructs an MPL3115A2 object and associates an I2C and optional Serial debug object.
sophtware 0:beb43bc3d6d4 108 MPL3115A2(I2C *i2c, Serial *pc = NULL);
sophtware 0:beb43bc3d6d4 109
sophtware 2:2ebc9c0d4a54 110 //! Call from main to initialize the sensor, defaulting to Altitude mode.
sophtware 0:beb43bc3d6d4 111 void init();
sophtware 0:beb43bc3d6d4 112
sophtware 2:2ebc9c0d4a54 113 //! Returns the fixed device ID number (usually equal to 0xC4).
sophtware 0:beb43bc3d6d4 114 char whoAmI() { return i2cRead(WHO_AM_I); }
sophtware 0:beb43bc3d6d4 115
sophtware 2:2ebc9c0d4a54 116 //! Returns the passed in altitude object with altitude data.
sophtware 2:2ebc9c0d4a54 117 Altitude* readAltitude(Altitude* a);
sophtware 2:2ebc9c0d4a54 118 //! Returns the passed in pressure object with barometric pressure data.
sophtware 2:2ebc9c0d4a54 119 Pressure* readPressure(Pressure* p);
sophtware 2:2ebc9c0d4a54 120 //! Returns the passed in temperature object with temperature data.
sophtware 2:2ebc9c0d4a54 121 Temperature* readTemperature(Temperature* t);
sophtware 0:beb43bc3d6d4 122
sophtware 0:beb43bc3d6d4 123 // Use these methods to set the sensor's offsets to increase its accuracy. You can generally
sophtware 0:beb43bc3d6d4 124 // find your current altitude with a smart phone or GPS. Same goes for the temperature. For
sophtware 0:beb43bc3d6d4 125 // the current pressure where you are, you may need an accurate weather station. Getting the
sophtware 0:beb43bc3d6d4 126 // pressure from the web for your area is generally not close enough to help with calibration.
sophtware 0:beb43bc3d6d4 127 // You may need to play with the setting to achieve good accuracy. I found the offset steps
sophtware 0:beb43bc3d6d4 128 // were not 100% accurate to the datasheet and had to adjust accordingly.
sophtware 2:2ebc9c0d4a54 129 //! Returns the altitude offset stored in the sensor.
sophtware 0:beb43bc3d6d4 130 char offsetAltitude() { return i2cRead(OFF_H); }
sophtware 2:2ebc9c0d4a54 131 //! Sets the altitude offset stored in the sensor. The allowed offset range is from -128 to 127 meters.
sophtware 2:2ebc9c0d4a54 132 void setOffsetAltitude(const char offset) { i2cWrite(OFF_H, offset); }
sophtware 2:2ebc9c0d4a54 133 //! Returns the pressure offset stored in the sensor.
sophtware 0:beb43bc3d6d4 134 char offsetPressure() { return i2cRead(OFF_P); }
sophtware 2:2ebc9c0d4a54 135 //! Sets the pressure offset stored in the sensor. The allowed offset range is from -128 to 127 where each LSB represents 4 Pa.
sophtware 2:2ebc9c0d4a54 136 void setOffsetPressure(const char offset) { i2cWrite(OFF_P, offset); }
sophtware 2:2ebc9c0d4a54 137 //! Returns the temperature offset stored in the sensor.
sophtware 0:beb43bc3d6d4 138 char offsetTemperature() { return i2cRead(OFF_T); }
sophtware 2:2ebc9c0d4a54 139 //! Sets the temperature offset stored in the sensor. The allowed offset range is from -128 to 127 where each LSB represents 0.0625ºC.
sophtware 2:2ebc9c0d4a54 140 void setOffsetTemperature(const char offset) { i2cWrite(OFF_T, offset); }
sophtware 0:beb43bc3d6d4 141
sophtware 2:2ebc9c0d4a54 142 //! Puts the sensor into Standby mode. Required when using methods below.
sophtware 2:2ebc9c0d4a54 143 void setModeStandby();
sophtware 2:2ebc9c0d4a54 144 //! Activates the sensor to start taking measurements.
sophtware 2:2ebc9c0d4a54 145 void setModeActive();
sophtware 0:beb43bc3d6d4 146
sophtware 2:2ebc9c0d4a54 147 //! Puts the sensor into barometric mode, be sure to put the sensor in standby mode first.
sophtware 2:2ebc9c0d4a54 148 void setModeBarometer();
sophtware 2:2ebc9c0d4a54 149 //! Puts the sensor into altimeter mode, be sure to put the sensor in standby mode first.
sophtware 2:2ebc9c0d4a54 150 void setModeAltimeter();
sophtware 2:2ebc9c0d4a54 151 //! Sets the number of samples from 1 to 128, be sure to put the sensor in standby mode first.
sophtware 2:2ebc9c0d4a54 152 void setOversampleRate(char rate);
sophtware 2:2ebc9c0d4a54 153 //! Sets all the event flags, be sure to put the sensor in standby mode first.
sophtware 2:2ebc9c0d4a54 154 void enableEventFlags();
sophtware 0:beb43bc3d6d4 155
sophtware 0:beb43bc3d6d4 156 private:
sophtware 2:2ebc9c0d4a54 157 //! The I2C object we use to communicate with the sensor. It is not part
sophtware 2:2ebc9c0d4a54 158 //! of the class so that it can be shared with other peripherals on the
sophtware 2:2ebc9c0d4a54 159 //! bus.
sophtware 2:2ebc9c0d4a54 160 I2C *_i2c;
sophtware 2:2ebc9c0d4a54 161
sophtware 2:2ebc9c0d4a54 162 //! Set this in the constructor if you want the class to output debug messages.
sophtware 2:2ebc9c0d4a54 163 //! If you need to pair down your code, you can remove this and all the
sophtware 2:2ebc9c0d4a54 164 //! references to it in the code.
sophtware 2:2ebc9c0d4a54 165 Serial *_debug;
sophtware 0:beb43bc3d6d4 166
sophtware 2:2ebc9c0d4a54 167 //! Debug method that mimics the printf function, but will output nothing if _debug has not
sophtware 2:2ebc9c0d4a54 168 //! been set. This means you can safely us it in your code and nothing will happen if you don't
sophtware 2:2ebc9c0d4a54 169 //! assign the _debug object.
sophtware 0:beb43bc3d6d4 170 void debugOut(const char * format, ...);
sophtware 0:beb43bc3d6d4 171
sophtware 2:2ebc9c0d4a54 172 //! This helper function is used to CLEAR bits. The mask should contain 1s in the position
sophtware 2:2ebc9c0d4a54 173 //! where the bits need to be cleared. One or more bits can be cleared this way.
sophtware 0:beb43bc3d6d4 174 void clearRegisterBit(const char regAddr, const char bitMask);
sophtware 2:2ebc9c0d4a54 175 //! This helper function is used to SET bits. The mask should contain 1s in the position
sophtware 2:2ebc9c0d4a54 176 //! where the bits need to be set. One or more bits can be set this way.
sophtware 0:beb43bc3d6d4 177 void setRegisterBit(const char regAddr, const char bitMask);
sophtware 0:beb43bc3d6d4 178
sophtware 2:2ebc9c0d4a54 179 //! Helper functions to check if data is ready in a register for either temperature or pressure.
sophtware 2:2ebc9c0d4a54 180 //! The mask passed in determines which register bit to look at.
sophtware 0:beb43bc3d6d4 181 int dataReady(const char mask);
sophtware 2:2ebc9c0d4a54 182 //! Blocks for about 1/2 a second while checking the data ready bit. Returns 0 on sucees.
sophtware 2:2ebc9c0d4a54 183 //! This function works for both altitude and barometric pressure depending on the mode the sensor is in.
sophtware 0:beb43bc3d6d4 184 int pressureDataReady() { return dataReady(0x04); }
sophtware 2:2ebc9c0d4a54 185 //! Blocks for about 1/2 a second while checking the data ready bit. Returns 0 on sucees.
sophtware 0:beb43bc3d6d4 186 int temperatureDataReady() { return dataReady(0x02); }
sophtware 0:beb43bc3d6d4 187
sophtware 2:2ebc9c0d4a54 188 //! Called to force the sensor to take another sample
sophtware 0:beb43bc3d6d4 189 void toggleOneShot();
sophtware 0:beb43bc3d6d4 190
sophtware 2:2ebc9c0d4a54 191 //! Helper functions to read one value from the I2C bus using the sensor's address.
sophtware 0:beb43bc3d6d4 192 char i2cRead(char regAddr);
sophtware 2:2ebc9c0d4a54 193 //! Helper functions to write one value from the I2C bus using the sensor's address.
sophtware 0:beb43bc3d6d4 194 void i2cWrite(char regAddr, char value);
sophtware 0:beb43bc3d6d4 195 };
sophtware 0:beb43bc3d6d4 196
sophtware 0:beb43bc3d6d4 197 #endif // MPL3115A2_H