forked from lpurdy01/Test-Station-Firmware
/
Adafruit_MPL3115A2.cpp
211 lines (178 loc) · 5.68 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**************************************************************************/
/*!
@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);
#ifndef __AVR_ATtiny85__
Serial.println(whoami, HEX);
#endif
if (whoami != 0xC4) {
return false;
}
write8(MPL3115A2_CTRL_REG1,
MPL3115A2_CTRL_REG1_SBYB |
MPL3115A2_CTRL_REG1_OS128 |
MPL3115A2_CTRL_REG1_ALT);
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() {
uint32_t pressure;
write8(MPL3115A2_CTRL_REG1,
MPL3115A2_CTRL_REG1_SBYB |
MPL3115A2_CTRL_REG1_OS128 |
MPL3115A2_CTRL_REG1_BAR);
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);
#ifdef __AVR_ATtiny85__
Wire.endTransmission(); // end transmission
#else
Wire.endTransmission(false); // end transmission
#endif
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() {
int32_t alt;
write8(MPL3115A2_CTRL_REG1,
MPL3115A2_CTRL_REG1_SBYB |
MPL3115A2_CTRL_REG1_OS128 |
MPL3115A2_CTRL_REG1_ALT);
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);
#ifdef __AVR_ATtiny85__
Wire.endTransmission(); // end transmission
#else
Wire.endTransmission(false); // end transmission
#endif
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 & 0x800000) {
alt |= 0xFF000000;
}
float altitude = alt;
altitude /= 16.0;
return altitude;
}
/**************************************************************************/
/*!
@brief Gets the floating-point temperature in Centigrade
*/
/**************************************************************************/
float Adafruit_MPL3115A2::getTemperature() {
uint16_t t;
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);
#ifdef __AVR_ATtiny85__
Wire.endTransmission(); // end transmission
#else
Wire.endTransmission(false); // end transmission
#endif
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
#ifdef __AVR_ATtiny85__
Wire.endTransmission(); // end transmission
#else
Wire.endTransmission(false); // end transmission
#endif
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) {
#ifndef __AVR_ATtiny85__
//Serial.print("Writing $"); Serial.print(a, HEX);
//Serial.print(" = 0x"); Serial.println(d, HEX);
#endif
Wire.beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
Wire.write(a); // sends register address to write to
Wire.write(d); // sends register data
#ifdef __AVR_ATtiny85__
Wire.endTransmission(); // end transmission
#else
Wire.endTransmission(false); // end transmission
#endif
}