Barometer.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Barometer library V1.0
  3. 2010 Copyright (c) Seeed Technology Inc. All right reserved.
  4. Original Author: LG
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include "Barometer.h"
  18. #include <Wire.h>
  19. #include <Arduino.h>
  20. void Barometer::init(void)
  21. {
  22. Wire.begin();
  23. Serial.print("Temperaturet: ");
  24. ac1 = bmp085ReadInt(0xAA);
  25. ac2 = bmp085ReadInt(0xAC);
  26. ac3 = bmp085ReadInt(0xAE);
  27. ac4 = bmp085ReadInt(0xB0);
  28. ac5 = bmp085ReadInt(0xB2);
  29. ac6 = bmp085ReadInt(0xB4);
  30. b1 = bmp085ReadInt(0xB6);
  31. b2 = bmp085ReadInt(0xB8);
  32. mb = bmp085ReadInt(0xBA);
  33. mc = bmp085ReadInt(0xBC);
  34. md = bmp085ReadInt(0xBE);
  35. Serial.print("Temperaturet2: ");
  36. }
  37. // Read 1 byte from the BMP085 at 'address'
  38. // Return: the read byte;
  39. char Barometer::bmp085Read(unsigned char address)
  40. {
  41. //Wire.begin();
  42. unsigned char data;
  43. Wire.beginTransmission(BMP085_ADDRESS);
  44. Wire.write(address);
  45. Wire.endTransmission();
  46. Wire.requestFrom(BMP085_ADDRESS, 1);
  47. while(!Wire.available());
  48. return Wire.read();
  49. }
  50. // Read 2 bytes from the BMP085
  51. // First byte will be from 'address'
  52. // Second byte will be from 'address'+1
  53. int Barometer::bmp085ReadInt(unsigned char address)
  54. {
  55. unsigned char msb, lsb;
  56. Wire.beginTransmission(BMP085_ADDRESS);
  57. Wire.write(address);
  58. Wire.endTransmission();
  59. Wire.requestFrom(BMP085_ADDRESS, 2);
  60. while(Wire.available()<2);
  61. msb = Wire.read();
  62. lsb = Wire.read();
  63. return (int) msb<<8 | lsb;
  64. }
  65. // Read the uncompensated temperature value
  66. unsigned int Barometer::bmp085ReadUT()
  67. {
  68. unsigned int ut;
  69. Wire.beginTransmission(BMP085_ADDRESS);
  70. Wire.write(0xF4);
  71. Wire.write(0x2E);
  72. Wire.endTransmission();
  73. delay(5);
  74. ut = bmp085ReadInt(0xF6);
  75. return ut;
  76. }
  77. // Read the uncompensated pressure value
  78. unsigned long Barometer::bmp085ReadUP()
  79. {
  80. unsigned char msb, lsb, xlsb;
  81. unsigned long up = 0;
  82. Wire.beginTransmission(BMP085_ADDRESS);
  83. Wire.write(0xF4);
  84. Wire.write(0x34 + (OSS<<6));
  85. Wire.endTransmission();
  86. delay(2 + (3<<OSS));
  87. // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  88. msb = bmp085Read(0xF6);
  89. lsb = bmp085Read(0xF7);
  90. xlsb = bmp085Read(0xF8);
  91. up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  92. return up;
  93. }
  94. void Barometer::writeRegister(int deviceAddress, byte address, byte val)
  95. {
  96. Wire.beginTransmission(deviceAddress); // start transmission to device
  97. Wire.write(address); // send register address
  98. Wire.write(val); // send value to write
  99. Wire.endTransmission(); // end transmission
  100. }
  101. int Barometer::readRegister(int deviceAddress, byte address)
  102. {
  103. int v;
  104. Wire.beginTransmission(deviceAddress);
  105. Wire.write(address); // register to read
  106. Wire.endTransmission();
  107. Wire.requestFrom(deviceAddress, 1); // read a byte
  108. while(!Wire.available()) {
  109. // waiting
  110. }
  111. v = Wire.read();
  112. return v;
  113. }
  114. float Barometer::calcAltitude(float pressure)
  115. {
  116. float A = pressure/101325;
  117. float B = 1/5.25588;
  118. float C = pow(A,B);
  119. C = 1 - C;
  120. C = C /0.0000225577;
  121. return C;
  122. }
  123. float Barometer::bmp085GetTemperature(unsigned int ut)
  124. {
  125. long x1, x2;
  126. x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  127. x2 = ((long)mc << 11)/(x1 + md);
  128. PressureCompensate = x1 + x2;
  129. float temp = ((PressureCompensate + 8)>>4);
  130. temp = temp /10;
  131. return temp;
  132. }
  133. long Barometer::bmp085GetPressure(unsigned long up)
  134. {
  135. long x1, x2, x3, b3, b6, p;
  136. unsigned long b4, b7;
  137. b6 = PressureCompensate - 4000;
  138. x1 = (b2 * (b6 * b6)>>12)>>11;
  139. x2 = (ac2 * b6)>>11;
  140. x3 = x1 + x2;
  141. b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  142. // Calculate B4
  143. x1 = (ac3 * b6)>>13;
  144. x2 = (b1 * ((b6 * b6)>>12))>>16;
  145. x3 = ((x1 + x2) + 2)>>2;
  146. b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  147. b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  148. if (b7 < 0x80000000)
  149. p = (b7<<1)/b4;
  150. else
  151. p = (b7/b4)<<1;
  152. x1 = (p>>8) * (p>>8);
  153. x1 = (x1 * 3038)>>16;
  154. x2 = (-7357 * p)>>16;
  155. p += (x1 + x2 + 3791)>>4;
  156. long temp = p;
  157. return temp;
  158. }