Library and demo program for MS4525DO differential pressure sensor based pitot tube, using I2C interface

Dependencies:   mbed

Temperature values are correct. But I dont trust the PSI and airspeed (especially airspeed).

Code blended and ported from a combination of the following sources:

Committer:
epremeaux
Date:
Fri Sep 13 08:41:37 2019 +0000
Revision:
0:5965bf423184
initial commit. I dont trust the output values yet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
epremeaux 0:5965bf423184 1
epremeaux 0:5965bf423184 2 #ifndef MS4525DO_H
epremeaux 0:5965bf423184 3 #define MS4525DO_H
epremeaux 0:5965bf423184 4
epremeaux 0:5965bf423184 5 #include "mbed.h"
epremeaux 0:5965bf423184 6
epremeaux 0:5965bf423184 7 #define I2C_ADDRESS_MS4525DO 0x28 /**< 7-bit address =0x28. 8-bit is 0x50. Depends on the order code (this is for code "I") */
epremeaux 0:5965bf423184 8
epremeaux 0:5965bf423184 9 /* Register address */
epremeaux 0:5965bf423184 10 #define ADDR_READ_MR 0x00 /* write to this address to start conversion */
epremeaux 0:5965bf423184 11
epremeaux 0:5965bf423184 12 // MS4525D sensor full scale range and units
epremeaux 0:5965bf423184 13 const int16_t MS4525FullScaleRange = 1; // 1 psi
epremeaux 0:5965bf423184 14
epremeaux 0:5965bf423184 15 // MS4525D Sensor type (A or B) comment out the wrong type assignments
epremeaux 0:5965bf423184 16 // Type A (10% to 90%)
epremeaux 0:5965bf423184 17 const int16_t MS4525MinScaleCounts = 1638;
epremeaux 0:5965bf423184 18 const int16_t MS4525FullScaleCounts = 14746;
epremeaux 0:5965bf423184 19
epremeaux 0:5965bf423184 20 const int16_t MS4525Span=MS4525FullScaleCounts-MS4525MinScaleCounts;
epremeaux 0:5965bf423184 21
epremeaux 0:5965bf423184 22 //MS4525D sensor pressure style, differential or otherwise. Comment out the wrong one.
epremeaux 0:5965bf423184 23 //Differential
epremeaux 0:5965bf423184 24 const int16_t MS4525ZeroCounts=(MS4525MinScaleCounts+MS4525FullScaleCounts)/2;
epremeaux 0:5965bf423184 25
epremeaux 0:5965bf423184 26
epremeaux 0:5965bf423184 27 class MS4525DO
epremeaux 0:5965bf423184 28 {
epremeaux 0:5965bf423184 29 public:
epremeaux 0:5965bf423184 30 // instance methods
epremeaux 0:5965bf423184 31 // explicit I2C pin names
epremeaux 0:5965bf423184 32 MS4525DO(PinName sda, PinName sck, char slave_adr = I2C_ADDRESS_MS4525DO);
epremeaux 0:5965bf423184 33
epremeaux 0:5965bf423184 34 // pin names as an object (ideal when sharing the bus)
epremeaux 0:5965bf423184 35 MS4525DO(I2C &i2c_obj, char slave_adr = I2C_ADDRESS_MS4525DO);
epremeaux 0:5965bf423184 36
epremeaux 0:5965bf423184 37 virtual ~MS4525DO();
epremeaux 0:5965bf423184 38
epremeaux 0:5965bf423184 39 // public functions
epremeaux 0:5965bf423184 40 void initialize(void);
epremeaux 0:5965bf423184 41 int measure(void); // returns status of measurement
epremeaux 0:5965bf423184 42 float getPSI(void); // returns the PSI of last measurement
epremeaux 0:5965bf423184 43 float getTemperature(void); // returns temperature of last measurement
epremeaux 0:5965bf423184 44 float getAirSpeed(void); // calculates and returns the airspeed
epremeaux 0:5965bf423184 45 int calibrate(void); // attempts to calibrate and returns a status code
epremeaux 0:5965bf423184 46
epremeaux 0:5965bf423184 47
epremeaux 0:5965bf423184 48 private:
epremeaux 0:5965bf423184 49 I2C *i2c_p;
epremeaux 0:5965bf423184 50 I2C &i2c;
epremeaux 0:5965bf423184 51 char address;
epremeaux 0:5965bf423184 52 char _status;
epremeaux 0:5965bf423184 53 float psi;
epremeaux 0:5965bf423184 54 float temperature;
epremeaux 0:5965bf423184 55 float airspeed;
epremeaux 0:5965bf423184 56 uint16_t P_dat; // 14 bit pressure data
epremeaux 0:5965bf423184 57 uint16_t T_dat; // 11 bit temperature data
epremeaux 0:5965bf423184 58
epremeaux 0:5965bf423184 59 // private functions
epremeaux 0:5965bf423184 60 int collect(void);
epremeaux 0:5965bf423184 61 char fetch_pressure(uint16_t &P_dat, uint16_t &T_dat);
epremeaux 0:5965bf423184 62
epremeaux 0:5965bf423184 63
epremeaux 0:5965bf423184 64 }; // end of the class
epremeaux 0:5965bf423184 65
epremeaux 0:5965bf423184 66
epremeaux 0:5965bf423184 67 /** airspeed scaling factors; out = (in * Vscale) + offset */
epremeaux 0:5965bf423184 68 struct airspeed_scale {
epremeaux 0:5965bf423184 69 float offset_pa;
epremeaux 0:5965bf423184 70 float scale;
epremeaux 0:5965bf423184 71 };
epremeaux 0:5965bf423184 72
epremeaux 0:5965bf423184 73 #endif