Barometer.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "Barometer.h"
  34. #include <Wire.h>
  35. #include <Arduino.h>
  36. void Barometer::init(void)
  37. {
  38. Wire.begin();
  39. Serial.print("Temperaturet: ");
  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. Serial.print("Temperaturet2: ");
  52. }
  53. // Read 1 byte from the BMP085 at 'address'
  54. // Return: the read byte;
  55. char Barometer::bmp085Read(unsigned char address)
  56. {
  57. //Wire.begin();
  58. unsigned char data;
  59. Wire.beginTransmission(BMP085_ADDRESS);
  60. Wire.write(address);
  61. Wire.endTransmission();
  62. Wire.requestFrom(BMP085_ADDRESS, 1);
  63. while(!Wire.available());
  64. return Wire.read();
  65. }
  66. // Read 2 bytes from the BMP085
  67. // First byte will be from 'address'
  68. // Second byte will be from 'address'+1
  69. short Barometer::bmp085ReadInt(unsigned char address)
  70. {
  71. unsigned char msb, lsb;
  72. Wire.beginTransmission(BMP085_ADDRESS);
  73. Wire.write(address);
  74. Wire.endTransmission();
  75. Wire.requestFrom(BMP085_ADDRESS, 2);
  76. while(Wire.available()<2);
  77. msb = Wire.read();
  78. lsb = Wire.read();
  79. return (short) msb<<8 | lsb;
  80. }
  81. // Read the uncompensated temperature value
  82. unsigned short Barometer::bmp085ReadUT()
  83. {
  84. unsigned short ut;
  85. Wire.beginTransmission(BMP085_ADDRESS);
  86. Wire.write(0xF4);
  87. Wire.write(0x2E);
  88. Wire.endTransmission();
  89. delay(5);
  90. ut = bmp085ReadInt(0xF6);
  91. return ut;
  92. }
  93. // Read the uncompensated pressure value
  94. unsigned long Barometer::bmp085ReadUP()
  95. {
  96. unsigned char msb, lsb, xlsb;
  97. unsigned long up = 0;
  98. Wire.beginTransmission(BMP085_ADDRESS);
  99. Wire.write(0xF4);
  100. Wire.write(0x34 + (OSS<<6));
  101. Wire.endTransmission();
  102. delay(2 + (3<<OSS));
  103. // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  104. msb = bmp085Read(0xF6);
  105. lsb = bmp085Read(0xF7);
  106. xlsb = bmp085Read(0xF8);
  107. up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  108. return up;
  109. }
  110. void Barometer::writeRegister(short deviceAddress, byte address, byte val)
  111. {
  112. Wire.beginTransmission(deviceAddress); // start transmission to device
  113. Wire.write(address); // send register address
  114. Wire.write(val); // send value to write
  115. Wire.endTransmission(); // end transmission
  116. }
  117. short Barometer::readRegister(short deviceAddress, byte address)
  118. {
  119. short v;
  120. Wire.beginTransmission(deviceAddress);
  121. Wire.write(address); // register to read
  122. Wire.endTransmission();
  123. Wire.requestFrom(deviceAddress, 1); // read a byte
  124. while(!Wire.available()) {
  125. // waiting
  126. }
  127. v = Wire.read();
  128. return v;
  129. }
  130. float Barometer::calcAltitude(float pressure)
  131. {
  132. float A = pressure/101325;
  133. float B = 1/5.25588;
  134. float C = pow(A,B);
  135. C = 1 - C;
  136. C = C /0.0000225577;
  137. return C;
  138. }
  139. float Barometer::bmp085GetTemperature(unsigned short ut)
  140. {
  141. long x1, x2;
  142. x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  143. x2 = ((long)mc << 11)/(x1 + md);
  144. PressureCompensate = x1 + x2;
  145. float temp = ((PressureCompensate + 8)>>4);
  146. temp = temp /10;
  147. return temp;
  148. }
  149. long Barometer::bmp085GetPressure(unsigned long up)
  150. {
  151. long x1, x2, x3, b3, b6, p;
  152. unsigned long b4, b7;
  153. b6 = PressureCompensate - 4000;
  154. x1 = (b2 * (b6 * b6)>>12)>>11;
  155. x2 = (ac2 * b6)>>11;
  156. x3 = x1 + x2;
  157. b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  158. // Calculate B4
  159. x1 = (ac3 * b6)>>13;
  160. x2 = (b1 * ((b6 * b6)>>12))>>16;
  161. x3 = ((x1 + x2) + 2)>>2;
  162. b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  163. b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  164. if (b7 < 0x80000000)
  165. p = (b7<<1)/b4;
  166. else
  167. p = (b7/b4)<<1;
  168. x1 = (p>>8) * (p>>8);
  169. x1 = (x1 * 3038)>>16;
  170. x2 = (-7357 * p)>>16;
  171. p += (x1 + x2 + 3791)>>4;
  172. long temp = p;
  173. return temp;
  174. }