Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Revision 2:4d9204790be1, committed 2020-05-19
- Comitter:
- altb2
- Date:
- Tue May 19 13:13:03 2020 +0000
- Parent:
- 1:a94ff5f08fd7
- Commit message:
- MS56 Baro-sensor with toggling Temper. and pressure overe i2c;
Changed in this revision
--- a/MS5611.lib Fri Feb 21 07:30:50 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/users/altb2/code/MS5611/#10c65e9fcc8d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MS5611/MS5611Base.h Tue May 19 13:13:03 2020 +0000 @@ -0,0 +1,194 @@ +/* +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. +*/ + +/* MS5611 Modifications +Aerodyne Labs +Matthew Nelson - 2014 +This library was originally the MS5607 library. It has been modified to +to be used the MS5611 Pressure sensor +------------ +IndNav: Made adaptions, even though, the original works well, problem: +the original has a delay of 8ms from writing to reading (see line 73 in MS5611I2c +Solution: seperate write and read in 2 processes (hene's hint): + - write at end of data_read thread, without waiting + - at beginning of next call, read the data +since there are 2 seperate reading/writings (one for pressure(altitude), one for Temperature -and we need both!) +toggle the 2 processes, this results, that only every 2nd pressure/altitude value is a new value! + +altb 20.2.2020 +*/ + +#ifndef MS5611_BASE_H +#define MS5611_BASE_H + +class MS5611Base { +public: + void printCoefficients() { + printf("%d, %d, %d, %d, %d, %d \r\n", c1, c2, c3, c4, c5, c6); + } + + int getRawPressure() { + return readADC(ADC_D1 | OSR_4096); // fuer 1024: gibt das 4 + } + int getRawPressure_read() { + return readADC_read(ADC_D1 | OSR_4096); // fuer 1024: gibt das 4 + } + int getRawTemperature() { + return readADC(ADC_D2 | OSR_4096); // fuer 1024: gibt das 20 + } + int getRawTemperature_read() { + return readADC_read(ADC_D2 | OSR_4096); // fuer 1024: gibt das 20 + } + + 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 << 16) + ((int64_t) dT * c4 >> 7); + int64_t sens = ((int64_t) c1 << 15) + ((int64_t) dT * c3 >> 8); + + // 2nd order temperature compensation + if (temp < 2000) { + int64_t off2 = (int64_t) 5 * (temp - 2000) * (temp - 2000) >> 1; + int64_t sens2 = (int64_t) 5 * (temp - 2000) * (temp - 2000) >> 2; + if (temp < -1500) { + off2 += (int64_t) 7 * (temp + 1500) * (temp + 1500); + sens2 += (int64_t) 11 * (temp + 1500) * (temp + 1500) >> 1; + } + off -= off2; + sens -= sens2; + } + + return float((((int64_t) getRawPressure() * sens >> 21) - off) >> 15); + } + float getPressure(int dT) { +// int dT = getRawTemperature() - (c5 << 8); + int temp = 2000 + ((dT * c6) >> 23); + int64_t off = ((int64_t) c2 << 16) + ((int64_t) dT * c4 >> 7); + int64_t sens = ((int64_t) c1 << 15) + ((int64_t) dT * c3 >> 8); + + // 2nd order temperature compensation + if (temp < 2000) { + int64_t off2 = (int64_t) 5 * (temp - 2000) * (temp - 2000) >> 1; + int64_t sens2 = (int64_t) 5 * (temp - 2000) * (temp - 2000) >> 2; + if (temp < -1500) { + off2 += (int64_t) 7 * (temp + 1500) * (temp + 1500); + sens2 += (int64_t) 11 * (temp + 1500) * (temp + 1500) >> 1; + } + off -= off2; + sens -= sens2; + } + + return float((((int64_t) getRawPressure() * sens >> 21) - off) >> 15); + } + + float getAltitude(int presssure = 0) { + return toAltitude(presssure ? presssure : (int) getPressure()); + } + + float getAltitude_toggle_Temp_read(int presssure = 0) { + if(temp_toggle) + { + dT_keep = getRawTemperature_read() - (c5 << 8); + temp_toggle = false; + } + else + { + temp_toggle = true; + old_alt = toAltitude(presssure ? presssure : (int) getPressure(dT_keep)); + } + return old_alt; + } + + int getAltitude_toggle_Temp_write(int presssure = 0) { + if(temp_toggle) + { + return readADC_write(ADC_D2 | OSR_4096); + } + else + { + return readADC_write(ADC_D1 | OSR_4096); + } + } + int dT_keep; +protected: + int32_t c1, c2, c3, c4, c5, c6; + bool temp_toggle; + float old_alt; // get altitude only every 2nd time. + 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; + virtual int readADC_write(int command) = 0; + virtual int readADC_read(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); + dT_keep = 2000; + temp_toggle = true; + old_alt = 0.0; + } + + 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MS5611/MS5611I2C.h Tue May 19 13:13:03 2020 +0000 @@ -0,0 +1,103 @@ +/* +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. +*/ + +/* MS5611 Modifications +Aerodyne Labs +Matthew Nelson - 2014 +This library was originally the MS5607 library. It has been modified to +to be used the MS5611 Pressure sensor +*/ + +#ifndef MS5611_I2C_H +#define MS5611_I2C_H + +#include "MS5611Base.h" + +extern Serial pc; + +class MS5611I2C : public MS5611Base { +public: + MS5611I2C(I2C &i2c, int csb = 0) : i2c(i2c), i2cAddr(csb ? 0xEC : 0xEE) { + wait(.01); + i2c.frequency(400000); + wait(.01); + init(); + ADC_toggler = false; + } + +private: + I2C &i2c; + int i2cAddr; + bool ADC_toggler; + + virtual void writeCommand(int command, int ms = 0) { + char buf[1]; + buf[0] = command; + i2c.write(i2cAddr, buf, 1); + if (ms) wait_ms(ms); + } + + virtual int readPROM(int address) { + char buf[2]; + buf[0] = (PROM_READ | (address << 1)); + + 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[1]; + cmd[0] = (ADC_CONV | command); + if (i2c.write(i2cAddr, cmd, 1) == 0) { + static int duration[] = {500, 1100, 2100, 4100, 8220}; // OSR_4096 + 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; + } + virtual int readADC_write(int command) { + char cmd[1]; + cmd[0] = (ADC_CONV | command); + return (i2c.write(i2cAddr, cmd, 1)==0); + } + + virtual int readADC_read(int command) { + char cmd[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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MS5611/MS5611SPI.h Tue May 19 13:13:03 2020 +0000 @@ -0,0 +1,77 @@ +/* +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. +*/ + +/* MS5611 Modifications +Aerodyne Labs +Matthew Nelson - 2014 +This library was originally the MS5607 library. It has been modified to +to be used the MS5611 Pressure sensor +*/ + +#ifndef MS5611_SPI_H +#define MS5611_SPI_H + +#include "MS5611Base.h" + +class MS5611SPI : public MS5611Base { +public: + MS5611SPI(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
--- a/main.cpp Fri Feb 21 07:30:50 2020 +0000 +++ b/main.cpp Tue May 19 13:13:03 2020 +0000 @@ -48,6 +48,7 @@ ms5611.getAltitude_toggle_Temp_write(); wait_ms(100); printf("Altitude = %.2f m \r\n", ms5611.getAltitude_toggle_Temp_read()); + printf("Altitude = %.2f m \r\n", ms5611.getAltitude()); float T = timer.read(); wait(1.0); }