Skip to content

Renamed STATUS to MPLSTATUS because of incompatibility with ... #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions src/SparkFunMPL3115A2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

This library allows an Arduino to read from the MPL3115A2 low-cost high-precision pressure sensor.

If you have feature suggestions or need support please use the github support page: https://github.com/sparkfun/MPL3115A2_Breakout

Hardware Setup: The MPL3115A2 lives on the I2C bus. Attach the SDA pin to A4, SCL to A5. Use inline 10k resistors
if you have a 5V board. If you are using the SparkFun breakout board you *do not* need 4.7k pull-up resistors
if you have a 5V board. If you are using the SparkFun breakout board you *do not* need 4.7k pull-up resistors
on the bus (they are built-in).

Link to the breakout board product:

Software:
.begin() Gets sensor on the I2C bus.
.readAltitude() Returns float with meters above sealevel. Ex: 1638.94
Expand All @@ -28,9 +28,9 @@
.setModeActive() Start taking measurements!
.setOversampleRate(byte) Sets the # of samples from 1 to 128. See datasheet.
.enableEventFlags() Sets the fundamental event flags. Required during setup.

Updated by PaulZC: October 19th, 2019

*/

#include <Wire.h>
Expand Down Expand Up @@ -61,7 +61,7 @@ float MPL3115A2::readAltitude()

//Wait for PDR bit, indicates we have new pressure data
int counter = 0;
while( (IIC_Read(STATUS) & (1<<1)) == 0)
while( (IIC_Read(MPLSTATUS) & (1<<1)) == 0)
{
if(++counter > 600) return(-999); //Error out after max of 512ms for a read
delay(1);
Expand All @@ -82,8 +82,8 @@ float MPL3115A2::readAltitude()

// The least significant bytes l_altitude and l_temp are 4-bit,
// fractional values, so you must cast the calulation in (float),
// shift the value over 4 spots to the right and divide by 16 (since
// there are 16 values in 4-bits).
// shift the value over 4 spots to the right and divide by 16 (since
// there are 16 values in 4-bits).
float tempcsb = (lsb>>4)/16.0;

float altitude = (float)( (msb << 8) | csb) + tempcsb;
Expand All @@ -103,11 +103,11 @@ float MPL3115A2::readAltitudeFt()
float MPL3115A2::readPressure()
{
//Check PDR bit, if it's not set then toggle OST
if(IIC_Read(STATUS) & (1<<2) == 0) toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading
if(IIC_Read(MPLSTATUS) & (1<<2) == 0) toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading

//Wait for PDR bit, indicates we have new pressure data
int counter = 0;
while(IIC_Read(STATUS) & (1<<2) == 0)
while(IIC_Read(MPLSTATUS) & (1<<2) == 0)
{
if(++counter > 600) return(-999); //Error out after max of 512ms for a read
delay(1);
Expand All @@ -125,7 +125,7 @@ float MPL3115A2::readPressure()
msb = _i2cPort->read();
csb = _i2cPort->read();
lsb = _i2cPort->read();

toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading

// Pressure comes back as a left shifted 20 bit number
Expand All @@ -143,11 +143,11 @@ float MPL3115A2::readPressure()

float MPL3115A2::readTemp()
{
if(IIC_Read(STATUS) & (1<<1) == 0) toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading
if(IIC_Read(MPLSTATUS) & (1<<1) == 0) toggleOneShot(); //Toggle the OST bit causing the sensor to immediately take another reading

//Wait for TDR bit, indicates we have new temp data
int counter = 0;
while( (IIC_Read(STATUS) & (1<<1)) == 0)
while( (IIC_Read(MPLSTATUS) & (1<<1)) == 0)
{
if(++counter > 600) return(-999); //Error out after max of 512ms for a read
delay(1);
Expand Down Expand Up @@ -176,20 +176,20 @@ float MPL3115A2::readTemp()
{
foo = ~((msb << 8) + lsb) + 1; //2’s complement
msb = foo >> 8;
lsb = foo & 0x00F0;
lsb = foo & 0x00F0;
negSign = true;
}

// The least significant bytes l_altitude and l_temp are 4-bit,
// fractional values, so you must cast the calulation in (float),
// shift the value over 4 spots to the right and divide by 16 (since
// there are 16 values in 4-bits).
// shift the value over 4 spots to the right and divide by 16 (since
// there are 16 values in 4-bits).
float templsb = (lsb>>4)/16.0; //temp, fraction of a degree

float temperature = (float)(msb + templsb);

if (negSign) temperature = 0 - temperature;

return(temperature);
}

Expand Down Expand Up @@ -236,14 +236,14 @@ void MPL3115A2::setModeActive()
}

//Call with a rate from 0 to 7. See page 33 for table of ratios.
//Sets the over sample rate. Datasheet calls for 128 but you can set it
//Sets the over sample rate. Datasheet calls for 128 but you can set it
//from 1 to 128 samples. The higher the oversample rate the greater
//the time between data samples.
void MPL3115A2::setOversampleRate(byte sampleRate)
{
if(sampleRate > 7) sampleRate = 7; //OS cannot be larger than 0b.0111
sampleRate <<= 3; //Align it for the CTRL_REG1 register

byte tempSetting = IIC_Read(CTRL_REG1); //Read current settings
tempSetting &= 0xc7; // B11000111; //Clear out old OS bits
tempSetting |= sampleRate; //Mask in new OS bits
Expand All @@ -254,7 +254,7 @@ void MPL3115A2::setOversampleRate(byte sampleRate)
//test against them. This is recommended in datasheet during setup.
void MPL3115A2::enableEventFlags()
{
IIC_Write(PT_DATA_CFG, 0x07); // Enable all three pressure and temp event flags
IIC_Write(PT_DATA_CFG, 0x07); // Enable all three pressure and temp event flags
}

//Clears then sets the OST bit which causes the sensor to immediately take another reading
Expand Down Expand Up @@ -290,4 +290,3 @@ void MPL3115A2::IIC_Write(byte regAddr, byte value)
_i2cPort->write(value);
_i2cPort->endTransmission(true);
}

18 changes: 9 additions & 9 deletions src/SparkFunMPL3115A2.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
/*
MPL3115A2 Barometric Pressure Sensor Library
By: Nathan Seidle
SparkFun Electronics
Date: September 24th, 2013
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

Get pressure, altitude and temperature from the MPL3115A2 sensor.

Updated by PaulZC: October 19th, 2019

*/
#ifndef _SPARKFUN_MPL3115A2_H_
#define _SPARKFUN_MPL3115A2_H_

#ifndef _SPARKFUN_MPL3115A2_H_
#define _SPARKFUN_MPL3115A2_H_

#include <Arduino.h>

Expand All @@ -21,7 +21,7 @@
// Define MPL3115A2 registers
enum mpl3115a2_regs
{
STATUS = 0x00,
MPLSTATUS = 0x00,
OUT_P_MSB = 0x01,
OUT_P_CSB = 0x02,
OUT_P_LSB = 0x03,
Expand Down Expand Up @@ -66,7 +66,7 @@ enum mpl3115a2_regs
CTRL_REG5 = 0x2A,
OFF_P = 0x2B,
OFF_T = 0x2C,
OFF_H = 0x2D
OFF_H = 0x2D
};


Expand Down