Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. /* Barometer Code for BMP085
  2. SCL: A5
  3. SDA: A4
  4. */
  5. #include <Wire.h>
  6.  
  7. int BMP085_ADDRESS = 0x77;
  8. /* This is the oversampling rate which goes from 0 to basic to 3 of a high resolution */
  9. const unsigned char OSS = 0;
  10.  
  11. /* Calibration data */
  12. int ac1;
  13. int ac2;
  14. int ac3;
  15. unsigned int ac4;
  16. unsigned int ac5;
  17. unsigned int ac6;
  18. int b1;
  19. int b2;
  20. int mb;
  21. int mc;
  22. int md;
  23. short Temperature = 0;
  24. long Pressure = 0;
  25. long b5;
  26.  
  27. /* Standard Atmosphere */
  28. const float Po = 101325;
  29. float altitude;
  30.  
  31. void setup() {
  32. Serial.begin(9600);
  33. Wire.begin();
  34. bmp085Calibration();
  35. }
  36.  
  37. /* Calcs from data sheet */
  38. void loop()
  39. {
  40. float Temperature = bmp085GetTemperature(bmp085ReadUT());
  41. float Pressure = bmp085GetPressure(bmp085ReadUP());
  42. float atm = Pressure / 101325;
  43. float altitude = 44330 * (1- pow(((float) Pressure/Po), 0.190295));
  44.  
  45.  
  46. Serial.print("Temperature:");
  47. Serial.print(Temperature, 2);
  48. Serial.println("Degrees C");
  49.  
  50. Serial.print("Pressure:");
  51. Serial.print(Pressure, 0);
  52. Serial.println("Pascals");
  53.  
  54. Serial.print("Standard Atmosphere:");
  55. Serial.println(atm, 4);
  56.  
  57. Serial.print("Altitude:");
  58. Serial.print(altitude, 2);
  59. Serial.println("Meters ASL");
  60.  
  61. Serial.println();
  62. delay(1000);
  63. }
  64.  
  65. void bmp085Calibration()
  66. {
  67. ac1 = bmp085ReadInt(0xAA);
  68. ac2 = bmp085ReadInt(0xAC);
  69. ac3 = bmp085ReadInt(0xAE);
  70. ac4 = bmp085ReadInt(0xB0);
  71. ac5 = bmp085ReadInt(0xB2);
  72. ac6 = bmp085ReadInt(0xB4);
  73. b1 = bmp085ReadInt(0xB6);
  74. b2 = bmp085ReadInt(0xB8);
  75. mb = bmp085ReadInt(0xBA);
  76. mc = bmp085ReadInt(0xBC);
  77. md = bmp085ReadInt(0xBE);
  78. }
  79.  
  80. short bmp085GetTemperature(unsigned int ut)
  81. {
  82. long x1, x2;
  83.  
  84. x1 = ((long)ut - (long)ac6) * (long)ac5 / pow(2, 15);
  85. x2 = ((long)mc * pow(2, 11)) / ((long)x1 + (long)md);
  86. b5 = x1 + x2;
  87. return ((b5 + 8) / pow(2, 4));
  88. }
  89. long bmp085GetPressure(unsigned long up)
  90. {
  91. long b6, x1, x2, x3, b3, p;
  92. unsigned long b4, b7;
  93.  
  94. b6 = b5 - 4000;
  95. x1 = (b2 * (b6 * b6 / pow(2, 12))) / pow(2, 11);
  96. x2 = (ac2 * b6) / pow(2, 11);
  97. x3 = x1 + x2;
  98. b3 = ((((long)ac1) * 4 + x3)<<OSS + 2) / 4;
  99. x1 = (ac3 * b6) / pow(2, 13);
  100. x2 = (b1 * (b6 * b6 / pow(2, 12))) / pow(2, 16);
  101. x3 = ((x1 + x2) + 2) / pow(2, 3);
  102. b4 = ac4 * ((unsigned long)(x3 + 32768)) / pow(2, 15);
  103. b7 = ((unsigned long)(up - b3) * (50000 >> OSS));
  104. if (b7 < 0x80000000)
  105. p = (b7 * 2) / b4;
  106. else
  107. p = (b7 / b4) * 2;
  108. x1 = (p/ pow(2, 8)) * (p/ pow(2, 8));
  109. x1 = (x1 * 3038) / pow(2, 16);
  110. x2 = (-7357 * p) / pow(2, 16);
  111. p = p + (x1 + x2 + 3791) / pow(2, 4);
  112.  
  113. long temp = p;
  114. return temp;
  115. }
  116.  
  117. // Read 1 byte from the BMP085 at 'address'
  118. char bmp085Read(unsigned char address)
  119. {
  120. unsigned char data;
  121.  
  122. Wire.beginTransmission(BMP085_ADDRESS);
  123. Wire.write(address);
  124. Wire.endTransmission();
  125.  
  126. Wire.requestFrom(BMP085_ADDRESS, 1);
  127. while(!Wire.available())
  128. ;
  129.  
  130. return Wire.read();
  131. }
  132.  
  133. // Read 2 bytes from the BMP085
  134. // First byte will be from 'address'
  135. // Second byte will be from 'address'+1
  136. int bmp085ReadInt(unsigned char address)
  137. {
  138. unsigned char msb, lsb;
  139.  
  140. Wire.beginTransmission(BMP085_ADDRESS);
  141. Wire.write(address);
  142. Wire.endTransmission();
  143.  
  144. Wire.requestFrom(BMP085_ADDRESS, 2);
  145. while(Wire.available()<2)
  146. ;
  147. msb = Wire.read();
  148. lsb = Wire.read();
  149.  
  150. return (int) msb<<8 | lsb;
  151. }
  152.  
  153. // Read the uncompensated temperature value
  154. unsigned int bmp085ReadUT()
  155. {
  156. unsigned int ut;
  157.  
  158. // Write 0x2E into Register 0xF4
  159. // This requests a temperature reading
  160. Wire.beginTransmission(BMP085_ADDRESS);
  161. Wire.write(0xF4);
  162. Wire.write(0x2E);
  163. Wire.endTransmission();
  164.  
  165. // Wait at least 4.5ms
  166. delay(5);
  167.  
  168. // Read two bytes from registers 0xF6 and 0xF7
  169. ut = bmp085ReadInt(0xF6);
  170. return ut;
  171. }
  172.  
  173. // Read the uncompensated pressure value
  174. unsigned long bmp085ReadUP()
  175. {
  176. unsigned char msb, lsb, xlsb;
  177. unsigned long up = 0;
  178.  
  179. // Write 0x34+(OSS<<6) into register 0xF4
  180. // Request a pressure reading w/ oversampling setting
  181. Wire.beginTransmission(BMP085_ADDRESS);
  182. Wire.write(0xF4);
  183. Wire.write(0x34 + (OSS<<6));
  184. Wire.endTransmission();
  185.  
  186. // Wait for conversion, delay time dependent on OSS
  187. delay(2 + (3<<OSS));
  188.  
  189. // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  190. Wire.beginTransmission(BMP085_ADDRESS);
  191. Wire.write(0xF6);
  192. Wire.endTransmission();
  193. Wire.requestFrom(BMP085_ADDRESS, 3);
  194.  
  195. // Wait for data to become available
  196. while(Wire.available() < 3)
  197. ;
  198. msb = Wire.read();
  199. lsb = Wire.read();
  200. xlsb = Wire.read();
  201.  
  202. up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  203.  
  204. return up;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement