-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMS5607.cpp
171 lines (120 loc) · 4.19 KB
/
MS5607.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
#include "MS5607.h"
#include <math.h>
#include <stdlib.h>
#include "hardware/gpio.h"
#include "hardware/i2c.h"
#include "pico/stdlib.h"
void MS5607::init() {
i2c_init(i2c0, 100 * 1000);
gpio_set_function(sda_pin, GPIO_FUNC_I2C);
gpio_set_function(scl_pin, GPIO_FUNC_I2C);
read_prom();
}
int MS5607::reset() {
uint8_t cmd = MS5607_RESET_CMD;
if (i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false) ==
PICO_ERROR_GENERIC)
return 1;
else
read_prom();
return 0;
}
int MS5607::read_prom() {
uint8_t cmd = MS5607_READ_C1_CMD;
uint8_t buf[2] = {0, 0};
if (i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false) ==
PICO_ERROR_GENERIC)
return 1;
if (i2c_read_blocking(i2c, MS5607_ADDR, buf, 2, false) ==
PICO_ERROR_GENERIC)
return 1;
c1 = ((uint16_t)buf[0] << 8) | buf[1];
cmd = MS5607_READ_C2_CMD;
if (i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false) ==
PICO_ERROR_GENERIC)
return 1;
if (i2c_read_blocking(i2c, MS5607_ADDR, buf, 2, false) ==
PICO_ERROR_GENERIC)
return 1;
c2 = ((uint16_t)buf[0] << 8) | buf[1];
cmd = MS5607_READ_C3_CMD;
if (i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false) ==
PICO_ERROR_GENERIC)
return 1;
if (i2c_read_blocking(i2c, MS5607_ADDR, buf, 2, false) ==
PICO_ERROR_GENERIC)
return 1;
c3 = ((uint16_t)buf[0] << 8) | buf[1];
cmd = MS5607_READ_C4_CMD;
if (i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false) ==
PICO_ERROR_GENERIC)
return 1;
if (i2c_read_blocking(i2c, MS5607_ADDR, buf, 2, false) ==
PICO_ERROR_GENERIC)
return 1;
c4 = ((uint16_t)buf[0] << 8) | buf[1];
cmd = MS5607_READ_C5_CMD;
if (i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false) ==
PICO_ERROR_GENERIC)
return 1;
if (i2c_read_blocking(i2c, MS5607_ADDR, buf, 2, false) ==
PICO_ERROR_GENERIC)
return 1;
c5 = ((uint16_t)buf[0] << 8) | buf[1];
cmd = MS5607_READ_C6_CMD;
if (i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false) ==
PICO_ERROR_GENERIC)
return 1;
if (i2c_read_blocking(i2c, MS5607_ADDR, buf, 2, false) ==
PICO_ERROR_GENERIC)
return 1;
c6 = ((uint16_t)buf[0] << 8) | buf[1];
return 0;
}
int MS5607::conversion() {
uint8_t cmd = MS5607_PRESS_CONV_CMD;
uint8_t buf[3] = {0, 0, 0};
i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false);
i2c_read_blocking(i2c, MS5607_ADDR, &buf[0], 3, false);
d1 = ((uint32_t)buf[0] << 16) | ((uint32_t)buf[1] << 8) | buf[2];
cmd = MS5607_TEMP_CONV_CMD;
i2c_write_blocking(i2c, MS5607_ADDR, &cmd, 1, false);
i2c_read_blocking(i2c, MS5607_ADDR, &buf[0], 3, false);
d2 = ((uint32_t)buf[0] << 16) | ((uint32_t)buf[1] << 8) | buf[2];
return 0;
}
void MS5607::ms5607_get_press_temp(double* pressure, double* temperature) {
int32_t press = 0;
int32_t temp = 0;
int32_t t2 = 0;
int64_t off2 = 0;
int64_t sens2 = 0;
// Step 1: Read calibration data from PROM
// Already did this in the init()
// Step 2: Read pressure and temperature from the MS5607
conversion();
// The rest of this function mostly looks like random math, it is actually
// the compensation calculations outline in the datasheet for the device!
// Step 3: Calculate temperature
dT = d2 - (c5 * ((uint16_t)256));
temp = 2000 + (dT * (c6 / ((uint32_t)8388608)));
// Step 4: Calculate temperature compensated pressure
off = (c2 * 131072) + ((c4 * dT) / 64);
sens = (c1 * 65536) + ((c3 * dT) / 128);
// Second order compensation
if ((((double)temp) / 100) < 20) {
t2 = pow(dT, 2) / (int32_t)2147483648;
off2 = (61 * pow((temp - 2000), 2)) / (int64_t)16;
sens2 = (2 * pow((temp - 2000), 2));
if ((((double)temp) / 100) < -15) {
off2 = off2 + 15 * pow((temp + 1500), 2);
sens2 = sens2 + 8 * pow((temp + 1500), 2);
}
temp = temp - t2;
off = off - off2;
sens - sens2;
}
press = (((d1 * sens) / 2097152) - off) / 32768;
*temperature = (double)temp / 100;
*pressure = (double)press / 100;
}