Blue Robotics Bar02 Pressure Sensor sample MS5837-02BA Pressure Sensor

Dependents:   Bar02_MS5837-02BA OCE360_Project

Fork of MS5837 by Andrew Olguin

Committer:
yuuitirou528
Date:
Tue Jul 17 16:11:48 2018 +0000
Revision:
1:ee71a13b8e68
Parent:
0:0c2c06ab8e72
Child:
2:365c8caabc0e
The first releas

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aolgu003 0:0c2c06ab8e72 1 #include <stdlib.h>
aolgu003 0:0c2c06ab8e72 2 #include "MS5837.h"
aolgu003 0:0c2c06ab8e72 3
aolgu003 0:0c2c06ab8e72 4
aolgu003 0:0c2c06ab8e72 5 /*
aolgu003 0:0c2c06ab8e72 6 * Sensor operating function according data sheet
aolgu003 0:0c2c06ab8e72 7 */
aolgu003 0:0c2c06ab8e72 8
aolgu003 0:0c2c06ab8e72 9 void MS5837::MS5837Init(void)
aolgu003 0:0c2c06ab8e72 10 {
aolgu003 0:0c2c06ab8e72 11 MS5837Reset();
aolgu003 0:0c2c06ab8e72 12 MS5837ReadProm();
aolgu003 0:0c2c06ab8e72 13 return;
aolgu003 0:0c2c06ab8e72 14 }
aolgu003 0:0c2c06ab8e72 15
aolgu003 0:0c2c06ab8e72 16 /* Send soft reset to the sensor */
aolgu003 0:0c2c06ab8e72 17 void MS5837::MS5837Reset(void)
aolgu003 0:0c2c06ab8e72 18 {
aolgu003 0:0c2c06ab8e72 19 /* transmit out 1 byte reset command */
aolgu003 0:0c2c06ab8e72 20 ms5837_tx_data[0] = ms5837_reset;
aolgu003 0:0c2c06ab8e72 21 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
aolgu003 0:0c2c06ab8e72 22 printf("send soft reset\n");
aolgu003 0:0c2c06ab8e72 23 wait_ms(20);
aolgu003 0:0c2c06ab8e72 24 }
aolgu003 0:0c2c06ab8e72 25
aolgu003 0:0c2c06ab8e72 26 /* read the sensor calibration data from rom */
aolgu003 0:0c2c06ab8e72 27 void MS5837::MS5837ReadProm(void)
aolgu003 0:0c2c06ab8e72 28 {
aolgu003 0:0c2c06ab8e72 29 uint8_t i,j;
aolgu003 0:0c2c06ab8e72 30 for (i=0; i<8; i++) {
aolgu003 0:0c2c06ab8e72 31 j = i;
aolgu003 0:0c2c06ab8e72 32 ms5837_tx_data[0] = ms5837_PROMread + (j<<1);
aolgu003 0:0c2c06ab8e72 33 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
aolgu003 0:0c2c06ab8e72 34 if ( i2c.read( device_address, ms5837_rx_data, 2 ) );
aolgu003 0:0c2c06ab8e72 35 C[i] = ms5837_rx_data[1] + (ms5837_rx_data[0]<<8);
aolgu003 0:0c2c06ab8e72 36 }
aolgu003 0:0c2c06ab8e72 37 }
aolgu003 0:0c2c06ab8e72 38
aolgu003 0:0c2c06ab8e72 39 /* Start the sensor pressure conversion */
aolgu003 0:0c2c06ab8e72 40 void MS5837::MS5837ConvertD1(void)
aolgu003 0:0c2c06ab8e72 41 {
aolgu003 0:0c2c06ab8e72 42 ms5837_tx_data[0] = ms5837_convD1;
aolgu003 0:0c2c06ab8e72 43 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
aolgu003 0:0c2c06ab8e72 44 }
aolgu003 0:0c2c06ab8e72 45
aolgu003 0:0c2c06ab8e72 46 /* Start the sensor temperature conversion */
aolgu003 0:0c2c06ab8e72 47 void MS5837:: MS5837ConvertD2(void)
aolgu003 0:0c2c06ab8e72 48 {
aolgu003 0:0c2c06ab8e72 49 ms5837_tx_data[0] = ms5837_convD2;
aolgu003 0:0c2c06ab8e72 50 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
aolgu003 0:0c2c06ab8e72 51 }
aolgu003 0:0c2c06ab8e72 52
aolgu003 0:0c2c06ab8e72 53 /* Read the previous started conversion results */
aolgu003 0:0c2c06ab8e72 54 int32_t MS5837::MS5837ReadADC(void)
aolgu003 0:0c2c06ab8e72 55 {
aolgu003 0:0c2c06ab8e72 56 int32_t adc;
aolgu003 0:0c2c06ab8e72 57 wait_ms(150);
aolgu003 0:0c2c06ab8e72 58 ms5837_tx_data[0] = ms5837_ADCread;
aolgu003 0:0c2c06ab8e72 59 if ( i2c.write( device_address, ms5837_tx_data, 1 ) );
aolgu003 0:0c2c06ab8e72 60 if ( i2c.read( device_address, ms5837_rx_data, 3 ) );
aolgu003 0:0c2c06ab8e72 61 adc = ms5837_rx_data[2] + (ms5837_rx_data[1]<<8) + (ms5837_rx_data[0]<<16);
aolgu003 0:0c2c06ab8e72 62 //printf("ADC value: %x\n", adc);
aolgu003 0:0c2c06ab8e72 63 return (adc);
aolgu003 0:0c2c06ab8e72 64 }
aolgu003 0:0c2c06ab8e72 65
aolgu003 0:0c2c06ab8e72 66 /* return the results */
aolgu003 0:0c2c06ab8e72 67 float MS5837::MS5837_Pressure (void)
aolgu003 0:0c2c06ab8e72 68 {
aolgu003 0:0c2c06ab8e72 69 return P_MS5837;
aolgu003 0:0c2c06ab8e72 70 }
aolgu003 0:0c2c06ab8e72 71 float MS5837::MS5837_Temperature (void)
aolgu003 0:0c2c06ab8e72 72 {
aolgu003 0:0c2c06ab8e72 73 return T_MS5837;
aolgu003 0:0c2c06ab8e72 74 }
aolgu003 0:0c2c06ab8e72 75
aolgu003 0:0c2c06ab8e72 76 /* Sensor reading and calculation procedure */
aolgu003 0:0c2c06ab8e72 77 void MS5837::Barometer_MS5837(void)
aolgu003 0:0c2c06ab8e72 78 {
aolgu003 0:0c2c06ab8e72 79 int32_t dT, temp;
aolgu003 0:0c2c06ab8e72 80 int64_t OFF, SENS, press;
yuuitirou528 1:ee71a13b8e68 81
yuuitirou528 1:ee71a13b8e68 82 //int32_t dT = 0;
yuuitirou528 1:ee71a13b8e68 83 //int64_t SENS = 0;
yuuitirou528 1:ee71a13b8e68 84 //int64_t OFF = 0;
yuuitirou528 1:ee71a13b8e68 85 int32_t SENSi = 0;
yuuitirou528 1:ee71a13b8e68 86 int32_t OFFi = 0;
yuuitirou528 1:ee71a13b8e68 87 int32_t Ti = 0;
yuuitirou528 1:ee71a13b8e68 88 int64_t OFF2 = 0;
yuuitirou528 1:ee71a13b8e68 89 int64_t SENS2 = 0;
aolgu003 0:0c2c06ab8e72 90
aolgu003 0:0c2c06ab8e72 91 //no need to do this everytime!
aolgu003 0:0c2c06ab8e72 92
aolgu003 0:0c2c06ab8e72 93
aolgu003 0:0c2c06ab8e72 94 MS5837ConvertD1(); // start pressure conversion
aolgu003 0:0c2c06ab8e72 95 D1 = MS5837ReadADC(); // read the pressure value
aolgu003 0:0c2c06ab8e72 96 MS5837ConvertD2(); // start temperature conversion
aolgu003 0:0c2c06ab8e72 97 D2 = MS5837ReadADC(); // read the temperature value
aolgu003 0:0c2c06ab8e72 98 //printf("D1 = %d\n", D1);
aolgu003 0:0c2c06ab8e72 99 /* calculation according MS5837-01BA data sheet DA5837-01BA_006 */
yuuitirou528 1:ee71a13b8e68 100 dT = D2 - ((uint32_t)C[5]* 256);
yuuitirou528 1:ee71a13b8e68 101 OFF = (int64_t)C[2] * (1<<17) + ((int64_t)dT * (int64_t)C[4]) / (1<<6);
yuuitirou528 1:ee71a13b8e68 102 SENS = (int64_t)C[1] * (1<<16) + ((int64_t)dT * (int64_t)C[3]) / (1<<7);
aolgu003 0:0c2c06ab8e72 103
yuuitirou528 1:ee71a13b8e68 104 temp = 2000 + ((int64_t)dT * C[6]) / (1<<23);
yuuitirou528 1:ee71a13b8e68 105 press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<15);
yuuitirou528 1:ee71a13b8e68 106 if((temp/100)<20){ //Low temp
yuuitirou528 1:ee71a13b8e68 107 //Serial.println("here");
yuuitirou528 1:ee71a13b8e68 108 Ti = (11*int64_t(dT)*int64_t(dT))/(34359738368LL);
yuuitirou528 1:ee71a13b8e68 109 OFFi = (31*(temp-2000)*(temp-2000))/8;
yuuitirou528 1:ee71a13b8e68 110 SENSi = (63*(temp-2000)*(temp-2000))/32;
yuuitirou528 1:ee71a13b8e68 111 }
yuuitirou528 1:ee71a13b8e68 112
yuuitirou528 1:ee71a13b8e68 113 OFF2 = OFF-OFFi; //Calculate pressure and temp second order
yuuitirou528 1:ee71a13b8e68 114 SENS2 = SENS-SENSi;
yuuitirou528 1:ee71a13b8e68 115
yuuitirou528 1:ee71a13b8e68 116 temp = (temp-Ti);
yuuitirou528 1:ee71a13b8e68 117 press = (((D1*SENS2)/2097152l-OFF2)/32768l)/100;
yuuitirou528 1:ee71a13b8e68 118
aolgu003 0:0c2c06ab8e72 119 T_MS5837 = (float) temp / 100.0f; // result of temperature in deg C in this var
yuuitirou528 1:ee71a13b8e68 120 P_MS5837 = (float) press / 1.00f; // result of pressure in mBar in this var
aolgu003 0:0c2c06ab8e72 121
aolgu003 0:0c2c06ab8e72 122 }
aolgu003 0:0c2c06ab8e72 123
aolgu003 0:0c2c06ab8e72 124 float MS5837::depth(void) {
yuuitirou528 1:ee71a13b8e68 125 //return (P_MS5837/100)*1.019716;
yuuitirou528 1:ee71a13b8e68 126 return ((P_MS5837*100)-101300)/(fluidDensity*9.80665);
yuuitirou528 1:ee71a13b8e68 127
yuuitirou528 1:ee71a13b8e68 128 }
yuuitirou528 1:ee71a13b8e68 129
yuuitirou528 1:ee71a13b8e68 130 float MS5837::altitude(void) {
yuuitirou528 1:ee71a13b8e68 131 //return (P_MS5837/100)*1.019716;
yuuitirou528 1:ee71a13b8e68 132 return (1-pow((P_MS5837/1013.25),0.190284))*145366.45*0.3048;
yuuitirou528 1:ee71a13b8e68 133
aolgu003 0:0c2c06ab8e72 134 }