Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MS5837.cpp
00001 #include <stdlib.h> 00002 #include "MS5837.h" 00003 00004 00005 /* 00006 * Sensor operating function according data sheet 00007 */ 00008 00009 void MS5837::MS5837Init(void) 00010 { 00011 MS5837Reset(); 00012 MS5837ReadProm(); 00013 return; 00014 } 00015 00016 /* Send soft reset to the sensor */ 00017 void MS5837::MS5837Reset(void) 00018 { 00019 /* transmit out 1 byte reset command */ 00020 ms5837_tx_data[0] = ms5837_reset; 00021 if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); 00022 printf("send soft reset\n"); 00023 wait_ms(20); 00024 } 00025 00026 /* read the sensor calibration data from rom */ 00027 void MS5837::MS5837ReadProm(void) 00028 { 00029 uint8_t i,j; 00030 for (i=0; i<8; i++) { 00031 j = i; 00032 ms5837_tx_data[0] = ms5837_PROMread + (j<<1); 00033 if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); 00034 if ( i2c.read( device_address, ms5837_rx_data, 2 ) ); 00035 C[i] = ms5837_rx_data[1] + (ms5837_rx_data[0]<<8); 00036 } 00037 } 00038 00039 /* Start the sensor pressure conversion */ 00040 void MS5837::MS5837ConvertD1(void) 00041 { 00042 ms5837_tx_data[0] = ms5837_convD1; 00043 if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); 00044 } 00045 00046 /* Start the sensor temperature conversion */ 00047 void MS5837:: MS5837ConvertD2(void) 00048 { 00049 ms5837_tx_data[0] = ms5837_convD2; 00050 if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); 00051 } 00052 00053 /* Read the previous started conversion results */ 00054 int32_t MS5837::MS5837ReadADC(void) 00055 { 00056 int32_t adc; 00057 wait_ms(150); 00058 ms5837_tx_data[0] = ms5837_ADCread; 00059 if ( i2c.write( device_address, ms5837_tx_data, 1 ) ); 00060 if ( i2c.read( device_address, ms5837_rx_data, 3 ) ); 00061 adc = ms5837_rx_data[2] + (ms5837_rx_data[1]<<8) + (ms5837_rx_data[0]<<16); 00062 //printf("ADC value: %x\n", adc); 00063 return (adc); 00064 } 00065 00066 /* return the results */ 00067 float MS5837::MS5837_Pressure (void) 00068 { 00069 return P_MS5837; 00070 } 00071 float MS5837::MS5837_Temperature (void) 00072 { 00073 return T_MS5837; 00074 } 00075 00076 /* Sensor reading and calculation procedure */ 00077 void MS5837::Barometer_MS5837(void) 00078 { 00079 int32_t dT, temp; 00080 int64_t OFF, SENS, press; 00081 00082 //int32_t dT = 0; 00083 //int64_t SENS = 0; 00084 //int64_t OFF = 0; 00085 int32_t SENSi = 0; 00086 int32_t OFFi = 0; 00087 int32_t Ti = 0; 00088 int64_t OFF2 = 0; 00089 int64_t SENS2 = 0; 00090 00091 //no need to do this everytime! 00092 00093 00094 MS5837ConvertD1(); // start pressure conversion 00095 D1 = MS5837ReadADC(); // read the pressure value 00096 MS5837ConvertD2(); // start temperature conversion 00097 D2 = MS5837ReadADC(); // read the temperature value 00098 //printf("D1 = %d\n", D1); 00099 /* calculation according MS5837-01BA data sheet DA5837-01BA_006 */ 00100 dT = D2 - ((uint32_t)C[5]* 256); 00101 OFF = (int64_t)C[2] * (1<<17) + ((int64_t)dT * (int64_t)C[4]) / (1<<6); 00102 SENS = (int64_t)C[1] * (1<<16) + ((int64_t)dT * (int64_t)C[3]) / (1<<7); 00103 00104 temp = 2000 + ((int64_t)dT * C[6]) / (1<<23); 00105 press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<15); 00106 if((temp/100)<20){ //Low temp 00107 //Serial.println("here"); 00108 Ti = (11*int64_t(dT)*int64_t(dT))/(34359738368LL); 00109 OFFi = (31*(temp-2000)*(temp-2000))/8; 00110 SENSi = (63*(temp-2000)*(temp-2000))/32; 00111 } 00112 00113 OFF2 = OFF-OFFi; //Calculate pressure and temp second order 00114 SENS2 = SENS-SENSi; 00115 00116 temp = (temp-Ti); 00117 press = (((D1*SENS2)/2097152l-OFF2)/32768l)/100; 00118 00119 T_MS5837 = (float) temp / 100.0f; // result of temperature in deg C in this var 00120 P_MS5837 = (float) press / 1.00f; // result of pressure in mBar in this var 00121 00122 } 00123 00124 float MS5837::depth(void) { 00125 //return (P_MS5837/100)*1.019716; 00126 return ((P_MS5837*100)-101300)/(fluidDensity*9.80665); 00127 00128 } 00129 00130 float MS5837::altitude(void) { 00131 //return (P_MS5837/100)*1.019716; 00132 return (1-pow((P_MS5837/1013.25),0.190284))*145366.45*0.3048; 00133 00134 }
Generated on Mon Sep 26 2022 18:48:51 by
1.7.2