BMP085.cpp 5.2 KB

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