Demonstrate the usage of MS5803-01BA Miniature Variometer Module.

Dependencies:   mbed

Committer:
Raig
Date:
Sat Apr 07 14:18:42 2012 +0000
Revision:
0:f0809a7877ff
Demo of MS5803 barometer sensor. Dont forget the pull up resistors of 10kOhm on the SDA and SCL lines!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Raig 0:f0809a7877ff 1 /*
Raig 0:f0809a7877ff 2 Permission is hereby granted, free of charge, to any person obtaining a copy
Raig 0:f0809a7877ff 3 of this software and associated documentation files (the "Software"), to deal
Raig 0:f0809a7877ff 4 in the Software without restriction, including without limitation the rights
Raig 0:f0809a7877ff 5 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Raig 0:f0809a7877ff 6 copies of the Software, and to permit persons to whom the Software is
Raig 0:f0809a7877ff 7 furnished to do so, subject to the following conditions:
Raig 0:f0809a7877ff 8
Raig 0:f0809a7877ff 9 The above copyright notice and this permission notice shall be included in
Raig 0:f0809a7877ff 10 all copies or substantial portions of the Software.
Raig 0:f0809a7877ff 11
Raig 0:f0809a7877ff 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Raig 0:f0809a7877ff 13 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Raig 0:f0809a7877ff 14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Raig 0:f0809a7877ff 15 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Raig 0:f0809a7877ff 16 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Raig 0:f0809a7877ff 17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Raig 0:f0809a7877ff 18 THE SOFTWARE.
Raig 0:f0809a7877ff 19
Raig 0:f0809a7877ff 20 * Barometer Sensor (Altimeter) MS5803-01BA of MEAS Switzerland (www.meas-spec.com).
Raig 0:f0809a7877ff 21 * The driver uses I2C mode (sensor PS pin low).
Raig 0:f0809a7877ff 22 * Other types of MEAS are compatible but not tested.
Raig 0:f0809a7877ff 23 * Written by Raig Kaufer distribute freely!
Raig 0:f0809a7877ff 24 */
Raig 0:f0809a7877ff 25
Raig 0:f0809a7877ff 26 #include <stdlib.h>
Raig 0:f0809a7877ff 27 #include "MS5803.h"
Raig 0:f0809a7877ff 28
Raig 0:f0809a7877ff 29
Raig 0:f0809a7877ff 30 /*
Raig 0:f0809a7877ff 31 * Sensor operating function according data sheet
Raig 0:f0809a7877ff 32 */
Raig 0:f0809a7877ff 33
Raig 0:f0809a7877ff 34 /* Send soft reset to the sensor */
Raig 0:f0809a7877ff 35 void MS5803::MS5803Reset(void) {
Raig 0:f0809a7877ff 36 /* transmit out 1 byte reset command */
Raig 0:f0809a7877ff 37 ms5803_tx_data[0] = ms5803_reset;
Raig 0:f0809a7877ff 38 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
Raig 0:f0809a7877ff 39 wait_ms(20);
Raig 0:f0809a7877ff 40 }
Raig 0:f0809a7877ff 41
Raig 0:f0809a7877ff 42 /* read the sensor calibration data from rom */
Raig 0:f0809a7877ff 43 void MS5803::MS5803ReadProm(void) {
Raig 0:f0809a7877ff 44 uint8_t i,j;
Raig 0:f0809a7877ff 45 for (i=0; i<8; i++) {
Raig 0:f0809a7877ff 46 j = i;
Raig 0:f0809a7877ff 47 ms5803_tx_data[0] = ms5803_PROMread + (j<<1);
Raig 0:f0809a7877ff 48 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
Raig 0:f0809a7877ff 49 if ( i2c.read( device_address, ms5803_rx_data, 2 ) );
Raig 0:f0809a7877ff 50 C[i] = ms5803_rx_data[1] + (ms5803_rx_data[0]<<8);
Raig 0:f0809a7877ff 51 }
Raig 0:f0809a7877ff 52 }
Raig 0:f0809a7877ff 53
Raig 0:f0809a7877ff 54 /* Start the sensor pressure conversion */
Raig 0:f0809a7877ff 55 void MS5803::MS5803ConvertD1(void) {
Raig 0:f0809a7877ff 56 ms5803_tx_data[0] = ms5803_convD1;
Raig 0:f0809a7877ff 57 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
Raig 0:f0809a7877ff 58 }
Raig 0:f0809a7877ff 59
Raig 0:f0809a7877ff 60 /* Start the sensor temperature conversion */
Raig 0:f0809a7877ff 61 void MS5803:: MS5803ConvertD2(void) {
Raig 0:f0809a7877ff 62 ms5803_tx_data[0] = ms5803_convD2;
Raig 0:f0809a7877ff 63 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
Raig 0:f0809a7877ff 64 }
Raig 0:f0809a7877ff 65
Raig 0:f0809a7877ff 66 /* Read the privious started conversion results */
Raig 0:f0809a7877ff 67 int32_t MS5803::MS5803ReadADC(void) {
Raig 0:f0809a7877ff 68 int32_t adc;
Raig 0:f0809a7877ff 69 wait_ms(150);
Raig 0:f0809a7877ff 70 ms5803_tx_data[0] = ms5803_ADCread;
Raig 0:f0809a7877ff 71 if ( i2c.write( device_address, ms5803_tx_data, 1 ) );
Raig 0:f0809a7877ff 72 if ( i2c.read( device_address, ms5803_rx_data, 3 ) );
Raig 0:f0809a7877ff 73 adc = ms5803_rx_data[2] + (ms5803_rx_data[1]<<8) + (ms5803_rx_data[0]<<16);
Raig 0:f0809a7877ff 74 return (adc);
Raig 0:f0809a7877ff 75 }
Raig 0:f0809a7877ff 76
Raig 0:f0809a7877ff 77 /* return the results */
Raig 0:f0809a7877ff 78 float MS5803::MS5803_Pressure (void) {
Raig 0:f0809a7877ff 79 return P_MS5803;
Raig 0:f0809a7877ff 80 }
Raig 0:f0809a7877ff 81 float MS5803::MS5803_Temperature (void) {
Raig 0:f0809a7877ff 82 return T_MS5803;
Raig 0:f0809a7877ff 83 }
Raig 0:f0809a7877ff 84
Raig 0:f0809a7877ff 85 /* Sensor reading and calculation prcedure */
Raig 0:f0809a7877ff 86 void MS5803::Barometer_MS5803(void) {
Raig 0:f0809a7877ff 87 int32_t dT, temp;
Raig 0:f0809a7877ff 88 int64_t OFF, SENS, press;
Raig 0:f0809a7877ff 89
Raig 0:f0809a7877ff 90 MS5803Reset(); // reset the sensor
Raig 0:f0809a7877ff 91 MS5803ReadProm(); // read the calibration values
Raig 0:f0809a7877ff 92 MS5803ConvertD1(); // start pressure convertion
Raig 0:f0809a7877ff 93 D1 = MS5803ReadADC(); // read the pressure value
Raig 0:f0809a7877ff 94 MS5803ConvertD2(); // start temperature convertion
Raig 0:f0809a7877ff 95 D2 = MS5803ReadADC(); // read the temperature value
Raig 0:f0809a7877ff 96
Raig 0:f0809a7877ff 97 /* calculation according MS5803-01BA data sheet DA5803-01BA_006 */
Raig 0:f0809a7877ff 98 dT = D2 - (C[5]* 256);
Raig 0:f0809a7877ff 99 OFF = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7);
Raig 0:f0809a7877ff 100 SENS = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8);
Raig 0:f0809a7877ff 101
Raig 0:f0809a7877ff 102 temp = 2000 + (dT * C[6]) / (1<<23);
Raig 0:f0809a7877ff 103 T_MS5803 = (float) temp / 100.0f; // result of temperature in deg C in this var
Raig 0:f0809a7877ff 104 press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<15);
Raig 0:f0809a7877ff 105 P_MS5803 = (float) press / 100.0f; // result of pressure in mBar in this var
Raig 0:f0809a7877ff 106 }