Skip to content

Commit

Permalink
Adding the first part of @NeilBaner PR #21
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulZC authored and patrickelectric committed Mar 3, 2021
1 parent acb6869 commit c5bc787
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
54 changes: 27 additions & 27 deletions src/MS5837.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "MS5837.h"
#include <Wire.h>

#define MS5837_ADDR 0x76
#define MS5837_ADDR 0x76
#define MS5837_RESET 0x1E
#define MS5837_ADC_READ 0x00
#define MS5837_PROM_READ 0xA0
Expand Down Expand Up @@ -64,65 +64,65 @@ void MS5837::read() {
Wire.endTransmission();

delay(20); // Max conversion time per datasheet

Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_ADC_READ);
Wire.endTransmission();

Wire.requestFrom(MS5837_ADDR,3);
D1 = 0;
D1 = Wire.read();
D1 = (D1 << 8) | Wire.read();
D1 = (D1 << 8) | Wire.read();
D1_pres = 0;
D1_pres = Wire.read();
D1_pres = (D1_pres << 8) | Wire.read();
D1_pres = (D1_pres << 8) | Wire.read();

// Request D2 conversion
Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_CONVERT_D2_8192);
Wire.endTransmission();

delay(20); // Max conversion time per datasheet

Wire.beginTransmission(MS5837_ADDR);
Wire.write(MS5837_ADC_READ);
Wire.endTransmission();

Wire.requestFrom(MS5837_ADDR,3);
D2 = 0;
D2 = Wire.read();
D2 = (D2 << 8) | Wire.read();
D2 = (D2 << 8) | Wire.read();
D2_temp = 0;
D2_temp = Wire.read();
D2_temp = (D2_temp << 8) | Wire.read();
D2_temp = (D2_temp << 8) | Wire.read();

calculate();
}

void MS5837::calculate() {
// Given C1-C6 and D1, D2, calculated TEMP and P
// Do conversion first and then second order temp compensation

int32_t dT = 0;
int64_t SENS = 0;
int64_t OFF = 0;
int32_t SENSi = 0;
int32_t OFFi = 0;
int32_t Ti = 0;
int32_t OFFi = 0;
int32_t Ti = 0;
int64_t OFF2 = 0;
int64_t SENS2 = 0;

// Terms called
dT = D2-uint32_t(C[5])*256l;
dT = D2_temp-uint32_t(C[5])*256l;
if ( _model == MS5837_02BA ) {
SENS = int64_t(C[1])*65536l+(int64_t(C[3])*dT)/128l;
OFF = int64_t(C[2])*131072l+(int64_t(C[4])*dT)/64l;
P = (D1*SENS/(2097152l)-OFF)/(32768l);
P = (D1_pres*SENS/(2097152l)-OFF)/(32768l);
} else {
SENS = int64_t(C[1])*32768l+(int64_t(C[3])*dT)/256l;
OFF = int64_t(C[2])*65536l+(int64_t(C[4])*dT)/128l;
P = (D1*SENS/(2097152l)-OFF)/(8192l);
P = (D1_pres*SENS/(2097152l)-OFF)/(8192l);
}

// Temp conversion
TEMP = 2000l+int64_t(dT)*C[6]/8388608LL;

//Second order compensation
if ( _model == MS5837_02BA ) {
if((TEMP/100)<20){ //Low temp
Expand All @@ -146,16 +146,16 @@ void MS5837::calculate() {
SENSi = 0;
}
}

OFF2 = OFF-OFFi; //Calculate pressure and temp second order
SENS2 = SENS-SENSi;

TEMP = (TEMP-Ti);

if ( _model == MS5837_02BA ) {
P = (((D1*SENS2)/2097152l-OFF2)/32768l);
P = (((D1_pres*SENS2)/2097152l-OFF2)/32768l);
} else {
P = (((D1*SENS2)/2097152l-OFF2)/8192l);
P = (((D1_pres*SENS2)/2097152l-OFF2)/8192l);
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@ uint8_t MS5837::crc4(uint16_t n_prom[]) {
}
}
}

n_rem = ((n_rem >> 12) & 0x000F);

return n_rem ^ 0x00;
Expand Down
10 changes: 5 additions & 5 deletions src/MS5837.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* Blue Robotics Arduino MS5837-30BA Pressure/Temperature Sensor Library
------------------------------------------------------------
Title: Blue Robotics Arduino MS5837-30BA Pressure/Temperature Sensor Library
Description: This library provides utilities to communicate with and to
read data from the Measurement Specialties MS5837-30BA pressure/temperature
read data from the Measurement Specialties MS5837-30BA pressure/temperature
sensor.
Authors: Rustom Jehangir, Blue Robotics Inc.
Expand Down Expand Up @@ -32,7 +32,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-------------------------------*/
-------------------------------*/

#ifndef MS5837_H_BLUEROBOTICS
#define MS5837_H_BLUEROBOTICS
Expand All @@ -57,7 +57,7 @@ class MS5837 {
*/
void setModel(uint8_t model);

/** Provide the density of the working fluid in kg/m^3. Default is for
/** Provide the density of the working fluid in kg/m^3. Default is for
* seawater. Should be 997 for freshwater.
*/
void setFluidDensity(float density);
Expand Down Expand Up @@ -85,7 +85,7 @@ class MS5837 {

private:
uint16_t C[8];
uint32_t D1, D2;
uint32_t D1_pres, D2_temp;
int32_t TEMP;
int32_t P;
uint8_t _model;
Expand Down

0 comments on commit c5bc787

Please sign in to comment.