ms

Committer:
wisnup
Date:
Thu Feb 20 04:13:21 2014 +0000
Revision:
0:375aa941f760
ms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wisnup 0:375aa941f760 1 /*
wisnup 0:375aa941f760 2 * mbed library to use a Bosch Sensortec MS5611 sensor
wisnup 0:375aa941f760 3 * Copyright (c) 2010 Hiroshi Suga
wisnup 0:375aa941f760 4 * Released under the MIT License: http://mbed.org/license/mit
wisnup 0:375aa941f760 5 */
wisnup 0:375aa941f760 6
wisnup 0:375aa941f760 7 /** @file MS5611.cpp
wisnup 0:375aa941f760 8 * @brief mbed library to use a Bosch Sensortec MS5611 sensor
wisnup 0:375aa941f760 9 * barometric pressure sensor MS5611 (Bosch Sensortec)
wisnup 0:375aa941f760 10 * interface: I2C digital
wisnup 0:375aa941f760 11 */
wisnup 0:375aa941f760 12
wisnup 0:375aa941f760 13 #include "mbed.h"
wisnup 0:375aa941f760 14 #include "MS5611.h"
wisnup 0:375aa941f760 15
wisnup 0:375aa941f760 16 #define WEATHER_MS5611 0xee
wisnup 0:375aa941f760 17 #define xpow(x, y) ((long)1 << y)
wisnup 0:375aa941f760 18
wisnup 0:375aa941f760 19 /**
wisnup 0:375aa941f760 20 * @brief Initializes interface (private I2C)
wisnup 0:375aa941f760 21 * @param p_sda port of I2C SDA
wisnup 0:375aa941f760 22 * @param p_scl port of I2C SCL
wisnup 0:375aa941f760 23 * @param p_oss parameter of OSS
wisnup 0:375aa941f760 24 */
wisnup 0:375aa941f760 25 MS5611::MS5611 (PinName p_sda, PinName p_scl, MS5611_oss p_oss) : i2c(p_sda, p_scl) {
wisnup 0:375aa941f760 26 init(p_oss);
wisnup 0:375aa941f760 27 }
wisnup 0:375aa941f760 28
wisnup 0:375aa941f760 29 /**
wisnup 0:375aa941f760 30 * @brief Initializes interface (public I2C)
wisnup 0:375aa941f760 31 * @param p_i2c instance of I2C class
wisnup 0:375aa941f760 32 * @param p_oss parameter of OSS
wisnup 0:375aa941f760 33 */
wisnup 0:375aa941f760 34 MS5611::MS5611 (I2C& p_i2c, MS5611_oss p_oss) : i2c(p_i2c) {
wisnup 0:375aa941f760 35 init(p_oss);
wisnup 0:375aa941f760 36 }
wisnup 0:375aa941f760 37
wisnup 0:375aa941f760 38 /**
wisnup 0:375aa941f760 39 * @brief Get temperature
wisnup 0:375aa941f760 40 * @return temperature (`C)
wisnup 0:375aa941f760 41 */
wisnup 0:375aa941f760 42 float MS5611::get_temperature() {
wisnup 0:375aa941f760 43 return temperature;
wisnup 0:375aa941f760 44 }
wisnup 0:375aa941f760 45
wisnup 0:375aa941f760 46 /**
wisnup 0:375aa941f760 47 * @brief Get pressure
wisnup 0:375aa941f760 48 * @return pressure (hPa)
wisnup 0:375aa941f760 49 */
wisnup 0:375aa941f760 50 float MS5611::get_pressure() {
wisnup 0:375aa941f760 51 return pressure;
wisnup 0:375aa941f760 52 }
wisnup 0:375aa941f760 53
wisnup 0:375aa941f760 54 /**
wisnup 0:375aa941f760 55 * @brief Update results
wisnup 0:375aa941f760 56 */
wisnup 0:375aa941f760 57 void MS5611::update () {
wisnup 0:375aa941f760 58 long t, p, ut, up, x1, x2, x3, b3, b5, b6;
wisnup 0:375aa941f760 59 unsigned long b4, b7;
wisnup 0:375aa941f760 60
wisnup 0:375aa941f760 61 twi_writechar(WEATHER_MS5611, 0xf4, 0x2e);
wisnup 0:375aa941f760 62 wait(0.0045); //wait(0.01);
wisnup 0:375aa941f760 63 ut = twi_readshort(WEATHER_MS5611, 0xf6);
wisnup 0:375aa941f760 64
wisnup 0:375aa941f760 65 twi_writechar(WEATHER_MS5611, 0xf4, 0x34 | (oss << 6));
wisnup 0:375aa941f760 66 wait(0.0045); //wait(0.05);
wisnup 0:375aa941f760 67 up = twi_readlong(WEATHER_MS5611, 0xf6) >> (8 - oss);
wisnup 0:375aa941f760 68
wisnup 0:375aa941f760 69 x1 = (ut - ac6) * ac5 / xpow(2, 15);
wisnup 0:375aa941f760 70 x2 = (long)mc * xpow(2, 11) / (x1 + md);
wisnup 0:375aa941f760 71 b5 = x1 + x2;
wisnup 0:375aa941f760 72 t = (b5 + 8) / xpow(2, 4);
wisnup 0:375aa941f760 73 temperature = (float)t / 10.0;
wisnup 0:375aa941f760 74
wisnup 0:375aa941f760 75 b6 = b5 - 4000;
wisnup 0:375aa941f760 76 x1 = (b2 * (b6 * b6 / xpow(2, 12))) / xpow(2, 11);
wisnup 0:375aa941f760 77 x2 = ac2 * b6 / xpow(2, 11);
wisnup 0:375aa941f760 78 x3 = x1 + x2;
wisnup 0:375aa941f760 79 b3 = ((((unsigned long)ac1 * 4 + x3) << oss) + 2) / 4;
wisnup 0:375aa941f760 80 x1 = ac3 * b6 / xpow(2, 13);
wisnup 0:375aa941f760 81 x2 = (b1 * (b6 * b6 / xpow(2, 12))) / xpow(2, 16);
wisnup 0:375aa941f760 82 x3 = ((x1 + x2) + 2) / xpow(2, 2);
wisnup 0:375aa941f760 83 b4 = ac4 * (unsigned long)(x3 + 32768) / xpow(2, 15);
wisnup 0:375aa941f760 84 b7 = ((unsigned long)up - b3) * (50000 >> oss);
wisnup 0:375aa941f760 85 if (b7 < (unsigned long)0x80000000) {
wisnup 0:375aa941f760 86 p = (b7 * 2) / b4;
wisnup 0:375aa941f760 87 } else {
wisnup 0:375aa941f760 88 p = (b7 / b4) * 2;
wisnup 0:375aa941f760 89 }
wisnup 0:375aa941f760 90 x1 = (p / xpow(2, 8)) * (p / xpow(2, 8));
wisnup 0:375aa941f760 91 x1 = (x1 * 3038) / xpow(2, 16);
wisnup 0:375aa941f760 92 x2 = (-7357 * p) / xpow(2, 16);
wisnup 0:375aa941f760 93 p = p + (x1 + x2 + 3791) / xpow(2, 4);
wisnup 0:375aa941f760 94 pressure = (float)p / 100.0;
wisnup 0:375aa941f760 95 }
wisnup 0:375aa941f760 96
wisnup 0:375aa941f760 97 void MS5611::init (MS5611_oss p_oss) {
wisnup 0:375aa941f760 98 ac1 = twi_readshort(WEATHER_MS5611, 0xaa);
wisnup 0:375aa941f760 99 ac2 = twi_readshort(WEATHER_MS5611, 0xac);
wisnup 0:375aa941f760 100 ac3 = twi_readshort(WEATHER_MS5611, 0xae);
wisnup 0:375aa941f760 101 ac4 = twi_readshort(WEATHER_MS5611, 0xb0);
wisnup 0:375aa941f760 102 ac5 = twi_readshort(WEATHER_MS5611, 0xb2);
wisnup 0:375aa941f760 103 ac6 = twi_readshort(WEATHER_MS5611, 0xb4);
wisnup 0:375aa941f760 104 b1 = twi_readshort(WEATHER_MS5611, 0xb6);
wisnup 0:375aa941f760 105 b2 = twi_readshort(WEATHER_MS5611, 0xb8);
wisnup 0:375aa941f760 106 mb = twi_readshort(WEATHER_MS5611, 0xba);
wisnup 0:375aa941f760 107 mc = twi_readshort(WEATHER_MS5611, 0xbc);
wisnup 0:375aa941f760 108 md = twi_readshort(WEATHER_MS5611, 0xbe);
wisnup 0:375aa941f760 109 oss = p_oss;
wisnup 0:375aa941f760 110 }
wisnup 0:375aa941f760 111
wisnup 0:375aa941f760 112 unsigned short MS5611::twi_readshort (int id, int addr) {
wisnup 0:375aa941f760 113 unsigned short i;
wisnup 0:375aa941f760 114
wisnup 0:375aa941f760 115 i2c.start();
wisnup 0:375aa941f760 116 i2c.write(id);
wisnup 0:375aa941f760 117 i2c.write(addr);
wisnup 0:375aa941f760 118
wisnup 0:375aa941f760 119 i2c.start();
wisnup 0:375aa941f760 120 i2c.write(id | 1);
wisnup 0:375aa941f760 121 i = i2c.read(1) << 8;
wisnup 0:375aa941f760 122 i |= i2c.read(0);
wisnup 0:375aa941f760 123 i2c.stop();
wisnup 0:375aa941f760 124
wisnup 0:375aa941f760 125 return i;
wisnup 0:375aa941f760 126 }
wisnup 0:375aa941f760 127
wisnup 0:375aa941f760 128 unsigned long MS5611::twi_readlong (int id, int addr) {
wisnup 0:375aa941f760 129 unsigned long i;
wisnup 0:375aa941f760 130
wisnup 0:375aa941f760 131 i2c.start();
wisnup 0:375aa941f760 132 i2c.write(id);
wisnup 0:375aa941f760 133 i2c.write(addr);
wisnup 0:375aa941f760 134
wisnup 0:375aa941f760 135 i2c.start();
wisnup 0:375aa941f760 136 i2c.write(id | 1);
wisnup 0:375aa941f760 137 i = i2c.read(1) << 16;
wisnup 0:375aa941f760 138 i |= i2c.read(1) << 8;
wisnup 0:375aa941f760 139 i |= i2c.read(0);
wisnup 0:375aa941f760 140 i2c.stop();
wisnup 0:375aa941f760 141
wisnup 0:375aa941f760 142 return i;
wisnup 0:375aa941f760 143 }
wisnup 0:375aa941f760 144
wisnup 0:375aa941f760 145 void MS5611::twi_writechar (int id, int addr, int dat) {
wisnup 0:375aa941f760 146
wisnup 0:375aa941f760 147 i2c.start();
wisnup 0:375aa941f760 148 i2c.write(id);
wisnup 0:375aa941f760 149 i2c.write(addr);
wisnup 0:375aa941f760 150 i2c.write(dat);
wisnup 0:375aa941f760 151 i2c.stop();
wisnup 0:375aa941f760 152 }