-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPL3115A2.c
244 lines (200 loc) · 7.99 KB
/
MPL3115A2.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*!
* @file Adafruit_MPL3115A2.cpp
*
* @mainpage Adafruit MPL3115A2 alitmeter
*
* @section intro_sec Introduction
*
* This is the documentation for Adafruit's MPL3115A2 driver for the
* Arduino platform. It is designed specifically to work with the
* Adafruit MPL3115A2 breakout: https://www.adafruit.com/products/1893
*
* These sensors use I2C to communicate, 2 pins (SCL+SDA) are required
* to interface with the breakout.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section dependencies Dependencies
*
* @section author Author
*
* Written by Kevin "KTOWN" Townsend for Adafruit Industries.
*
* @section license License
*
* BSD license, all text here must be included in any redistribution.
*
*/
#include "MPL3115A2.h"
#define delay(x) __delay_cycles(x * 1000)
static ctrl_reg1 _ctrl_reg1;
/**************************************************************************/
/*!
@brief Setups the HW (reads coefficients values, etc.)
@param twoWire Optional TwoWire I2C object
@return true on successful startup, false otherwise
*/
/**************************************************************************/
int MPL3115A2_begin()
{
uint8_t whoami = I2C_read8(MPL3115A2_WHOAMI);
if (whoami != 0xC4) return 0;
I2C_write8(MPL3115A2_CTRL_REG1, MPL3115A2_CTRL_REG1_RST);
delay(10);
while(I2C_read8(MPL3115A2_CTRL_REG1) & MPL3115A2_CTRL_REG1_RST) delay(10);
_ctrl_reg1.reg = MPL3115A2_CTRL_REG1_OS128 | MPL3115A2_CTRL_REG1_ALT;
I2C_write8(MPL3115A2_CTRL_REG1, _ctrl_reg1.reg);
I2C_write8(MPL3115A2_PT_DATA_CFG,
MPL3115A2_PT_DATA_CFG_TDEFE |
MPL3115A2_PT_DATA_CFG_PDEFE |
MPL3115A2_PT_DATA_CFG_DREM);
return 1;
}
/**************************************************************************/
/*!
@brief Gets the floating-point pressure level in kPa
@return altitude reading as a floating point value
*/
/**************************************************************************/
float MPL3115A2_getPressure()
{
while(I2C_read8(MPL3115A2_CTRL_REG1) & MPL3115A2_CTRL_REG1_OST) delay(10);
_ctrl_reg1.bit.ALT = 0;
I2C_write8(MPL3115A2_CTRL_REG1, _ctrl_reg1.reg);
_ctrl_reg1.bit.OST = 1;
I2C_write8(MPL3115A2_CTRL_REG1, _ctrl_reg1.reg);
uint8_t sta = 0;
while (! (sta & MPL3115A2_REGISTER_STATUS_PDR)) {
sta = I2C_read8(MPL3115A2_REGISTER_STATUS);
delay(10);
}
/*******************************************************************************************/
//uint32_t pressure;
// _i2c->beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
// _i2c->write(MPL3115A2_REGISTER_PRESSURE_MSB);
// _i2c->endTransmission(false); // end transmission
// _i2c->requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)3);// send data n-bytes read
// pressure = _i2c->read(); // receive DATA
// pressure <<= 8;
// pressure |= _i2c->read(); // receive DATA
// pressure <<= 8;
// pressure |= _i2c->read(); // receive DATA
// pressure >>= 4;
/*******************************************************************************************/
uint8_t pressure[3];
pressure[0] = I2C_read8(MPL3115A2_REGISTER_PRESSURE_MSB);
pressure[1] = I2C_read8(MPL3115A2_REGISTER_PRESSURE_CSB);
pressure[2] = I2C_read8(MPL3115A2_REGISTER_PRESSURE_LSB);
float baro = (pressure[0] << 12) | (pressure[1] << 4) | (pressure[2] >> 4);
baro /= 4.0;
return baro;
}
/**************************************************************************/
/*!
@brief Gets the floating-point altitude value
@return altitude reading as a floating-point value
*/
/**************************************************************************/
float MPL3115A2_getAltitude()
{
while(I2C_read8(MPL3115A2_CTRL_REG1) & MPL3115A2_CTRL_REG1_OST) delay(10);
_ctrl_reg1.bit.ALT = 1;
I2C_write8(MPL3115A2_CTRL_REG1, _ctrl_reg1.reg);
_ctrl_reg1.bit.OST = 1;
I2C_write8(MPL3115A2_CTRL_REG1, _ctrl_reg1.reg);
uint8_t sta = 0;
while (! (sta & MPL3115A2_REGISTER_STATUS_PDR)) {
sta = I2C_read8(MPL3115A2_REGISTER_STATUS);
delay(10);
}
// int32_t alt;
// _i2c->beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
// _i2c->write(MPL3115A2_REGISTER_PRESSURE_MSB);
// _i2c->endTransmission(false); // end transmission
// _i2c->requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)3);// send data n-bytes read
// alt = ((uint32_t)_i2c->read()) << 24; // receive DATA
// alt |= ((uint32_t)_i2c->read()) << 16; // receive DATA
// alt |= ((uint32_t)_i2c->read()) << 8; // receive DATA
uint32_t alt[3];
alt[0] = I2C_read8(MPL3115A2_REGISTER_PRESSURE_MSB);
alt[1] = I2C_read8(MPL3115A2_REGISTER_PRESSURE_CSB);
alt[2] = I2C_read8(MPL3115A2_REGISTER_PRESSURE_LSB);
float altitude = (alt[0] << 24) | (alt[1] << 16) | (alt[2] << 8);
altitude /= 65536.0;
return altitude;
}
/**************************************************************************/
/*!
@brief Set the local sea level barometric pressure
@param pascal the pressure to use as the baseline
*/
/**************************************************************************/
void MPL3115A2_setSeaPressure(float pascal) {
// uint16_t bar = pascal/2;
// _i2c->beginTransmission(MPL3115A2_ADDRESS);
// _i2c->write((uint8_t)MPL3115A2_BAR_IN_MSB);
// _i2c->write((uint8_t)(bar>>8));
// _i2c->write((uint8_t)bar);
// _i2c->endTransmission(false);
}
/**************************************************************************/
/*!
@brief Gets the floating-point temperature in Centigrade
@return temperature reading in Centigrade as a floating-point value
*/
/**************************************************************************/
float MPL3115A2_getTemperature()
{
uint8_t sta = 0;
while (!(sta & MPL3115A2_REGISTER_STATUS_TDR)) {
sta = I2C_read8(MPL3115A2_REGISTER_STATUS);
delay(10);
}
// int16_t t;
// _i2c->beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
// _i2c->write(MPL3115A2_REGISTER_TEMP_MSB);
// _i2c->endTransmission(false); // end transmission
// _i2c->requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)2);// send data n-bytes read
// t = _i2c->read(); // receive DATA
// t <<= 8;
// t |= _i2c->read(); // receive DATA
// t >>= 4;
uint8_t t_array[2];
t_array[0] = I2C_read8(MPL3115A2_REGISTER_TEMP_MSB);
t_array[1] = I2C_read8(MPL3115A2_REGISTER_TEMP_LSB);
uint16_t t = (t_array[0] << 4) | (t_array[1] >> 4);
if (t & 0x800) {
t |= 0xF000;
}
float temp = t;
temp /= 16.0;
return temp;
}
/**************************************************************************/
/*!
@brief read 1 byte of data at the specified address
@param a the address to read
@return the read data byte
*/
/**************************************************************************/
// uint8_t I2C_read8(uint8_t a) {
// _i2c->beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
// _i2c->write(a); // sends register address to read from
// _i2c->endTransmission(false); // end transmission
// _i2c->requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)1);// send data n-bytes read
// return _i2c->read(); // receive DATA
// }
// /**************************************************************************/
// !
// @brief write a byte of data to the specified address
// @param a the address to write to
// @param d the byte to write
// /**************************************************************************/
// void I2C_write8(uint8_t a, uint8_t d) {
// _i2c->beginTransmission(MPL3115A2_ADDRESS); // start transmission to device
// _i2c->write(a); // sends register address to write to
// _i2c->write(d); // sends register data
// _i2c->endTransmission(false); // end transmission
// }