MS5607 Parallax micro altimeter module interfaces (I2C & SPI)
Dependents: MS5607Example Vertex_Integration_4 Time_MS5607_Pressure Time_MS5607_Temperature ... more
Revision 0:5760862143d1, committed 2012-07-03
- Comitter:
- yamaguch
- Date:
- Tue Jul 03 08:57:57 2012 +0000
- Commit message:
- 0.92
Changed in this revision
diff -r 000000000000 -r 5760862143d1 MS5607Base.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MS5607Base.h Tue Jul 03 08:57:57 2012 +0000 @@ -0,0 +1,121 @@ +/* +Copyright (c) 2012, Senio Networks, Inc. + +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. +*/ + +#ifndef MS5607_BASE_H +#define MS5607_BASE_H + +class MS5607Base { +public: + void printCoefficients() { + printf("%d, %d, %d, %d, %d, %d\n", c1, c2, c3, c4, c5, c6); + } + + int getRawTemperature() { + return readADC(ADC_D2 | OSR_4096); + } + + int getRawPressure() { + return readADC(ADC_D1 | OSR_4096); + } + + float getTemperature() { + int dT = getRawTemperature() - (c5 << 8); + int temp = 2000 + ((dT * c6) >> 23); + + // 2nd order temperature compensation + if (temp < 2000) { + int t2 = (int64_t) dT * dT >> 31; + temp -= t2; + } + + return float(temp) / 100; + } + + float getPressure() { + int dT = getRawTemperature() - (c5 << 8); + int temp = 2000 + ((dT * c6) >> 23); + int64_t off = ((int64_t) c2 << 17) + ((int64_t) dT * c4 >> 6); + int64_t sens = ((int64_t) c1 << 16) + ((int64_t) dT * c3 >> 7); + + // 2nd order temperature compensation + if (temp < 2000) { + int64_t off2 = (int64_t) 61 * (temp - 2000) * (temp - 2000) >> 4; + int64_t sens2 = (int64_t) 2 * (temp - 2000) * (temp - 2000); + if (temp < -1500) { + off2 += (int64_t) 15 * (temp + 1500) * (temp + 1500); + sens2 += (int64_t) 8 * (temp + 1500) * (temp + 1500); + } + off -= off2; + sens -= sens2; + } + + return float((((int64_t) getRawPressure() * sens >> 21) - off) >> 15); + } + + float getAltitude(int presssure = 0) { + return toAltitude(presssure ? presssure : (int) getPressure()); + } + +protected: + int32_t c1, c2, c3, c4, c5, c6; + + enum { + RESET = 0x1E, + ADC_READ = 0x00, + ADC_CONV = 0x40, + ADC_D1 = 0x00, + ADC_D2 = 0x10, + OSR_256 = 0x00, + OSR_512 = 0x02, + OSR_1024 = 0x04, + OSR_2048 = 0x06, + OSR_4096 = 0x08, + PROM_READ = 0xA0 + }; + + virtual void writeCommand(int command, int ms = 0) = 0; + virtual int readPROM(int address) = 0; + virtual int readADC(int command) = 0; + + void init() { + writeCommand(RESET, 3); + c1 = readPROM(1); + c2 = readPROM(2); + c3 = readPROM(3); + c4 = readPROM(4); + c5 = readPROM(5); + c6 = readPROM(6); + } + + float toAltitude(int pressure) { + // Ref. 29124-AltimeterAppNote1.pdf + const float R = 287.052; // specific gas constant R*/M0 + const float g = 9.80665; // standard gravity + const float t_grad = 0.0065; // gradient of temperature + const float t0 = 273.15 + 15; // temperature at 0 altitude + const float p0 = 101325; // pressure at 0 altitude + + return t0 / t_grad * (1 - exp((t_grad * R / g) * log(pressure / p0))); + } +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r 5760862143d1 MS5607I2C.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MS5607I2C.h Tue Jul 03 08:57:57 2012 +0000 @@ -0,0 +1,71 @@ +/* +Copyright (c) 2012, Senio Networks, Inc. + +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. +*/ + +#ifndef MS5607_I2C_H +#define MS5607_I2C_H + +#include "MS5607Base.h" + +class MS5607I2C : public MS5607Base { +public: + MS5607I2C(PinName sda, PinName scl, int csb = 0) : i2c(sda, scl), i2cAddr(csb ? 0xEC : 0xEE) { + init(); + } + +private: + I2C i2c; + int i2cAddr; + + virtual void writeCommand(int command, int ms = 0) { + char buf[1] = {command}; + i2c.write(i2cAddr, buf, 1); + if (ms) wait_ms(ms); + } + + virtual int readPROM(int address) { + char buf[2] = {PROM_READ | (address << 1), 0}; + + if (i2c.write(i2cAddr, buf, 1) == 0 && + i2c.read(i2cAddr, buf, 2) == 0) + return buf[0] << 8 | buf[1]; + + return -1; + } + + virtual int readADC(int command) { + char cmd[] = {ADC_CONV | command}; + + if (i2c.write(i2cAddr, cmd, sizeof(cmd)) == 0) { + static int duration[] = {500, 1100, 2100, 4100, 8220}; + wait_us(duration[(command & 0x0F) >> 1]); + cmd[0] = ADC_READ; + char buf[3]; + if (i2c.write(i2cAddr, cmd, sizeof(cmd)) == 0 && + i2c.read(i2cAddr, buf, sizeof(buf)) == 0) + return buf[0] << 16 | buf[1] << 8 | buf[2]; + } + + return -1; + } +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r 5760862143d1 MS5607SPI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MS5607SPI.h Tue Jul 03 08:57:57 2012 +0000 @@ -0,0 +1,70 @@ +/* +Copyright (c) 2012, Senio Networks, Inc. + +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. +*/ + +#ifndef MS5607_SPI_H +#define MS5607_SPI_H + +#include "MS5607Base.h" + +class MS5607SPI : public MS5607Base { +public: + MS5607SPI(PinName mosi, PinName miso, PinName sclk, PinName csb) : spi(mosi, miso, sclk), csb(csb) { + init(); + } + +private: + SPI spi; + DigitalOut csb; + + virtual void writeCommand(int command, int ms = 0) { + csb = 0; + spi.write(command); + if (ms) wait_ms(ms); + csb = 1; + } + + virtual int readPROM(int address) { + csb = 0; + spi.write(PROM_READ | address << 1); + int hi = spi.write(0); + int low = spi.write(0); + csb = 1; + return hi << 8 | low; + } + + virtual int readADC(int command) { + csb = 0; + spi.write(ADC_CONV | command); + static int duration[] = {500, 1100, 2100, 4100, 8220}; + wait_us(duration[(command & 0x0F) >> 1]); + csb = 1; + csb = 0; + spi.write(ADC_READ); + int hi = spi.write(0); + int mid = spi.write(0); + int low = spi.write(0); + csb = 1; + return hi << 16 | mid << 8 | low; + } +}; + +#endif \ No newline at end of file