BMP085.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. Barometer.cpp
  3. A library for barometer
  4. Copyright (c) 2012 seeed technology inc.
  5. Website : www.seeed.cc
  6. Author : LG
  7. Create Time:
  8. Change Log :
  9. loovee 9-24-2014
  10. Change all int to short, all unsigned int to unsigned short to fit some 32bit system
  11. The MIT License (MIT)
  12. Permission is hereby granted, free of charge, to any person obtaining a copy
  13. of this software and associated documentation files (the "Software"), to deal
  14. in the Software without restriction, including without limitation the rights
  15. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. copies of the Software, and to permit persons to whom the Software is
  17. furnished to do so, subject to the following conditions:
  18. The above copyright notice and this permission notice shall be included in
  19. all copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. THE SOFTWARE.
  27. */
  28. #include "BMP085.h"
  29. #include <Wire.h>
  30. #include <Arduino.h>
  31. #include <math.h>
  32. void BMP085::init(void) {
  33. Wire.begin();
  34. ac1 = bmp085ReadInt(0xAA);
  35. ac2 = bmp085ReadInt(0xAC);
  36. ac3 = bmp085ReadInt(0xAE);
  37. ac4 = bmp085ReadInt(0xB0);
  38. ac5 = bmp085ReadInt(0xB2);
  39. ac6 = bmp085ReadInt(0xB4);
  40. b1 = bmp085ReadInt(0xB6);
  41. b2 = bmp085ReadInt(0xB8);
  42. mb = bmp085ReadInt(0xBA);
  43. mc = bmp085ReadInt(0xBC);
  44. md = bmp085ReadInt(0xBE);
  45. }
  46. // Read 1 byte from the BMP085 at 'address'
  47. // Return: the read byte;
  48. char BMP085::bmp085Read(unsigned char address) {
  49. Wire.beginTransmission(BMP085_ADDRESS);
  50. Wire.write(address);
  51. Wire.endTransmission();
  52. Wire.requestFrom(BMP085_ADDRESS, 1);
  53. while (!Wire.available());
  54. return Wire.read();
  55. }
  56. // Read 2 bytes from the BMP085
  57. // First byte will be from 'address'
  58. // Second byte will be from 'address'+1
  59. short BMP085::bmp085ReadInt(unsigned char address) {
  60. unsigned char msb, lsb;
  61. Wire.beginTransmission(BMP085_ADDRESS);
  62. Wire.write(address);
  63. Wire.endTransmission();
  64. Wire.requestFrom(BMP085_ADDRESS, 2);
  65. while (Wire.available() < 2);
  66. msb = Wire.read();
  67. lsb = Wire.read();
  68. return (short) msb << 8 | lsb;
  69. }
  70. // Read the uncompensated temperature value
  71. unsigned short BMP085::bmp085ReadUT() {
  72. unsigned short ut;
  73. Wire.beginTransmission(BMP085_ADDRESS);
  74. Wire.write(0xF4);
  75. Wire.write(0x2E);
  76. Wire.endTransmission();
  77. delay(5);
  78. ut = bmp085ReadInt(0xF6);
  79. return ut;
  80. }
  81. // Read the uncompensated pressure value
  82. unsigned long BMP085::bmp085ReadUP() {
  83. unsigned char msb, lsb, xlsb;
  84. unsigned long up = 0;
  85. Wire.beginTransmission(BMP085_ADDRESS);
  86. Wire.write(0xF4);
  87. Wire.write(0x34 + (OSS << 6));
  88. Wire.endTransmission();
  89. delay(2 + (3 << OSS));
  90. // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  91. msb = bmp085Read(0xF6);
  92. lsb = bmp085Read(0xF7);
  93. xlsb = bmp085Read(0xF8);
  94. up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8 - OSS);
  95. return up;
  96. }
  97. void BMP085::writeRegister(short deviceAddress, byte address, byte val) {
  98. Wire.beginTransmission(deviceAddress); // start transmission to device
  99. Wire.write(address); // send register address
  100. Wire.write(val); // send value to write
  101. Wire.endTransmission(); // end transmission
  102. }
  103. short BMP085::readRegister(short deviceAddress, byte address) {
  104. short v;
  105. Wire.beginTransmission(deviceAddress);
  106. Wire.write(address); // register to read
  107. Wire.endTransmission();
  108. Wire.requestFrom(deviceAddress, 1); // read a byte
  109. while (!Wire.available()) {
  110. // waiting
  111. }
  112. v = Wire.read();
  113. return v;
  114. }
  115. float BMP085::calcAltitude(float seaLevelPressure) {
  116. float pressure = bmp085GetPressure(bmp085ReadUP());//Get the temperature
  117. float altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903));
  118. return altitude;
  119. }
  120. float BMP085::bmp085GetTemperature(unsigned short ut) {
  121. long x1, x2;
  122. x1 = (((long)ut - (long)ac6) * (long)ac5) >> 15;
  123. x2 = ((long)mc << 11) / (x1 + md);
  124. PressureCompensate = x1 + x2;
  125. float temp = ((PressureCompensate + 8) >> 4);
  126. temp = temp / 10;
  127. return temp;
  128. }
  129. long BMP085::bmp085GetPressure(unsigned long up) {
  130. long x1, x2, x3, b3, b6, p;
  131. unsigned long b4, b7;
  132. b6 = PressureCompensate - 4000;
  133. x1 = (b2 * (b6 * b6) >> 12) >> 11;
  134. x2 = (ac2 * b6) >> 11;
  135. x3 = x1 + x2;
  136. b3 = (((((long)ac1) * 4 + x3) << OSS) + 2) >> 2;
  137. // Calculate B4
  138. x1 = (ac3 * b6) >> 13;
  139. x2 = (b1 * ((b6 * b6) >> 12)) >> 16;
  140. x3 = ((x1 + x2) + 2) >> 2;
  141. b4 = (ac4 * (unsigned long)(x3 + 32768)) >> 15;
  142. b7 = ((unsigned long)(up - b3) * (50000 >> OSS));
  143. if (b7 < 0x80000000) {
  144. p = (b7 << 1) / b4;
  145. } else {
  146. p = (b7 / b4) << 1;
  147. }
  148. x1 = (p >> 8) * (p >> 8);
  149. x1 = (x1 * 3038) >> 16;
  150. x2 = (-7357 * p) >> 16;
  151. p += (x1 + x2 + 3791) >> 4;
  152. long temp = p;
  153. return temp;
  154. }