Moraa Onwonga / SaveAirPressureDataInTextFile
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MS5803.cpp Source File

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 /* Send soft reset to the sensor */
00038 void MS5803::MS5803Reset(void) {
00039     /* transmit out 1 byte reset command */
00040     ms5803_tx_data[0] = ms5803_reset;
00041     if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00042     //printf("send soft reset");
00043     wait_ms(20);
00044 }
00045 
00046 /* read the sensor calibration data from rom */
00047 void MS5803::MS5803ReadProm(void) {
00048     uint8_t i,j;
00049     for (i=0; i<8; i++) {
00050         j = i;
00051         ms5803_tx_data[0] = ms5803_PROMread + (j<<1);
00052         if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00053         if ( i2c.read( device_address,  ms5803_rx_data, 2 ) );
00054         C[i]   = ms5803_rx_data[1] + (ms5803_rx_data[0]<<8);
00055     }
00056 }
00057 
00058 /* Start the sensor pressure conversion */
00059 void MS5803::MS5803ConvertD1(void) {
00060     ms5803_tx_data[0] = ms5803_convD1;
00061     if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00062 }
00063 
00064 /* Start the sensor temperature conversion */
00065 void MS5803:: MS5803ConvertD2(void) {
00066     ms5803_tx_data[0] = ms5803_convD2;
00067     if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00068 }
00069 
00070 /* Read the privious started conversion results */
00071 int32_t MS5803::MS5803ReadADC(void) {
00072     int32_t adc;
00073     wait_ms(150);
00074     ms5803_tx_data[0] = ms5803_ADCread;
00075     if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00076     if ( i2c.read( device_address,  ms5803_rx_data, 3 ) );
00077     adc = ms5803_rx_data[2] + (ms5803_rx_data[1]<<8) + (ms5803_rx_data[0]<<16);
00078     return (adc);
00079 }
00080 
00081 /* return the results */
00082 float MS5803::MS5803_Pressure (void) {
00083     return P_MS5803;
00084 }
00085 float MS5803::MS5803_Temperature (void) {
00086     return T_MS5803;
00087 }
00088 
00089 /* Sensor reading and calculation procedure */
00090 void MS5803::Barometer_MS5803(void) {
00091     int32_t dT, temp;
00092     int64_t OFF, SENS, press;
00093 
00094     MS5803Reset();                 // reset the sensor
00095     MS5803ReadProm();             // read the calibration values
00096     MS5803ConvertD1();             // start pressure conversion
00097     D1 = MS5803ReadADC();        // read the pressure value
00098     MS5803ConvertD2();             // start temperature conversion
00099     D2 = MS5803ReadADC();         // read the temperature value
00100 
00101     /* calculation according MS5803-01BA data sheet DA5803-01BA_006 */
00102     dT       = D2 - (C[5]* 256);
00103     OFF      = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7);
00104     SENS     = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8);
00105 
00106     temp     = 2000 + (dT * C[6]) / (1<<23);
00107     T_MS5803 = (float) temp / 100.0f;                 // result of temperature in deg C in this var
00108     press    = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<15);
00109     P_MS5803 = (float) press / 100.0f;                 // result of pressure in mBar in this var
00110 }