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