forked from jarzebski/Arduino-MS5611
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MS5611.cpp
263 lines (207 loc) · 5.42 KB
/
MS5611.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
MS5611.cpp - Class file for the MS5611 Barometric Pressure & Temperature Sensor Arduino Library.
Version: 1.0.0
(c) 2014 Korneliusz Jarzebski
www.jarzebski.pl
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <math.h>
#include <MS5611.h>
bool MS5611::begin(ms5611_osr_t osr)
{
Wire.begin();
reset();
setOversampling(osr);
delay(100);
readPROM();
return true;
}
// Set oversampling value
void MS5611::setOversampling(ms5611_osr_t osr)
{
switch (osr)
{
case MS5611_ULTRA_LOW_POWER:
ct = 1;
break;
case MS5611_LOW_POWER:
ct = 2;
break;
case MS5611_STANDARD:
ct = 3;
break;
case MS5611_HIGH_RES:
ct = 5;
break;
case MS5611_ULTRA_HIGH_RES:
ct = 10;
break;
}
uosr = osr;
}
// Get oversampling value
ms5611_osr_t MS5611::getOversampling(void)
{
return (ms5611_osr_t)uosr;
}
void MS5611::reset(void)
{
Wire.beginTransmission(MS5611_ADDRESS);
#if ARDUINO >= 100
Wire.write(MS5611_CMD_RESET);
#else
Wire.send(MS5611_CMD_RESET);
#endif
Wire.endTransmission();
}
void MS5611::readPROM(void)
{
for (uint8_t offset = 0; offset < 6; offset++)
{
fc[offset] = readRegister16(MS5611_CMD_READ_PROM + (offset * 2));
}
}
uint32_t MS5611::readRawTemperature(void)
{
Wire.beginTransmission(MS5611_ADDRESS);
#if ARDUINO >= 100
Wire.write(MS5611_CMD_CONV_D2 + uosr);
#else
Wire.send(MS5611_CMD_CONV_D2 + uosr);
#endif
Wire.endTransmission();
delay(ct);
return readRegister24(MS5611_CMD_ADC_READ);
}
uint32_t MS5611::readRawPressure(void)
{
Wire.beginTransmission(MS5611_ADDRESS);
#if ARDUINO >= 100
Wire.write(MS5611_CMD_CONV_D1 + uosr);
#else
Wire.send(MS5611_CMD_CONV_D1 + uosr);
#endif
Wire.endTransmission();
delay(ct);
return readRegister24(MS5611_CMD_ADC_READ);
}
int32_t MS5611::readPressure(bool compensation)
{
uint32_t D1 = readRawPressure();
uint32_t D2 = readRawTemperature();
int32_t dT = D2 - (uint32_t)fc[4] * 256;
int64_t OFF = (int64_t)fc[1] * 65536 + (int64_t)fc[3] * dT / 128;
int64_t SENS = (int64_t)fc[0] * 32768 + (int64_t)fc[2] * dT / 256;
if (compensation)
{
int32_t TEMP = 2000 + ((int64_t) dT * fc[5]) / 8388608;
OFF2 = 0;
SENS2 = 0;
if (TEMP < 2000)
{
OFF2 = 5 * ((TEMP - 2000) * (TEMP - 2000)) / 2;
SENS2 = 5 * ((TEMP - 2000) * (TEMP - 2000)) / 4;
}
if (TEMP < -1500)
{
OFF2 = OFF2 + 7 * ((TEMP + 1500) * (TEMP + 1500));
SENS2 = SENS2 + 11 * ((TEMP + 1500) * (TEMP + 1500)) / 2;
}
OFF = OFF - OFF2;
SENS = SENS - SENS2;
}
uint32_t P = (D1 * SENS / 2097152 - OFF) / 32768;
return P;
}
double MS5611::readTemperature(bool compensation)
{
uint32_t D2 = readRawTemperature();
int32_t dT = D2 - (uint32_t)fc[4] * 256;
int32_t TEMP = 2000 + ((int64_t) dT * fc[5]) / 8388608;
TEMP2 = 0;
if (compensation)
{
if (TEMP < 2000)
{
TEMP2 = (dT * dT) / (2 << 30);
}
}
TEMP = TEMP - TEMP2;
return ((double)TEMP/100);
}
// Calculate altitude from Pressure & Sea level pressure
double MS5611::getAltitude(double pressure, double seaLevelPressure)
{
return (44330.0f * (1.0f - pow((double)pressure / (double)seaLevelPressure, 0.1902949f)));
}
// Calculate sea level from Pressure given on specific altitude
double MS5611::getSeaLevel(double pressure, double altitude)
{
return ((double)pressure / pow(1.0f - ((double)altitude / 44330.0f), 5.255f));
}
// Read 16-bit from register (oops MSB, LSB)
uint16_t MS5611::readRegister16(uint8_t reg)
{
uint16_t value;
Wire.beginTransmission(MS5611_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.beginTransmission(MS5611_ADDRESS);
Wire.requestFrom(MS5611_ADDRESS, 2);
while(!Wire.available()) {};
#if ARDUINO >= 100
uint8_t vha = Wire.read();
uint8_t vla = Wire.read();
#else
uint8_t vha = Wire.receive();
uint8_t vla = Wire.receive();
#endif;
Wire.endTransmission();
value = vha << 8 | vla;
return value;
}
// Read 24-bit from register (oops XSB, MSB, LSB)
uint32_t MS5611::readRegister24(uint8_t reg)
{
uint32_t value;
Wire.beginTransmission(MS5611_ADDRESS);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
Wire.beginTransmission(MS5611_ADDRESS);
Wire.requestFrom(MS5611_ADDRESS, 3);
while(!Wire.available()) {};
#if ARDUINO >= 100
uint8_t vxa = Wire.read();
uint8_t vha = Wire.read();
uint8_t vla = Wire.read();
#else
uint8_t vxa = Wire.receive();
uint8_t vha = Wire.receive();
uint8_t vla = Wire.receive();
#endif;
Wire.endTransmission();
value = ((int32_t)vxa << 16) | ((int32_t)vha << 8) | vla;
return value;
}