Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 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. return p;
  114. }
  115.  
  116. // Read 1 byte from the BMP085 at 'address'
  117. char bmp085Read(unsigned char address)
  118. {
  119. unsigned char data;
  120.  
  121. Wire.beginTransmission(BMP085_ADDRESS);
  122. Wire.write(address);
  123. Wire.endTransmission();
  124.  
  125. Wire.requestFrom(BMP085_ADDRESS, 1);
  126. while(!Wire.available())
  127. ;
  128.  
  129. return Wire.read();
  130. }
  131.  
  132. // Read 2 bytes from the BMP085
  133. // First byte will be from 'address'
  134. // Second byte will be from 'address'+1
  135. int bmp085ReadInt(unsigned char address)
  136. {
  137. unsigned char msb, lsb;
  138.  
  139. Wire.beginTransmission(BMP085_ADDRESS);
  140. Wire.write(address);
  141. Wire.endTransmission();
  142.  
  143. Wire.requestFrom(BMP085_ADDRESS, 2);
  144. while(Wire.available()<2)
  145. ;
  146. msb = Wire.read();
  147. lsb = Wire.read();
  148.  
  149. return (int) msb<<8 | lsb;
  150. }
  151.  
  152. // Read the uncompensated temperature value
  153. unsigned int bmp085ReadUT()
  154. {
  155. unsigned int ut;
  156.  
  157. // Write 0x2E into Register 0xF4
  158. // This requests a temperature reading
  159. Wire.beginTransmission(BMP085_ADDRESS);
  160. Wire.write(0xF4);
  161. Wire.write(0x2E);
  162. Wire.endTransmission();
  163.  
  164. // Wait at least 4.5ms
  165. delay(5);
  166.  
  167. // Read two bytes from registers 0xF6 and 0xF7
  168. ut = bmp085ReadInt(0xF6);
  169. return ut;
  170. }
  171.  
  172. // Read the uncompensated pressure value
  173. unsigned long bmp085ReadUP()
  174. {
  175. unsigned char msb, lsb, xlsb;
  176. unsigned long up = 0;
  177.  
  178. // Write 0x34+(OSS<<6) into register 0xF4
  179. // Request a pressure reading w/ oversampling setting
  180. Wire.beginTransmission(BMP085_ADDRESS);
  181. Wire.write(0xF4);
  182. Wire.write(0x34 + (OSS<<6));
  183. Wire.endTransmission();
  184.  
  185. // Wait for conversion, delay time dependent on OSS
  186. delay(2 + (3<<OSS));
  187.  
  188. // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  189. Wire.beginTransmission(BMP085_ADDRESS);
  190. Wire.write(0xF6);
  191. Wire.endTransmission();
  192. Wire.requestFrom(BMP085_ADDRESS, 3);
  193.  
  194. // Wait for data to become available
  195. while(Wire.available() < 3)
  196. ;
  197. msb = Wire.read();
  198. lsb = Wire.read();
  199. xlsb = Wire.read();
  200.  
  201. up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  202.  
  203. return up;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement