Raig Kaufer / Mbed 2 deprecated MS5803-Demo

Dependencies:   mbed

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   * Barometer Sensor (Altimeter) MS5803-01BA of MEAS Switzerland     (www.meas-spec.com).
00021   * The driver uses I2C mode (sensor PS pin low).
00022   * Other types of MEAS are compatible but not tested.
00023   * Written by Raig Kaufer distribute freely!
00024  */
00025 
00026 #include <stdlib.h>
00027 #include "MS5803.h"
00028 
00029 
00030 /*
00031  * Sensor operating function according data sheet
00032  */
00033 
00034 /* Send soft reset to the sensor */
00035 void MS5803::MS5803Reset(void) {
00036     /* transmit out 1 byte reset command */
00037     ms5803_tx_data[0] = ms5803_reset;
00038     if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00039     wait_ms(20);
00040 }
00041 
00042 /* read the sensor calibration data from rom */
00043 void MS5803::MS5803ReadProm(void) {
00044     uint8_t i,j;
00045     for (i=0; i<8; i++) {
00046         j = i;
00047         ms5803_tx_data[0] = ms5803_PROMread + (j<<1);
00048         if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00049         if ( i2c.read( device_address,  ms5803_rx_data, 2 ) );
00050         C[i]   = ms5803_rx_data[1] + (ms5803_rx_data[0]<<8);
00051     }
00052 }
00053 
00054 /* Start the sensor pressure conversion */
00055 void MS5803::MS5803ConvertD1(void) {
00056     ms5803_tx_data[0] = ms5803_convD1;
00057     if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00058 }
00059 
00060 /* Start the sensor temperature conversion */
00061 void MS5803:: MS5803ConvertD2(void) {
00062     ms5803_tx_data[0] = ms5803_convD2;
00063     if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00064 }
00065 
00066 /* Read the privious started conversion results */
00067 int32_t MS5803::MS5803ReadADC(void) {
00068     int32_t adc;
00069     wait_ms(150);
00070     ms5803_tx_data[0] = ms5803_ADCread;
00071     if ( i2c.write( device_address,  ms5803_tx_data, 1 ) );
00072     if ( i2c.read( device_address,  ms5803_rx_data, 3 ) );
00073     adc = ms5803_rx_data[2] + (ms5803_rx_data[1]<<8) + (ms5803_rx_data[0]<<16);
00074     return (adc);
00075 }
00076 
00077 /* return the results */
00078 float MS5803::MS5803_Pressure (void) {
00079     return P_MS5803;
00080 }
00081 float MS5803::MS5803_Temperature (void) {
00082     return T_MS5803;
00083 }
00084 
00085 /* Sensor reading and calculation prcedure */
00086 void MS5803::Barometer_MS5803(void) {
00087     int32_t dT, temp;
00088     int64_t OFF, SENS, press;
00089 
00090     MS5803Reset();                 // reset the sensor
00091     MS5803ReadProm();             // read the calibration values
00092     MS5803ConvertD1();             // start pressure convertion
00093     D1 = MS5803ReadADC();        // read the pressure value
00094     MS5803ConvertD2();             // start temperature convertion
00095     D2 = MS5803ReadADC();         // read the temperature value
00096 
00097     /* calculation according MS5803-01BA data sheet DA5803-01BA_006 */
00098     dT       = D2 - (C[5]* 256);
00099     OFF      = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7);
00100     SENS     = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8);
00101 
00102     temp     = 2000 + (dT * C[6]) / (1<<23);
00103     T_MS5803 = (float) temp / 100.0f;                 // result of temperature in deg C in this var
00104     press    = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<15);
00105     P_MS5803 = (float) press / 100.0f;                 // result of pressure in mBar in this var
00106 }