1
1
//
2
2
// FILE: MS5837.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.1.0
4
+ // VERSION: 0.1.1
5
5
// DATE: 2023-11-12
6
6
// PURPOSE: Arduino library for MS5837 temperature and pressure sensor.
7
7
// URL: https://github.com/RobTillaart/MS5837
@@ -50,10 +50,10 @@ bool MS5837::reset(uint8_t mathMode)
50
50
51
51
initConstants (mathMode);
52
52
53
- // SKIP CRC check
53
+ // SKIP CRC check (for now)
54
54
55
55
// derive the type from mathMode instead of the other way around.
56
- // user must
56
+ // user must provide correct mathMode.
57
57
_type = MS5837_TYPE_30;
58
58
if (mathMode == 1 ) _type = MS5837_TYPE_02;
59
59
if (mathMode == 2 ) _type = MS5803_TYPE_01;
@@ -75,19 +75,28 @@ uint8_t MS5837::getAddress()
75
75
//
76
76
// READ
77
77
//
78
+ // datasheet page 7
79
+ // bits determines OSR => nr of samples => accuracy etc
78
80
bool MS5837::read (uint8_t bits)
79
81
{
80
82
if (isConnected () == false ) return false ;
81
83
82
- int index = constrain (bits, 8 , 13 );
83
- index -= 8 ;
84
+ int OSR = constrain (bits, 8 , 13 );
85
+ OSR -= 8 ;
86
+ // datasheet page 2 OSR, ADC maximum conversion time.
84
87
uint8_t waitMillis[6 ] = { 1 , 2 , 3 , 5 , 9 , 18 };
85
- uint8_t wait = waitMillis[index ];
86
-
88
+ uint8_t wait = waitMillis[OSR ];
89
+
87
90
// D1 conversion
88
91
_wire->beginTransmission (_address);
89
- _wire->write (MS5837_CMD_CONVERT_D1 + index * 2 );
90
- _wire->endTransmission (); // TODO check all of these
92
+ // datasheet page 7 adjust command byte based on OSR
93
+ _wire->write (MS5837_CMD_CONVERT_D1 + OSR * 2 );
94
+ _error = _wire->endTransmission ();
95
+ if (_error != 0 )
96
+ {
97
+ // _error = MS5837_I2C_ERROR ?
98
+ return false ;
99
+ }
91
100
92
101
uint32_t start = millis ();
93
102
@@ -97,13 +106,19 @@ bool MS5837::read(uint8_t bits)
97
106
yield ();
98
107
delay (1 );
99
108
}
100
- // NOTE: D1 and D2 are reserved in MBED (NANO BLE)
109
+ // NOTE: names D1 and D2 are reserved in MBED (NANO BLE)
101
110
uint32_t _D1 = readADC ();
102
111
103
112
// D2 conversion
104
113
_wire->beginTransmission (_address);
105
- _wire->write (MS5837_CMD_CONVERT_D2 + index * 2 );
106
- _wire->endTransmission ();
114
+ // datasheet page 7 adjust command byte based on OSR
115
+ _wire->write (MS5837_CMD_CONVERT_D2 + OSR * 2 );
116
+ _error = _wire->endTransmission ();
117
+ if (_error != 0 )
118
+ {
119
+ // _error = MS5837_I2C_ERROR ?
120
+ return false ;
121
+ }
107
122
108
123
start = millis ();
109
124
// while loop prevents blocking RTOS
@@ -113,7 +128,7 @@ bool MS5837::read(uint8_t bits)
113
128
delay (1 );
114
129
}
115
130
116
- // NOTE: D1 and D2 are reserved in MBED (NANO BLE)
131
+ // NOTE: names D1 and D2 are reserved in MBED (NANO BLE)
117
132
uint32_t _D2 = readADC ();
118
133
119
134
float dT = _D2 - C[5 ];
@@ -140,32 +155,38 @@ bool MS5837::read(uint8_t bits)
140
155
_pressure = (_D1 * sens * 4.76837158203E-7 - offset) * C[7 ] * 0.01 ;
141
156
_temperature *= 0.01 ;
142
157
158
+ _lastRead = millis ();
143
159
return true ;
144
160
}
145
161
162
+ uint32_t MS5837::lastRead ()
163
+ {
164
+ return _lastRead;
165
+ }
166
+
146
167
147
168
float MS5837::getPressure ()
148
169
{
149
170
return _pressure;
150
171
}
151
172
173
+
152
174
float MS5837::getTemperature ()
153
175
{
154
176
return _temperature;
155
177
}
156
178
157
179
158
180
// https://www.mide.com/air-pressure-at-altitude-calculator
159
- // https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702
160
- //
181
+ // https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 (stale link).
182
+ // https://en.wikipedia.org/wiki/Pressure_altitude
161
183
float MS5837::getAltitude (float airPressure)
162
184
{
163
185
float ratio = _pressure / airPressure;
164
- return 44330 * (1 - pow (ratio, 0.190294957 ));
186
+ return 44330 * (1 - pow (ratio, 0.190294957 ));
165
187
}
166
188
167
189
168
-
169
190
// ////////////////////////////////////////////////////////////////////
170
191
//
171
192
// DENSITY for depth
@@ -186,11 +207,23 @@ float MS5837::getDepth(float airPressure)
186
207
// 1 / (_density * 9.80665 * 10) can be pre-calculated and cached in setDensity.
187
208
//
188
209
// delta P = rho * g * h => h = delta P / rho * g
189
- // pressure = mbar, density grams/cm3 => correction factor 0.1 (=1/10)
210
+ // pressure = mbar,
211
+ // density grams/cm3 => correction factor 0.1 (= 1/10)
190
212
return (_pressure - airPressure)/(_density * 9.80665 * 10 );
191
213
}
192
214
193
215
216
+ // ////////////////////////////////////////////////////////////////////
217
+ //
218
+ // ERROR
219
+ //
220
+ int MS5837::getLastError ()
221
+ {
222
+ int e = _error;
223
+ _error = 0 ;
224
+ return e;
225
+ }
226
+
194
227
195
228
// ////////////////////////////////////////////////////////////////////
196
229
//
@@ -213,17 +246,17 @@ void MS5837::initConstants(uint8_t mathMode)
213
246
//
214
247
// datasheet MS5837_30 page 7
215
248
//
216
- // mathMode = 0; | = 1
217
- C[0 ] = 1 ;
218
- C[1 ] = 32768L ; // SENSt1 = C[1] * 2^15 | * 2^16
219
- C[2 ] = 65536L ; // OFFt1 = C[2] * 2^16 | * 2^17
220
- C[3 ] = 3.90625E-3 ; // TCS = C[3] / 2^8 | / 2^7
221
- C[4 ] = 7.8125E-3 ; // TCO = C[4] / 2^7 | / 2^6
222
- C[5 ] = 256 ; // Tref = C[5] * 2^8 | * 2^8
223
- C[6 ] = 1.1920928955E-7 ; // TEMPSENS = C[6] / 2^23 | / 2^23
224
- C[7 ] = 1.220703125E-4 ; // compensate uses / 2^13 | / 2^15
225
-
226
- // Appnote version for pressure.
249
+ // mathMode = 0 | = 1 | = 2 |
250
+ C[0 ] = 1 ; // manufacturer
251
+ C[1 ] = 32768L ; // SENSt1 = C[1] * 2^15 | * 2^16 | * 2^15 |
252
+ C[2 ] = 65536L ; // OFFt1 = C[2] * 2^16 | * 2^17 | * 2^16 |
253
+ C[3 ] = 3.90625E-3 ; // TCS = C[3] / 2^8 | / 2^7 | / 2^8 |
254
+ C[4 ] = 7.8125E-3 ; // TCO = C[4] / 2^7 | / 2^6 | / 2^7 |
255
+ C[5 ] = 256 ; // Tref = C[5] * 2^8 | * 2^8 | * 2^8 |
256
+ C[6 ] = 1.1920928955E-7 ; // TEMPSENS = C[6] / 2^23 | / 2^23 | / 2^23 |
257
+ C[7 ] = 1.220703125E-4 ; // compensate uses / 2^13 | / 2^15 | / 2^15 |
258
+
259
+ // App note version for pressure.
227
260
// adjustments for MS5837_02
228
261
if (mathMode == 1 )
229
262
{
@@ -275,5 +308,25 @@ uint32_t MS5837::readADC()
275
308
}
276
309
277
310
311
+ // ////////////////////////////////////////////////////////////////////
312
+ //
313
+ // DERIVED CLASSES
314
+ //
315
+
316
+ // ////////////////////////////////////////////////////////////////////
317
+ //
318
+ // MS5803
319
+ //
320
+ MS5803::MS5803 (TwoWire *wire):MS5837(wire)
321
+ {
322
+ }
323
+
324
+ MS5803::MS5803 (uint32_t address, TwoWire *wire)
325
+ {
326
+ _address = address;
327
+ _wire = wire;
328
+ }
329
+
330
+
278
331
// -- END OF FILE --
279
332
0 commit comments