Skip to content

Commit 7058a5a

Browse files
authored
Merge pull request #3 from PaulZC/Artemis_Updates
Updates for Artemis
2 parents a0d7df7 + e2fcc16 commit 7058a5a

File tree

3 files changed

+173
-89
lines changed

3 files changed

+173
-89
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
MPL3115A2 Barometric Pressure Sensor Library Example Code
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: September 24th, 2013
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
Uses the MPL3115A2 library to display the current altitude and temperature
9+
10+
This example shows how to connect using alternate I2C ports on the Artemis. Enjoy! PaulZC 19/10/2019
11+
12+
Available functions:
13+
.begin() Gets sensor on the I2C bus.
14+
.readAltitude() Returns float with meters above sealevel. Ex: 1638.94
15+
.readAltitudeFt() Returns float with feet above sealevel. Ex: 5376.68
16+
.readPressure() Returns float with barometric pressure in Pa. Ex: 83351.25
17+
.readTemp() Returns float with current temperature in Celsius. Ex: 23.37
18+
.readTempF() Returns float with current temperature in Fahrenheit. Ex: 73.96
19+
.setModeBarometer() Puts the sensor into Pascal measurement mode.
20+
.setModeAltimeter() Puts the sensor into altimetery mode.
21+
.setModeStandy() Puts the sensor into Standby mode. Required when changing CTRL1 register.
22+
.setModeActive() Start taking measurements!
23+
.setOversampleRate(byte) Sets the # of samples from 1 to 128. See datasheet.
24+
.enableEventFlags() Sets the fundamental event flags. Required during setup.
25+
26+
*/
27+
28+
#include <Wire.h>
29+
#include "SparkFunMPL3115A2.h"
30+
31+
//Define the Artemis TwoWire port for the MPL3115A2
32+
//See the Wire/Example2_MoreI2CPorts example for more details
33+
TwoWire myWire(3); //Will use Artemis pads 42/43, SCL1/SDA1 on the Artemis Thing Plus
34+
35+
//Create an instance of the object
36+
MPL3115A2 myPressure;
37+
38+
void setup()
39+
{
40+
myWire.begin(); // Join i2c bus
41+
Serial.begin(9600); // Start serial for output
42+
43+
myPressure.begin(myWire); // Get sensor online
44+
45+
// Configure the sensor
46+
//myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
47+
myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
48+
49+
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
50+
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
51+
}
52+
53+
void loop()
54+
{
55+
/*float altitude = myPressure.readAltitude();
56+
Serial.print("Altitude(m):");
57+
Serial.print(altitude, 2);
58+
59+
altitude = myPressure.readAltitudeFt();
60+
Serial.print(" Altitude(ft):");
61+
Serial.print(altitude, 2);*/
62+
63+
float pressure = myPressure.readPressure();
64+
Serial.print("Pressure(Pa):");
65+
Serial.print(pressure, 2);
66+
67+
//float temperature = myPressure.readTemp();
68+
//Serial.print(" Temp(c):");
69+
//Serial.print(temperature, 2);
70+
71+
float temperature = myPressure.readTempF();
72+
Serial.print(" Temp(f):");
73+
Serial.print(temperature, 2);
74+
75+
Serial.println();
76+
}

src/SparkFunMPL3115A2.cpp

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
.setOversampleRate(byte) Sets the # of samples from 1 to 128. See datasheet.
3030
.enableEventFlags() Sets the fundamental event flags. Required during setup.
3131
32-
*/
32+
Updated by PaulZC: October 19th, 2019
33+
34+
*/
3335

3436
#include <Wire.h>
3537

@@ -43,9 +45,11 @@ MPL3115A2::MPL3115A2()
4345
//Begin
4446
/*******************************************************************************************/
4547
//Start I2C communication
46-
void MPL3115A2::begin(void)
48+
void MPL3115A2::begin(TwoWire &wirePort, uint8_t deviceAddress)
4749
{
48-
Wire.begin();
50+
// Let's assume that Wire.begin(); has been done elsewhere
51+
_i2cPort = &wirePort;
52+
_I2Caddress = deviceAddress;
4953
}
5054

5155

@@ -64,17 +68,17 @@ float MPL3115A2::readAltitude()
6468
}
6569

6670
// Read pressure registers
67-
Wire.beginTransmission(MPL3115A2_ADDRESS);
68-
Wire.write(OUT_P_MSB); // Address of data to get
69-
Wire.endTransmission(false); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
70-
if (Wire.requestFrom(MPL3115A2_ADDRESS, 3) != 3) { // Request three bytes
71+
_i2cPort->beginTransmission(_I2Caddress);
72+
_i2cPort->write(OUT_P_MSB); // Address of data to get
73+
_i2cPort->endTransmission(false); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
74+
if (_i2cPort->requestFrom(_I2Caddress, 3) != 3) { // Request three bytes
7175
return -999;
7276
}
7377

7478
byte msb, csb, lsb;
75-
msb = Wire.read();
76-
csb = Wire.read();
77-
lsb = Wire.read();
79+
msb = _i2cPort->read();
80+
csb = _i2cPort->read();
81+
lsb = _i2cPort->read();
7882

7983
// The least significant bytes l_altitude and l_temp are 4-bit,
8084
// fractional values, so you must cast the calulation in (float),
@@ -110,25 +114,25 @@ float MPL3115A2::readPressure()
110114
}
111115

112116
// Read pressure registers
113-
Wire.beginTransmission(MPL3115A2_ADDRESS);
114-
Wire.write(OUT_P_MSB); // Address of data to get
115-
Wire.endTransmission(false); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
116-
if (Wire.requestFrom(MPL3115A2_ADDRESS, 3) != 3) { // Request three bytes
117+
_i2cPort->beginTransmission(_I2Caddress);
118+
_i2cPort->write(OUT_P_MSB); // Address of data to get
119+
_i2cPort->endTransmission(false); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
120+
if (_i2cPort->requestFrom(_I2Caddress, 3) != 3) { // Request three bytes
117121
return -999;
118122
}
119123

120124
byte msb, csb, lsb;
121-
msb = Wire.read();
122-
csb = Wire.read();
123-
lsb = Wire.read();
125+
msb = _i2cPort->read();
126+
csb = _i2cPort->read();
127+
lsb = _i2cPort->read();
124128

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

127131
// Pressure comes back as a left shifted 20 bit number
128132
long pressure_whole = (long)msb<<16 | (long)csb<<8 | (long)lsb;
129133
pressure_whole >>= 6; //Pressure is an 18 bit number with 2 bits of decimal. Get rid of decimal portion.
130134

131-
lsb &= B00110000; //Bits 5/4 represent the fractional component
135+
lsb &= 0x3F; // B00110000; //Bits 5/4 represent the fractional component
132136
lsb >>= 4; //Get it right aligned
133137
float pressure_decimal = (float)lsb/4.0; //Turn it into fraction
134138

@@ -150,21 +154,21 @@ float MPL3115A2::readTemp()
150154
}
151155

152156
// Read temperature registers
153-
Wire.beginTransmission(MPL3115A2_ADDRESS);
154-
Wire.write(OUT_T_MSB); // Address of data to get
155-
Wire.endTransmission(false); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
156-
if (Wire.requestFrom(MPL3115A2_ADDRESS, 2) != 2) { // Request two bytes
157+
_i2cPort->beginTransmission(_I2Caddress);
158+
_i2cPort->write(OUT_T_MSB); // Address of data to get
159+
_i2cPort->endTransmission(false); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
160+
if (_i2cPort->requestFrom(_I2Caddress, 2) != 2) { // Request two bytes
157161
return -999;
158162
}
159163

160164
byte msb, lsb;
161-
msb = Wire.read();
162-
lsb = Wire.read();
165+
msb = _i2cPort->read();
166+
lsb = _i2cPort->read();
163167

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

166170
//Negative temperature fix by D.D.G.
167-
word foo = 0;
171+
uint16_t foo = 0;
168172
bool negSign = false;
169173

170174
//Check for 2s compliment
@@ -241,7 +245,7 @@ void MPL3115A2::setOversampleRate(byte sampleRate)
241245
sampleRate <<= 3; //Align it for the CTRL_REG1 register
242246

243247
byte tempSetting = IIC_Read(CTRL_REG1); //Read current settings
244-
tempSetting &= B11000111; //Clear out old OS bits
248+
tempSetting &= 0xc7; // B11000111; //Clear out old OS bits
245249
tempSetting |= sampleRate; //Mask in new OS bits
246250
IIC_Write(CTRL_REG1, tempSetting);
247251
}
@@ -271,19 +275,19 @@ void MPL3115A2::toggleOneShot(void)
271275
byte MPL3115A2::IIC_Read(byte regAddr)
272276
{
273277
// This function reads one byte over IIC
274-
Wire.beginTransmission(MPL3115A2_ADDRESS);
275-
Wire.write(regAddr); // Address of CTRL_REG1
276-
Wire.endTransmission(false); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
277-
Wire.requestFrom(MPL3115A2_ADDRESS, 1); // Request the data...
278-
return Wire.read();
278+
_i2cPort->beginTransmission(_I2Caddress);
279+
_i2cPort->write(regAddr); // Address of CTRL_REG1
280+
_i2cPort->endTransmission(false); // Send data to I2C dev with option for a repeated start. THIS IS NECESSARY and not supported before Arduino V1.0.1!
281+
_i2cPort->requestFrom(_I2Caddress, 1); // Request the data...
282+
return _i2cPort->read();
279283
}
280284

281285
void MPL3115A2::IIC_Write(byte regAddr, byte value)
282286
{
283287
// This function writes one byto over IIC
284-
Wire.beginTransmission(MPL3115A2_ADDRESS);
285-
Wire.write(regAddr);
286-
Wire.write(value);
287-
Wire.endTransmission(true);
288+
_i2cPort->beginTransmission(_I2Caddress);
289+
_i2cPort->write(regAddr);
290+
_i2cPort->write(value);
291+
_i2cPort->endTransmission(true);
288292
}
289293

src/SparkFunMPL3115A2.h

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,76 @@
77
88
Get pressure, altitude and temperature from the MPL3115A2 sensor.
99
10+
Updated by PaulZC: October 19th, 2019
11+
1012
*/
1113

1214
#ifndef _SPARKFUN_MPL3115A2_H_
1315
#define _SPARKFUN_MPL3115A2_H_
1416

15-
#if defined(ARDUINO) && ARDUINO >= 100
16-
#include "Arduino.h"
17-
#else
18-
#include "WProgram.h"
19-
#endif
20-
21-
#include <Wire.h>
17+
#include <Arduino.h>
2218

2319
#define MPL3115A2_ADDRESS 0x60 // Unshifted 7-bit I2C address for sensor
2420

25-
#define STATUS 0x00
26-
#define OUT_P_MSB 0x01
27-
#define OUT_P_CSB 0x02
28-
#define OUT_P_LSB 0x03
29-
#define OUT_T_MSB 0x04
30-
#define OUT_T_LSB 0x05
31-
#define DR_STATUS 0x06
32-
#define OUT_P_DELTA_MSB 0x07
33-
#define OUT_P_DELTA_CSB 0x08
34-
#define OUT_P_DELTA_LSB 0x09
35-
#define OUT_T_DELTA_MSB 0x0A
36-
#define OUT_T_DELTA_LSB 0x0B
37-
#define WHO_AM_I 0x0C
38-
#define F_STATUS 0x0D
39-
#define F_DATA 0x0E
40-
#define F_SETUP 0x0F
41-
#define TIME_DLY 0x10
42-
#define SYSMOD 0x11
43-
#define INT_SOURCE 0x12
44-
#define PT_DATA_CFG 0x13
45-
#define BAR_IN_MSB 0x14
46-
#define BAR_IN_LSB 0x15
47-
#define P_TGT_MSB 0x16
48-
#define P_TGT_LSB 0x17
49-
#define T_TGT 0x18
50-
#define P_WND_MSB 0x19
51-
#define P_WND_LSB 0x1A
52-
#define T_WND 0x1B
53-
#define P_MIN_MSB 0x1C
54-
#define P_MIN_CSB 0x1D
55-
#define P_MIN_LSB 0x1E
56-
#define T_MIN_MSB 0x1F
57-
#define T_MIN_LSB 0x20
58-
#define P_MAX_MSB 0x21
59-
#define P_MAX_CSB 0x22
60-
#define P_MAX_LSB 0x23
61-
#define T_MAX_MSB 0x24
62-
#define T_MAX_LSB 0x25
63-
#define CTRL_REG1 0x26
64-
#define CTRL_REG2 0x27
65-
#define CTRL_REG3 0x28
66-
#define CTRL_REG4 0x29
67-
#define CTRL_REG5 0x2A
68-
#define OFF_P 0x2B
69-
#define OFF_T 0x2C
70-
#define OFF_H 0x2D
21+
// Define MPL3115A2 registers
22+
enum mpl3115a2_regs
23+
{
24+
STATUS = 0x00,
25+
OUT_P_MSB = 0x01,
26+
OUT_P_CSB = 0x02,
27+
OUT_P_LSB = 0x03,
28+
OUT_T_MSB = 0x04,
29+
OUT_T_LSB = 0x05,
30+
DR_STATUS = 0x06,
31+
OUT_P_DELTA_MSB = 0x07,
32+
OUT_P_DELTA_CSB = 0x08,
33+
OUT_P_DELTA_LSB = 0x09,
34+
OUT_T_DELTA_MSB = 0x0A,
35+
OUT_T_DELTA_LSB = 0x0B,
36+
WHO_AM_I = 0x0C,
37+
F_STATUS = 0x0D,
38+
F_DATA = 0x0E,
39+
F_SETUP = 0x0F,
40+
TIME_DLY = 0x10,
41+
SYSMOD = 0x11,
42+
INT_SOURCE = 0x12,
43+
PT_DATA_CFG = 0x13,
44+
BAR_IN_MSB = 0x14,
45+
BAR_IN_LSB = 0x15,
46+
P_TGT_MSB = 0x16,
47+
P_TGT_LSB = 0x17,
48+
T_TGT = 0x18,
49+
P_WND_MSB = 0x19,
50+
P_WND_LSB = 0x1A,
51+
T_WND = 0x1B,
52+
P_MIN_MSB = 0x1C,
53+
P_MIN_CSB = 0x1D,
54+
P_MIN_LSB = 0x1E,
55+
T_MIN_MSB = 0x1F,
56+
T_MIN_LSB = 0x20,
57+
P_MAX_MSB = 0x21,
58+
P_MAX_CSB = 0x22,
59+
P_MAX_LSB = 0x23,
60+
T_MAX_MSB = 0x24,
61+
T_MAX_LSB = 0x25,
62+
CTRL_REG1 = 0x26,
63+
CTRL_REG2 = 0x27,
64+
CTRL_REG3 = 0x28,
65+
CTRL_REG4 = 0x29,
66+
CTRL_REG5 = 0x2A,
67+
OFF_P = 0x2B,
68+
OFF_T = 0x2C,
69+
OFF_H = 0x2D
70+
};
71+
7172

7273
class MPL3115A2 {
7374

7475
public:
7576
MPL3115A2();
7677

7778
//Public Functions
78-
void begin(); // Gets sensor on the I2C bus.
79+
void begin(TwoWire &wirePort = Wire, uint8_t deviceAddress = MPL3115A2_ADDRESS); // Gets sensor on the I2C bus.
7980
float readAltitude(); // Returns float with meters above sealevel. Ex: 1638.94
8081
float readAltitudeFt(); // Returns float with feet above sealevel. Ex: 5376.68
8182
float readPressure(); // Returns float with barometric pressure in Pa. Ex: 83351.25
@@ -99,6 +100,9 @@ class MPL3115A2 {
99100

100101
//Private Variables
101102

103+
TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware
104+
uint8_t _I2Caddress = MPL3115A2_ADDRESS; //Default 7-bit unshifted address of the MPL3115A2
105+
102106
};
103107

104108
#endif // End include guard

0 commit comments

Comments
 (0)