MS5607 Parallax micro altimeter module interfaces (I2C & SPI)

Dependents:   MS5607Example Vertex_Integration_4 Time_MS5607_Pressure Time_MS5607_Temperature ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MS5607Base.h Source File

MS5607Base.h

00001 /*
00002 Copyright (c) 2012, Senio Networks, Inc.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #ifndef MS5607_BASE_H
00024 #define MS5607_BASE_H
00025 
00026 class MS5607Base {
00027 public:
00028     void printCoefficients() {
00029         printf("%d, %d, %d, %d, %d, %d\n", c1, c2, c3, c4, c5, c6);
00030     }
00031 
00032     int getRawTemperature() {
00033         return readADC(ADC_D2 | OSR_4096);
00034     }
00035 
00036     int getRawPressure() {
00037         return readADC(ADC_D1 | OSR_4096);
00038     }
00039 
00040     float getTemperature() {
00041         int dT = getRawTemperature() - (c5 << 8);
00042         int temp = 2000 + ((dT * c6) >> 23);
00043         
00044         // 2nd order temperature compensation
00045         if (temp < 2000) {
00046             int t2 = (int64_t) dT * dT >> 31;
00047             temp -= t2;
00048         }
00049 
00050         return float(temp) / 100;
00051     }
00052 
00053     float getPressure() {
00054         int dT = getRawTemperature() - (c5 << 8);
00055         int temp = 2000 + ((dT * c6) >> 23);
00056         int64_t off = ((int64_t) c2 << 17) + ((int64_t) dT * c4 >> 6);
00057         int64_t sens = ((int64_t) c1 << 16) + ((int64_t) dT * c3 >> 7);
00058 
00059         // 2nd order temperature compensation
00060         if (temp < 2000) {
00061             int64_t off2 = (int64_t) 61 * (temp - 2000) * (temp - 2000) >> 4;
00062             int64_t sens2 = (int64_t) 2 * (temp - 2000) * (temp - 2000);
00063             if (temp < -1500) {
00064                 off2 += (int64_t) 15 * (temp + 1500) * (temp + 1500);
00065                 sens2 += (int64_t) 8 * (temp + 1500) * (temp + 1500);
00066             }
00067             off -= off2;
00068             sens -= sens2;
00069         }
00070 
00071         return float((((int64_t) getRawPressure() * sens >> 21) - off) >> 15);
00072     }
00073 
00074     float getAltitude(int presssure = 0) {
00075         return toAltitude(presssure ? presssure : (int) getPressure());
00076     }
00077 
00078 protected:
00079     int32_t c1, c2, c3, c4, c5, c6;
00080 
00081     enum {
00082         RESET     = 0x1E,
00083         ADC_READ  = 0x00,
00084         ADC_CONV  = 0x40,
00085         ADC_D1    = 0x00,
00086         ADC_D2    = 0x10,
00087         OSR_256   = 0x00,
00088         OSR_512   = 0x02,
00089         OSR_1024  = 0x04,
00090         OSR_2048  = 0x06,
00091         OSR_4096  = 0x08,
00092         PROM_READ = 0xA0
00093     };
00094 
00095     virtual void writeCommand(int command, int ms = 0) = 0;
00096     virtual int readPROM(int address) = 0;
00097     virtual int readADC(int command) = 0;
00098 
00099     void init() {
00100         writeCommand(RESET, 3);
00101         c1 = readPROM(1);
00102         c2 = readPROM(2);
00103         c3 = readPROM(3);
00104         c4 = readPROM(4);
00105         c5 = readPROM(5);
00106         c6 = readPROM(6);
00107     }
00108 
00109     float toAltitude(int pressure) {
00110         // Ref. 29124-AltimeterAppNote1.pdf
00111         const float R = 287.052; // specific gas constant R*/M0
00112         const float g = 9.80665; // standard gravity 
00113         const float t_grad = 0.0065; // gradient of temperature
00114         const float t0 = 273.15 + 15; // temperature at 0 altitude
00115         const float p0 = 101325; // pressure at 0 altitude
00116 
00117         return t0 / t_grad * (1 - exp((t_grad * R / g) * log(pressure / p0)));
00118     }
00119 };
00120 
00121 #endif