-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMS5803.cpp
More file actions
executable file
·287 lines (233 loc) · 8.65 KB
/
Copy pathMS5803.cpp
File metadata and controls
executable file
·287 lines (233 loc) · 8.65 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/******************************************************************************
MS5803_I2C.cpp
Library for MS5803 pressure sensor.
Bobby Schulz @ Northern Widget LLC
6/26/2014
https://github.com/sparkfun/MS5803-14BA_Breakout
The MS5803 is a media isolated temperature and pressure sensor made by
Measurment Specialties which can be used to measure either water pressure
and depth, or baramatric (atmospheric) pressure, and altitude along with that
"Instruments register only through things they're designed to register.
Space still contains infinite unknowns."
-Mr. Spock
Distributed as-is; no warranty is given.
******************************************************************************/
#include <Wire.h> // Wire library is used for I2C
#include "MS5803.h"
MS5803::MS5803(uint8_t address, int MaxPressure)
// Base library type I2C
{
_address = address; //set interface used for communication
switch(MaxPressure)
{
// Set model number based on maximum pressure range
case (1): {Model = 1; break;} //BA01
case (2): {Model = 2; break;} //BA02
case (5): {Model = 3; break;} //BA05
case (7): {Model = 4; break;} //BA07
case (14): {Model = 5; break;} //BA14
case (30): {Model = 6; break;} //BA30
default: {Model = 3; break;} //BA05
}
}
void MS5803::reset(void)
// Reset device I2C
{
sendCommand(CMD_RESET);
sensorWait(3);
}
uint8_t MS5803::begin()
// Initialize library for subsequent pressure measurements
{
Wire.begin(); // Arduino Wire library initializer
uint8_t i;
uint8_t highByte = 0;
uint8_t lowByte = 0;
for(i = 0; i <= 7; i++){
sendCommand(CMD_PROM + (i * 2));
Wire.requestFrom((int)_address, 2);
highByte = Wire.read();
lowByte = Wire.read();
coefficient[i] = (highByte << 8)|lowByte;
// Uncomment below for debugging output.
// Serial.print("C");
// Serial.print(i);
// Serial.print("= ");
// Serial.println(coefficient[i]);
}
switch(Model){
case 1:
{
int ConvTemp1[] = {16, 7, 15, 8, 10000, 1, 31, 3, 0, 7, 0, 0, 3, 0, 0, 0};
memcpy(ConvCoef, ConvTemp1, 16);
}
break;
case 2:
{
int ConvTemp2[] = {17, 6, 16, 7, 10000, 1, 31, 61, 4, 2, 0, 20, 12, 0, 0, 0};
memcpy(ConvCoef, ConvTemp2, 16);
}
break;
case 3:
{
int ConvTemp3[] = {18, 5, 17, 7, 10000, 3, 33, 3, 3, 7, 3, 0, 3, 0, 0, 0};
memcpy(ConvCoef, ConvTemp3, 16);
}
break;
case 4:
{
int ConvTemp4[] = {18, 5, 17, 6, 2500, 3, 33, 3, 3, 7, 3, 0, 3, 0, 0, 0};
memcpy(ConvCoef, ConvTemp4, 16);
}
break;
case 5:
{
int ConvTemp5[] = {16, 7, 15, 8, 1000, 3, 33, 3, 1, 5, 3, 7, 4, 7, 37, 1};
memcpy(ConvCoef, ConvTemp5, 16);
}
break;
case 6:
{
int ConvTemp6[] = {16, 7, 15, 8, 10, 10, 3, 33, 3, 1, 5, 7, 7, 4, 7, 37, 1};
memcpy(ConvCoef, ConvTemp6, 16);
}
break;
}
Wire.beginTransmission((int)_address);
return Wire.endTransmission(); //Return sucess or failue of I2C connection
}
uint8_t MS5803::begin(uint8_t address, int MaxPressure)
// Initialize library for subsequent pressure measurements
{
// Reset address and pressure if given in begin
_address = address; //set interface used for communication
switch(MaxPressure)
{
// Set model number based on maximum pressure range
case (1): {Model = 1; break;} //BA01
case (2): {Model = 2; break;} //BA02
case (5): {Model = 3; break;} //BA05
case (7): {Model = 4; break;} //BA07
case (14): {Model = 5; break;} //BA14
case (30): {Model = 6; break;} //BA30
default: {Model = 3; break;} //BA05
}
return begin();
}
float MS5803::getTemperature(temperature_units units, precision _precision)
// Return a temperature reading in either F or C.
{
float temperature_reported, pressure_reported;
getMeasurements(_precision, units, temperature_reported, pressure_reported);
return temperature_reported;
}
float MS5803::getPressure(precision _precision)
// Return a pressure reading units Pa.
{
float temperature_reported, pressure_reported;
getMeasurements(_precision, CELSIUS, temperature_reported, pressure_reported);
return pressure_reported;
}
void MS5803::getMeasurements(precision _precision, temperature_units units, float &temperature_reported, float &pressure_reported)
// Gets resuts from ADC and stores them into internal variables
{
//Retrieve ADC result
int32_t temperature_raw = getADCconversion(TEMPERATURE, _precision);
int32_t pressure_raw = getADCconversion(PRESSURE, _precision);
//Create Variables for calculations
int32_t temp_calc;
int32_t pressure_calc;
int32_t dT;
//Now that we have a raw temperature, let's compute our actual.
dT = temperature_raw - ((int32_t)coefficient[5] << 8);
temp_calc = (((int64_t)dT * coefficient[6]) >> 23) + 2000;
// TODO TESTING _temperature_actual = temp_calc;
//Now we have our first order Temperature, let's calculate the second order.
int64_t T2, OFF2, SENS2, OFF, SENS; //working variables
if (temp_calc < 2000)
// If temp_calc is below 20.0C
//LOW TEMP
{
T2 = ConvCoef[5]* (((int64_t)dT * dT) >> ConvCoef[6]);
OFF2 = ConvCoef[7] * ((temp_calc - 2000) * (temp_calc - 2000)) / (pow(2,ConvCoef[8]));
SENS2 = ConvCoef[9] * ((temp_calc - 2000) * (temp_calc - 2000)) / (pow(2,ConvCoef[10]));
if(temp_calc < -1500)
// If temp_calc is below -15.0C
//VERY LOW TEMP
{
OFF2 = OFF2 + ConvCoef[11] * ((temp_calc + 1500) * (temp_calc + 1500));
SENS2 = SENS2 + ConvCoef[12] * ((temp_calc + 1500) * (temp_calc + 1500));
}
}
else
// If temp_calc is above 20.0C
//HIGH TEMP
{
T2 = ConvCoef[13] * ((uint64_t)dT * dT)/pow(2,ConvCoef[14]);
OFF2 = ConvCoef[15]*((temp_calc - 2000) * (temp_calc - 2000)) / 16;
SENS2 = 0;
if(temp_calc > 4500 && Model == 1) SENS2 = SENS2 - ((temp_calc + 1500) * (temp_calc + 1500))/8; //NOTE: this condition is only used for the 01BA model!
}
// Now bring it all together to apply offsets
OFF = ((int64_t)coefficient[2] << ConvCoef[0]) + (((coefficient[4] * (int64_t)dT)) >> ConvCoef[1]); //05BA model!
SENS = ((int64_t)coefficient[1] << ConvCoef[2]) + (((coefficient[3] * (int64_t)dT)) >> ConvCoef[3]); //05BA model!
// OFF = ((int64_t)coefficient[2] << 16) + (((coefficient[4] * (int64_t)dT)) >> 7);
// SENS = ((int64_t)coefficient[1] << 15) + (((coefficient[3] * (int64_t)dT)) >> 8);
temp_calc = temp_calc - T2;
OFF = OFF - OFF2;
SENS = SENS - SENS2;
// Now lets calculate the pressure
pressure_calc = (((SENS * pressure_raw) / 2097152 ) - OFF) / 32768;
_temperature_actual = temp_calc ;
_pressure_actual = pressure_calc ; // 10;// pressure_calc;
// default: Celsius
temperature_reported = _temperature_actual / 100.0f;
// If Fahrenheit is selected return the temperature converted to F
if(units == FAHRENHEIT){
temperature_reported = (((temperature_reported) * 9) / 5) + 32;
}
pressure_reported = _pressure_actual;
pressure_reported = pressure_reported / (float(ConvCoef[4])/100.0); //05BA model!
}
uint32_t MS5803::getADCconversion(measurement _measurement, precision _precision)
// Retrieve ADC measurement from the device.
// Select measurement type and precision
// TODO: Set up for asynchronous conversion? - ie, w/o waits
{
uint32_t result;
uint8_t highByte = 0;
uint8_t midByte = 0;
uint8_t lowByte = 0;
sendCommand(CMD_ADC_CONV + _measurement + _precision);
// Wait for conversion to complete
sensorWait(1); //general delay
switch( _precision )
{
case ADC_256 : sensorWait(1); break;
case ADC_512 : sensorWait(3); break;
case ADC_1024: sensorWait(4); break;
case ADC_2048: sensorWait(6); break;
case ADC_4096: sensorWait(10); break;
}
sendCommand(CMD_ADC_READ);
Wire.requestFrom((int)_address, 3);
while(Wire.available())
{
highByte = Wire.read();
midByte = Wire.read();
lowByte = Wire.read();
}
result = ((uint32_t)highByte << 16) + ((uint32_t)midByte << 8) + lowByte;
return result;
}
void MS5803::sendCommand(uint8_t command)
{
Wire.beginTransmission(_address);
Wire.write(command);
Wire.endTransmission();
}
void MS5803::sensorWait(uint8_t time)
// Delay function. This can be modified to work outside of Arduino based MCU's
{
delay(time);
};