-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_MPL3115A2.cpp
153 lines (150 loc) · 5.22 KB
/
Adafruit_MPL3115A2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**************************************************************************/
/*!
EDITED FOR ES2A8 PROJECT - ROCKET INSTRUMENTATION - GROUP C
@file Adafruit_MPL3115A2.cpp
@author K.Townsend (Adafruit Industries)
@license BSD (see license.txt)
Driver for the MPL3115A2 barometric pressure sensor
This is a library for the Adafruit MPL3115A2 breakout
----> https://www.adafruit.com/products/1893
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#ifdef __AVR_ATtiny85__
#include <TinyWireM.h>
#define Wire TinyWireM
#else
#include <Wire.h>
#endif
#include "Adafruit_MPL3115A2.h"
/**************************************************************************/
/*!
@brief Instantiates a new MPL3115A2 class
*/
/**************************************************************************/
Adafruit_MPL3115A2::Adafruit_MPL3115A2() {
}
/**************************************************************************/
/*!
@brief Setups the HW (reads coefficients values, etc.)
*/
/**************************************************************************/
boolean Adafruit_MPL3115A2::begin() {
Wire.begin();
uint8_t whoami = read8(MPL3115A2_WHOAMI);
if (whoami != 0xC4) {
return false;
}
write8(MPL3115A2_CTRL_REG1,
MPL3115A2_CTRL_REG1_OST);
write8(MPL3115A2_PT_DATA_CFG,
MPL3115A2_PT_DATA_CFG_TDEFE |
MPL3115A2_PT_DATA_CFG_PDEFE |
MPL3115A2_PT_DATA_CFG_DREM);
return true;
}
/**************************************************************************/
/*!
@brief Gets the floating-point pressure level in kPa
*/
/**************************************************************************/
float Adafruit_MPL3115A2::getPressure(int OverSample) {
uint32_t pressure;
write8(MPL3115A2_CTRL_REG1,
MPL3115A2_CTRL_REG1_OST | OverSample);
uint8_t sta = 0;
while (!(sta & MPL3115A2_REGISTER_STATUS_PDR)) {
sta = read8(MPL3115A2_REGISTER_STATUS);
delay(10);
}
Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
Wire.write(MPL3115A2_REGISTER_PRESSURE_MSB);
Wire.endTransmission(false); // end transmission
Wire.requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)3);// send data n-bytes read
pressure = Wire.read(); // receive DATA
pressure <<= 8;
pressure |= Wire.read(); // receive DATA
pressure <<= 8;
pressure |= Wire.read(); // receive DATA
pressure >>= 4;
float baro = pressure;
baro /= 4.0;
return baro;
}
float Adafruit_MPL3115A2::getAltitude(int OverSample) {
int32_t alt;
write8(MPL3115A2_CTRL_REG1,
MPL3115A2_CTRL_REG1_OST | MPL3115A2_CTRL_REG1_ALT | OverSample);
uint8_t sta = 0;
while (!(sta & MPL3115A2_REGISTER_STATUS_PDR)) {
sta = read8(MPL3115A2_REGISTER_STATUS);
delay(10);
}
Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
Wire.write(MPL3115A2_REGISTER_PRESSURE_MSB);
Wire.endTransmission(false); // end transmission
Wire.requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)3);// send data n-bytes read
alt = Wire.read(); // receive DATA
alt <<= 8;
alt |= Wire.read(); // receive DATA
alt <<= 8;
alt |= Wire.read(); // receive DATA
alt >>= 4;
if (alt & 0x80000) {
alt |= 0xFFF00000;
}
float altitude = alt;
altitude /= 16.0;
return altitude;
}
/**************************************************************************/
/*!
@brief Gets the floating-point temperature in Centigrade
*/
/**************************************************************************/
float Adafruit_MPL3115A2::getTemperature(int OverSample) {
int16_t t;
write8(MPL3115A2_CTRL_REG1,
MPL3115A2_CTRL_REG1_OST | OverSample);
uint8_t sta = 0;
while (!(sta & MPL3115A2_REGISTER_STATUS_TDR)) {
sta = read8(MPL3115A2_REGISTER_STATUS);
delay(10);
}
Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
Wire.write(MPL3115A2_REGISTER_TEMP_MSB);
Wire.endTransmission(false); // end transmission
Wire.requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)2);// send data n-bytes read
t = Wire.read(); // receive DATA
t <<= 8;
t |= Wire.read(); // receive DATA
t >>= 4;
float temp = t;
temp /= 16.0;
return temp;
}
/*********************************************************************/
uint8_t Adafruit_MPL3115A2::read8(uint8_t a) {
Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
Wire.write(a); // sends register address to read from
Wire.endTransmission(false); // end transmission
Wire.requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)1);// send data n-bytes read
return Wire.read(); // receive DATA
}
void Adafruit_MPL3115A2::write8(uint8_t a, uint8_t d) {
Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
Wire.write(a); // sends register address to write to
Wire.write(d); // sends register data
Wire.endTransmission(false); // end transmission
}