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.
Fork of MS5837 by
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\r"); 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 //no need to do this everytime! 00083 00084 00085 MS5837ConvertD1(); // start pressure conversion 00086 D1 = MS5837ReadADC(); // read the pressure value 00087 MS5837ConvertD2(); // start temperature conversion 00088 D2 = MS5837ReadADC(); // read the temperature value 00089 //printf("D1 = %d\n", D1); 00090 /* calculation according MS5837-01BA data sheet DA5837-01BA_006 */ 00091 dT = D2 - (C[5]* 256); 00092 OFF = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7); 00093 SENS = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8); 00094 00095 temp = 2000 + (dT * C[6]) / (1<<23); 00096 T_MS5837 = (float) temp / 100.0f; // result of temperature in deg C in this var 00097 press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<13); 00098 P_MS5837 = (float) press / 10.0f; // result of pressure in mBar in this var 00099 00100 if (P_MS5837 < 900 || P_MS5837 > 3000) { 00101 MS5837Reset(); // reset the sensor 00102 MS5837ReadProm(); // read the calibration values 00103 } 00104 } 00105 00106 float MS5837::get_depth_initial(void){ 00107 depth_iter = 0; 00108 Depth_0 = 0; 00109 for(int i = 0; i < 5; i++){ 00110 Barometer_MS5837(); 00111 depth_iter = ((P_MS5837)*1.019716); 00112 Depth_0 = depth_iter + Depth_0; 00113 } 00114 Depth_0 = Depth_0/5; 00115 if(Depth_0 < 1000 || Depth_0 > 1100){ 00116 MS5837Init(); 00117 get_depth_initial(); 00118 } 00119 return Depth_0; 00120 } 00121 00122 float MS5837::get_depth(void) { 00123 depth_iter = 0; 00124 depth = 0; 00125 for(int i = 0; i <= 2; i++){ 00126 Barometer_MS5837(); 00127 depth_iter = ((P_MS5837)*1.019716)-Depth_0; 00128 depth = depth_iter + depth; 00129 } 00130 depth = depth/3; 00131 return depth; 00132 }
Generated on Mon Jul 18 2022 21:44:04 by
1.7.2
