Correction de la lib car erreur de format dans les calculs
Embed:
(wiki syntax)
Show/hide line numbers
MS5803.cpp
00001 /* 00002 Permission is hereby granted, free of charge, to any person obtaining a copy 00003 of this software and associated documentation files (the "Software"), to deal 00004 in the Software without restriction, including without limitation the rights 00005 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00006 copies of the Software, and to permit persons to whom the Software is 00007 furnished to do so, subject to the following conditions: 00008 00009 The above copyright notice and this permission notice shall be included in 00010 all copies or substantial portions of the Software. 00011 00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00013 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00014 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00015 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00016 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00018 THE SOFTWARE. 00019 00020 * Library for Pressure Sensors of type MS5803-x of MEAS Switzerland (www.meas-spec.com). 00021 * The driver uses I2C mode (sensor's Protocol Select (PS) pin pulled to high). 00022 * MS5803-01BA was successfully tested by Raig Kaufer. 00023 * MS5803-14BA (Underwater Pressure Sensor 14 bar) was successfully tested by Robert Katzschmann 00024 * Other types of MEAS are compatible but not tested 00025 * Written by Raig Kaufer, distribute freely! 00026 * Revised by Robert Katzschmann 00027 */ 00028 00029 #include <stdlib.h> 00030 #include "MS5803.h" 00031 00032 00033 /* 00034 * Sensor operating function according data sheet 00035 */ 00036 00037 void MS5803::MS5803Init(void) 00038 { 00039 MS5803Reset(); 00040 MS5803ReadProm(); 00041 return; 00042 } 00043 00044 /* Send soft reset to the sensor */ 00045 void MS5803::MS5803Reset(void) 00046 { 00047 /* transmit out 1 byte reset command */ 00048 ms5803_tx_data[0] = ms5803_reset; 00049 if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); 00050 //printf("send soft reset"); 00051 wait_ms(20); 00052 } 00053 00054 /* read the sensor calibration data from rom */ 00055 void MS5803::MS5803ReadProm(void) 00056 { 00057 uint8_t i,j; 00058 for (i=0; i<8; i++) { 00059 j = i; 00060 ms5803_tx_data[0] = ms5803_PROMread + (j<<1); 00061 if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); 00062 if ( i2c.read( device_address, ms5803_rx_data, 2 ) ); 00063 C[i] = (unsigned short) ((unsigned int)ms5803_rx_data[1] + (((unsigned int)ms5803_rx_data[0])<<8)); 00064 } 00065 } 00066 00067 /* Start the sensor pressure conversion */ 00068 void MS5803::MS5803ConvertD1(void) 00069 { 00070 ms5803_tx_data[0] = ms5803_convD1; 00071 if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); 00072 } 00073 00074 /* Start the sensor temperature conversion */ 00075 void MS5803:: MS5803ConvertD2(void) 00076 { 00077 ms5803_tx_data[0] = ms5803_convD2; 00078 if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); 00079 } 00080 00081 /* Read the previous started conversion results */ 00082 int32_t MS5803::MS5803ReadADC(void) 00083 { 00084 int32_t adc; 00085 wait_ms(150); 00086 ms5803_tx_data[0] = ms5803_ADCread; 00087 if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); 00088 if ( i2c.read( device_address, ms5803_rx_data, 3 ) ); 00089 adc = (unsigned int)ms5803_rx_data[2] + (((unsigned int)ms5803_rx_data[1])<<8) + (((unsigned int)ms5803_rx_data[0])<<16); 00090 return (adc); 00091 } 00092 00093 /* return the results */ 00094 float MS5803::MS5803_Pressure (void) 00095 { 00096 return P_MS5803; 00097 } 00098 float MS5803::MS5803_Temperature (void) 00099 { 00100 return T_MS5803; 00101 } 00102 00103 /* Sensor reading and calculation procedure */ 00104 void MS5803::Barometer_MS5803(void) 00105 { 00106 int32_t dT, temp; 00107 int64_t OFF, SENS, press; 00108 00109 //no need to do this everytime! 00110 //MS5803Reset(); // reset the sensor 00111 //MS5803ReadProm(); // read the calibration values 00112 00113 00114 MS5803ConvertD1(); // start pressure conversion 00115 D1 = MS5803ReadADC(); // read the pressure value 00116 MS5803ConvertD2(); // start temperature conversion 00117 D2 = MS5803ReadADC(); // read the temperature value 00118 00119 /* calculation according MS5803-01BA data sheet DA5803-01BA_006 */ 00120 dT = D2 - (C[5]* 256); 00121 // OFF = (int64_t)C[2] * (1<<18) + ((int64_t)dT * (int64_t)C[4]) / (1<<5); 00122 // SENS = (int64_t)C[1] * (1<<17) + ((int64_t)dT * (int64_t)C[3]) / (1<<6); 00123 OFF = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7); 00124 SENS = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8); 00125 00126 temp = 2000 + ((int64_t)dT * C[6]) / (1<<23); 00127 T_MS5803 = (float) temp / 100.0f; // result of temperature in deg C in this var 00128 press = ((( (((int64_t)D1)&0xFFFFFFFF) * SENS) / (1<<21) - OFF)) / (1<<15); 00129 P_MS5803 = (float)press / 10.; // result of pressure in mBar in this var 00130 }
Generated on Fri Jul 29 2022 16:25:21 by
1.7.2