This is a fork of MS5803-Demo by Raig Kaufer adapted for the MS5803-14BA.
Fork of MS5803-Demo by
Revision 1:52ab0453b01d, committed 2015-04-24
- Comitter:
- frada
- Date:
- Fri Apr 24 18:53:08 2015 +0000
- Parent:
- 0:f0809a7877ff
- Commit message:
- Fork of MS5803-Demo by Raig Kaufer
Changed in this revision
diff -r f0809a7877ff -r 52ab0453b01d MS5803/MS5803.cpp --- a/MS5803/MS5803.cpp Sat Apr 07 14:18:42 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -/* - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - - * Barometer Sensor (Altimeter) MS5803-01BA of MEAS Switzerland (www.meas-spec.com). - * The driver uses I2C mode (sensor PS pin low). - * Other types of MEAS are compatible but not tested. - * Written by Raig Kaufer distribute freely! - */ - -#include <stdlib.h> -#include "MS5803.h" - - -/* - * Sensor operating function according data sheet - */ - -/* Send soft reset to the sensor */ -void MS5803::MS5803Reset(void) { - /* transmit out 1 byte reset command */ - ms5803_tx_data[0] = ms5803_reset; - if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); - wait_ms(20); -} - -/* read the sensor calibration data from rom */ -void MS5803::MS5803ReadProm(void) { - uint8_t i,j; - for (i=0; i<8; i++) { - j = i; - ms5803_tx_data[0] = ms5803_PROMread + (j<<1); - if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); - if ( i2c.read( device_address, ms5803_rx_data, 2 ) ); - C[i] = ms5803_rx_data[1] + (ms5803_rx_data[0]<<8); - } -} - -/* Start the sensor pressure conversion */ -void MS5803::MS5803ConvertD1(void) { - ms5803_tx_data[0] = ms5803_convD1; - if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); -} - -/* Start the sensor temperature conversion */ -void MS5803:: MS5803ConvertD2(void) { - ms5803_tx_data[0] = ms5803_convD2; - if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); -} - -/* Read the privious started conversion results */ -int32_t MS5803::MS5803ReadADC(void) { - int32_t adc; - wait_ms(150); - ms5803_tx_data[0] = ms5803_ADCread; - if ( i2c.write( device_address, ms5803_tx_data, 1 ) ); - if ( i2c.read( device_address, ms5803_rx_data, 3 ) ); - adc = ms5803_rx_data[2] + (ms5803_rx_data[1]<<8) + (ms5803_rx_data[0]<<16); - return (adc); -} - -/* return the results */ -float MS5803::MS5803_Pressure (void) { - return P_MS5803; -} -float MS5803::MS5803_Temperature (void) { - return T_MS5803; -} - -/* Sensor reading and calculation prcedure */ -void MS5803::Barometer_MS5803(void) { - int32_t dT, temp; - int64_t OFF, SENS, press; - - MS5803Reset(); // reset the sensor - MS5803ReadProm(); // read the calibration values - MS5803ConvertD1(); // start pressure convertion - D1 = MS5803ReadADC(); // read the pressure value - MS5803ConvertD2(); // start temperature convertion - D2 = MS5803ReadADC(); // read the temperature value - - /* calculation according MS5803-01BA data sheet DA5803-01BA_006 */ - dT = D2 - (C[5]* 256); - OFF = (int64_t)C[2] * (1<<16) + ((int64_t)dT * (int64_t)C[4]) / (1<<7); - SENS = (int64_t)C[1] * (1<<15) + ((int64_t)dT * (int64_t)C[3]) / (1<<8); - - temp = 2000 + (dT * C[6]) / (1<<23); - T_MS5803 = (float) temp / 100.0f; // result of temperature in deg C in this var - press = (((int64_t)D1 * SENS) / (1<<21) - OFF) / (1<<15); - P_MS5803 = (float) press / 100.0f; // result of pressure in mBar in this var -}
diff -r f0809a7877ff -r 52ab0453b01d MS5803/MS5803.h --- a/MS5803/MS5803.h Sat Apr 07 14:18:42 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -/* - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - - * Barometer Sensor (Altimeter) MS5803-01BA of MEAS Switzerland (www.meas-spec.com). - * The driver uses I2C mode (sensor PS pin low). - * Other types of MEAS are compatible but not tested. - * Written by Raig Kaufer distribute freely! - */ -#include "mbed.h" - -#ifndef MS5803_H -#define MS5803_H - -#define MS5803_RX_DEPTH 3 -#define MS5803_TX_DEPTH 2 - - -#define ms5803_addrCL 0x77 //0b1110111 CSB Pin is low -#define ms5803_addrCH 0x76 //0b1110110 CSB Pin is high - -#define ms5803_base_addr ms5803_addrCL // choose your connection here - -#define ms5803_reset 0x1E // Sensor Reset - -#define ms5803_convD1_256 0x40 // Convert D1 OSR 256 -#define ms5803_convD1_512 0x42 // Convert D1 OSR 512 -#define ms5803_convD1_1024 0x44 // Convert D1 OSR 1024 -#define ms5803_convD1_2048 0x46 // Convert D1 OSR 2048 -#define ms5803_convD1_4096 0x48 // Convert D1 OSR 2048 - -#define ms5803_convD1 ms5803_convD1_4096 // choose your sampling rate here - -#define ms5803_convD2_256 0x50 // Convert D2 OSR 256 -#define ms5803_convD2_512 0x52 // Convert D2 OSR 512 -#define ms5803_convD2_1024 0x54 // Convert D2 OSR 1024 -#define ms5803_convD2_2048 0x56 // Convert D2 OSR 2048 -#define ms5803_convD2_4096 0x58 // Convert D2 OSR 2048 - -#define ms5803_convD2 ms5803_convD2_4096 // choose your sampling rate here - -#define ms5803_ADCread 0x00 // read ADC command -#define ms5803_PROMread 0xA0 // read PROM command base address - -class MS5803 : public Base { -private: - int D1, D2, Temp, C[8]; - float T_MS5803, P_MS5803; - /* Data buffers */ - char ms5803_rx_data[MS5803_RX_DEPTH]; - char ms5803_tx_data[MS5803_TX_DEPTH]; - -public: - MS5803 (PinName sda, PinName scl, - char ms5803_addr = ms5803_base_addr ) - : i2c( sda, scl ), device_address( ms5803_addr << 1 ) { - } - void MS5803Reset(void); - void MS5803ReadProm(void); - void MS5803ConvertD1(void); - void MS5803ConvertD2(void); - int32_t MS5803ReadADC(void); - float MS5803_Pressure (void); - float MS5803_Temperature (void); - void Barometer_MS5803(void); - - -private: - I2C i2c; - char device_address; - -}; -#endif
diff -r f0809a7877ff -r 52ab0453b01d MS5803_14BA/MS5803_14BA.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MS5803_14BA/MS5803_14BA.cpp Fri Apr 24 18:53:08 2015 +0000 @@ -0,0 +1,114 @@ +/* + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + + * Demo Program + * Barometer Sensor (Altimeter) MS5803-01BA of MEAS Switzerland (www.meas-spec.com). + * The driver uses I2C mode (sensor PS pin low). + * Other types of MEAS are compatible but not tested. + * Written by Raig Kaufer distribute freely! + * + * Modified for MS5803-14BA by Francesco Adamo, Italy + */ + +#include <stdlib.h> +#include "MS5803_14BA.h" + +MS5803_14BA::MS5803_14BA(PinName sda, PinName scl, char MS5803_ADDR) : _i2c(sda, scl) { + device_address = MS5803_ADDR << 1; + _d1_osr = D1_OSR_4096; + _d2_osr = D2_OSR_4096; + reset(); // reset the sensor + readPROM(); // read the calibration values +} + +MS5803_14BA::MS5803_14BA(PinName sda, PinName scl, char MS5803_ADDR, char d1_osr, char d2_osr) : _i2c( sda, scl ) { + device_address = MS5803_ADDR << 1; + _d1_osr = D1_OSR_4096; + _d2_osr = D2_OSR_4096; + reset(); // reset the sensor + readPROM(); // read the calibration values and store them in array C[ ] +} + +/* Send soft reset to the sensor */ +void MS5803_14BA::reset(void) { + MS5803_tx_data[0] = COMMAND_RESET; + _i2c.write(device_address, MS5803_tx_data, 1); + wait_ms(20); +} + +/* read the sensor calibration data from ROM */ +void MS5803_14BA::readPROM(void) { + for (uint8_t i = 0; i < 8; i++) { + MS5803_tx_data[0] = COMMAND_READ_PROM + (i << 1); + _i2c.write( device_address, MS5803_tx_data, 1); + _i2c.read( device_address, MS5803_rx_data, 2); + C[i] = (MS5803_rx_data[0] << 8) + MS5803_rx_data[1]; + } +} + +/* Start the sensor pressure conversion */ +void MS5803_14BA::convertD1(void) { + MS5803_tx_data[0] = _d1_osr; + _i2c.write( device_address, MS5803_tx_data, 1); +} + +/* Start the sensor temperature conversion */ +void MS5803_14BA::convertD2(void) { + MS5803_tx_data[0] = _d2_osr; + _i2c.write(device_address, MS5803_tx_data, 1); +} + +/* Read the previous started conversion results */ +int32_t MS5803_14BA::readADC(void) { + wait_ms(150); + MS5803_tx_data[0] = COMMAND_READ_ADC; + _i2c.write(device_address, MS5803_tx_data, 1); + _i2c.read(device_address, MS5803_rx_data, 3); + return (MS5803_rx_data[0] << 16) + (MS5803_rx_data[1] << 8) + MS5803_rx_data[2]; +} + +/* return the results */ +float MS5803_14BA::getPressure(void) { + return P_MS5803; +} + +float MS5803_14BA::getTemperature (void) { + return T_MS5803; +} + +/* Sensor reading and calculation procedure */ +void MS5803_14BA::convert(void) { + int32_t dT, TEMP; + int64_t OFF, SENS, P; + + convertD1(); // start pressure convertion + D1 = readADC(); // read the pressure value + convertD2(); // start temperature convertion + D2 = readADC(); // read the temperature value + + /* calculation according MS5803-14BA data sheet */ + dT = D2 - (C[5]* 256); + TEMP = 2000 + (dT * C[6])/8388608; + T_MS5803 = (float) TEMP/100.0f; // result of temperature in deg C in this var + + OFF = (int64_t)65536*C[2] + ((int64_t)dT * (int64_t)C[4])/128; + SENS = (int64_t)32768*C[1] + ((int64_t)dT * (int64_t)C[3])/256; + P = (((int64_t)D1 * SENS)/2097152 - OFF)/32768; + P_MS5803 = (float) P/10.0f; // result of pressure in mBar in this var +}
diff -r f0809a7877ff -r 52ab0453b01d MS5803_14BA/MS5803_14BA.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MS5803_14BA/MS5803_14BA.h Fri Apr 24 18:53:08 2015 +0000 @@ -0,0 +1,85 @@ +/* + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + + * Demo Program + * Barometer Sensor (Altimeter) MS5803-01BA of MEAS Switzerland (www.meas-spec.com). + * The driver uses I2C mode (sensor PS pin low). + * Other types of MEAS are compatible but not tested. + * Written by Raig Kaufer distribute freely! + * + * Modified for MS5803-14BA by Francesco Adamo, Italy + */ + +#include "mbed.h" + +#ifndef MS5803_14BA_H +#define MS5803_14BA_H + +#define MS5803_RX_DEPTH 3 +#define MS5803_TX_DEPTH 2 + + +#define MS5803_ADDRC_L 0x77 //0b1110111 CSB Pin is low +#define MS5803_ADDRC_H 0x76 //0b1110110 CSB Pin is high + +#define MS5803_BASE_ADDR MS5803_ADDRC_L // choose your connection here + +#define COMMAND_RESET 0x1E // Sensor Reset + +#define D1_OSR_256 0x40 // Convert D1 OSR 256 +#define D1_OSR_512 0x42 // Convert D1 OSR 512 +#define D1_OSR_1024 0x44 // Convert D1 OSR 1024 +#define D1_OSR_2048 0x46 // Convert D1 OSR 2048 +#define D1_OSR_4096 0x48 // Convert D1 OSR 4096 + +#define D2_OSR_256 0x50 // Convert D2 OSR 256 +#define D2_OSR_512 0x52 // Convert D2 OSR 512 +#define D2_OSR_1024 0x54 // Convert D2 OSR 1024 +#define D2_OSR_2048 0x56 // Convert D2 OSR 2048 +#define D2_OSR_4096 0x58 // Convert D2 OSR 4096 + +#define COMMAND_READ_ADC 0x00 // read ADC command +#define COMMAND_READ_PROM 0xA0 // read PROM command base address + +class MS5803_14BA { +private: + I2C _i2c; + char device_address, _d1_osr, _d2_osr; + + uint32_t D1, D2; + uint16_t C[8]; + float T_MS5803, P_MS5803; + /* Data buffers */ + char MS5803_rx_data[MS5803_RX_DEPTH]; + char MS5803_tx_data[MS5803_TX_DEPTH]; + +public: + MS5803_14BA(PinName sda, PinName scl, char MS5803_ADDR); + MS5803_14BA(PinName sda, PinName scl, char MS5803_ADDR, char d1_osr, char d2_osr); + + void reset(void); + void readPROM(void); + void convertD1(void); + void convertD2(void); + int32_t readADC(void); + float getPressure(void); + float getTemperature(void); + void convert(void); +}; +#endif
diff -r f0809a7877ff -r 52ab0453b01d USBDevice.lib --- a/USBDevice.lib Sat Apr 07 14:18:42 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/samux/code/USBDevice/#5e24508aa46e
diff -r f0809a7877ff -r 52ab0453b01d main.cpp --- a/main.cpp Sat Apr 07 14:18:42 2012 +0000 +++ b/main.cpp Fri Apr 24 18:53:08 2015 +0000 @@ -22,34 +22,40 @@ * The driver uses I2C mode (sensor PS pin low). * Other types of MEAS are compatible but not tested. * Written by Raig Kaufer distribute freely! + * + * Modified for MS5803-14BA by Francesco Adamo, Italy */ #include "mbed.h" -#include "MS5803.h" -#include "USBSerial.h" +#include "MS5803_14BA.h" + -#define debug -#ifdef debug -USBSerial serial; -Serial pc(USBTX, USBRX); +#ifdef TARGET_K64F + #define SPI_SCK PTD1 + #define SPI_MOSI PTD2 + #define SPI_MISO PTD3 + #define SPI_CS PTD0 + #define I2C_SDA PTE25 + #define I2C_SCL PTE24 +#elif defined(TARGET_KL25Z) + #define SPI_SCK PTD1 + #define SPI_MOSI PTD2 + #define SPI_MISO PTD3 + #define SPI_CS PTD0 + #define I2C_SDA PTE0 + #define I2C_SCL PTE1 #endif -MS5803 press_sensor( p9, p10, ms5803_base_addr ); // sda, scl, I2C_address 0x76 or 0x77 look at MS5803.h +Serial pc(USBTX, USBRX); +MS5803_14BA ps(I2C_SDA, I2C_SCL, MS5803_ADDRC_H, D1_OSR_4096, D2_OSR_4096); // SDA, SCL, I2C_address (0x76 or 0x77 look at MS5803.h) int main() { - -#ifdef debug - wait( 3 ); - serial.printf("MS5803 demo"); -#endif - - while ( 1 ) { -#ifdef debug - press_sensor.Barometer_MS5803(); - serial.printf("%4.1f (mBar)\r\n", press_sensor.MS5803_Pressure()); - serial.printf("%4.1f (deg C)\r\n", press_sensor.MS5803_Temperature()); -#endif - - wait( 1 ); + pc.baud(115200); + pc.printf("MS5803 demo\r\n"); + + while (1) { + ps.convert(); + pc.printf("%4.1f, %4.1f\r\n", ps.getPressure(), ps.getTemperature()); + wait(0.5); } }
diff -r f0809a7877ff -r 52ab0453b01d mbed.bld --- a/mbed.bld Sat Apr 07 14:18:42 2012 +0000 +++ b/mbed.bld Fri Apr 24 18:53:08 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/b3c9f16cbb96 +http://mbed.org/users/mbed_official/code/mbed/builds/433970e64889 \ No newline at end of file