Barometer.cpp 5.1 KB

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