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

Dependents:   Bar02_MS5837-02BA OCE360_Project

Fork of MS5837 by Andrew Olguin

Committer:
aolgu003
Date:
Mon Jul 25 00:19:52 2016 +0000
Revision:
0:0c2c06ab8e72
Child:
1:ee71a13b8e68
Nothing was changed

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;
aolgu003 0:0c2c06ab8e72 81
aolgu003 0:0c2c06ab8e72 82 //no need to do this everytime!
aolgu003 0:0c2c06ab8e72 83
aolgu003 0:0c2c06ab8e72 84
aolgu003 0:0c2c06ab8e72 85 MS5837ConvertD1(); // start pressure conversion
aolgu003 0:0c2c06ab8e72 86 D1 = MS5837ReadADC(); // read the pressure value
aolgu003 0:0c2c06ab8e72 87 MS5837ConvertD2(); // start temperature conversion
aolgu003 0:0c2c06ab8e72 88 D2 = MS5837ReadADC(); // read the temperature value
aolgu003 0:0c2c06ab8e72 89 //printf("D1 = %d\n", D1);
aolgu003 0:0c2c06ab8e72 90 /* calculation according MS5837-01BA data sheet DA5837-01BA_006 */
aolgu003 0:0c2c06ab8e72 91 dT = D2 - (C[5]* 256);
aolgu003 0:0c2c06ab8e72 92 OFF = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7);
aolgu003 0:0c2c06ab8e72 93 SENS = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8);
aolgu003 0:0c2c06ab8e72 94
aolgu003 0:0c2c06ab8e72 95 temp = 2000 + (dT * C[6]) / (1<<23);
aolgu003 0:0c2c06ab8e72 96 T_MS5837 = (float) temp / 100.0f; // result of temperature in deg C in this var
aolgu003 0:0c2c06ab8e72 97 press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<13);
aolgu003 0:0c2c06ab8e72 98 P_MS5837 = (float) press / 10.0f; // result of pressure in mBar in this var
aolgu003 0:0c2c06ab8e72 99
aolgu003 0:0c2c06ab8e72 100 if (P_MS5837 < 900 || P_MS5837 > 3000) {
aolgu003 0:0c2c06ab8e72 101 MS5837Reset(); // reset the sensor
aolgu003 0:0c2c06ab8e72 102 MS5837ReadProm(); // read the calibration values
aolgu003 0:0c2c06ab8e72 103 }
aolgu003 0:0c2c06ab8e72 104 }
aolgu003 0:0c2c06ab8e72 105
aolgu003 0:0c2c06ab8e72 106 float MS5837::depth(void) {
aolgu003 0:0c2c06ab8e72 107 return (P_MS5837/100)*1.019716;
aolgu003 0:0c2c06ab8e72 108 }