Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include "Wire.h"
  3. LiquidCrystal lcd(12,11, 5, 4, 3, 2);
  4.  
  5. float pressao;
  6. float p0,P;
  7. int altitude;
  8. byte i=0;
  9.  
  10.  
  11. #define BMP085_ADDRESS 0x77
  12.  
  13. const unsigned char oversampling_setting = 3;
  14. const unsigned char pressure_waittime[4] = {
  15. 5, 8, 14, 26 };
  16.  
  17. int ac1, ac2, ac3;
  18. unsigned int ac4, ac5, ac6;
  19. int b1, b2, mb, mc, md;
  20. int temperature=0;
  21. long pressure=0;
  22.  
  23. double pressaoB = 101325 ;
  24. long b5;
  25.  
  26. void setup()
  27. {
  28. Serial.begin(9600);
  29. lcd.begin(16,2);
  30. lcd.setCursor(10, 2);
  31. lcd.print("P");
  32. lcd.setCursor(11, 0);
  33. lcd.print("T");
  34. lcd.setCursor(0,0);
  35. lcd.print("A");
  36. lcd.setCursor(1,0);
  37. lcd.print("-");
  38. lcd.setCursor(0,1);
  39. lcd.print("H");
  40. Serial.println("Setting up BMP085");
  41.  
  42. Wire.begin();
  43.  
  44. bmp085_get_cal_data( );
  45. void bmp085_read_temperature_and_pressure(int& temperature,long& pressure);
  46. Serial.print("Temperature:");
  47. lcd.setCursor(11,2);
  48. lcd.print(pressure/100);
  49.  
  50. float temp =(float)temperature / 10.0;
  51. Serial.print(temp,2);
  52. lcd.setCursor(12,0);
  53. lcd.print(temp,2);
  54. float pressao = (float)pressao/100.0;
  55. Serial.print("Pressao:");
  56. Serial.print(pressure);
  57. Serial.print(" ");
  58. lcd.setCursor(12,2);
  59. lcd.print(pressure);
  60. lcd.setCursor(11,2);
  61. lcd.print("-");
  62.  
  63. }
  64. void loop()
  65. {
  66. bmp085_read_temperature_and_pressure(&temperature,& pressure);
  67. Serial.print("Temperature:");
  68. float temp =(float)temperature / 10.0;
  69.  
  70.  
  71. Serial.print(temp,2);
  72.  
  73. lcd.setCursor(12,0);
  74. lcd.print(temp,2);
  75. delay(3000);
  76. Serial.print("Pressao:");
  77. Serial.print(pressure/100);
  78. Serial.print(" ");
  79.  
  80. lcd.setCursor(12,2);
  81. lcd.print(pressure/100);
  82.  
  83.  
  84. pressao= pressure;//
  85. p0 = pressaoB;
  86.  
  87. altitude = (float)44330 * (1 - pow(((float) pressao/p0), 0.1902950));
  88.  
  89. Serial.print("ALTITUDE barometrica;");
  90. Serial.print(altitude );
  91.  
  92. Serial.println("m ");
  93.  
  94. lcd.setCursor(2,0);
  95. lcd.print(altitude);
  96. lcd.setCursor(4,1);
  97. // lcd.println("m");
  98.  
  99. }
  100. void bmp085_read_temperature_and_pressure(int* temperature, long* pressure) {
  101. int ut= bmp085_read_ut();
  102. long up = bmp085_read_up();
  103. long x1, x2, x3, b3, b5, b6, p;
  104. unsigned long b4, b7;
  105.  
  106. //calculate the temperature
  107. x1 = ((long)ut - ac6) * ac5 >> 15;
  108. x2 = ((long) mc << 11) / (x1 + md);
  109. b5 = x1 + x2;
  110. *temperature = (b5 + 8) >> 4;
  111.  
  112. //calculate the pressure
  113. b6 = b5 - 4000;
  114. x1 = (b2 * (b6 * b6 >> 12)) >> 11;
  115. x2 = ac2 * b6 >> 11;
  116. x3 = x1 + x2;
  117.  
  118. //b3 = (((int32_t) ac1 * 4 + x3)<> 2;
  119.  
  120. if (oversampling_setting == 3) b3 = ((int32_t) ac1 * 4 + x3 + 2) << 1;
  121. if (oversampling_setting == 2) b3 = ((int32_t) ac1 * 4 + x3 + 2);
  122. if (oversampling_setting == 1) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 1;
  123. if (oversampling_setting == 0) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 2;
  124.  
  125. x1 = ac3 * b6 >> 13;
  126. x2 = (b1 * (b6 * b6 >> 12)) >> 16;
  127. x3 = ((x1 + x2) + 2) >> 2;
  128. b4 = (ac4 * (uint32_t) (x3 + 32768)) >> 15;
  129. b7 = ((uint32_t) up - b3) * (50000 >> oversampling_setting);
  130. p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
  131.  
  132. x1 = (p >> 8) * (p >> 8);
  133. x1 = (x1 * 3038) >> 16;
  134. x2 = (-7357 * p) >> 16;
  135. *pressure = p + ((x1 + x2 + 3791) >> 4);
  136.  
  137. }
  138.  
  139. unsigned int bmp085_read_ut() {
  140. write_register(0xf4,0x2e);
  141. delay(5); //longer than 4.5 ms
  142. return read_int_register(0xf6);
  143. }
  144.  
  145. void bmp085_get_cal_data() {
  146. Serial.println("Reading Calibration Data");
  147. ac1 = read_int_register(0xAA);
  148. Serial.print("AC1: ");
  149. Serial.println(ac1,DEC);
  150. ac2 = read_int_register(0xAC);
  151. Serial.print("AC2: ");
  152. Serial.println(ac2,DEC);
  153. ac3 = read_int_register(0xAE);
  154. Serial.print("AC3: ");
  155. Serial.println(ac3,DEC);
  156. ac4 = read_int_register(0xB0);
  157. Serial.print("AC4: ");
  158. Serial.println(ac4,DEC);
  159. ac5 = read_int_register(0xB2);
  160. Serial.print("AC5: ");
  161. Serial.println(ac5,DEC);
  162. ac6 = read_int_register(0xB4);
  163. Serial.print("AC6: ");
  164. Serial.println(ac6,DEC);
  165. b1 = read_int_register(0xB6);
  166. Serial.print("B1: ");
  167. Serial.println(b1,DEC);
  168. b2 = read_int_register(0xB8);
  169. Serial.print("B2: ");
  170. Serial.println(b1,DEC);
  171. mb = read_int_register(0xBA);
  172. Serial.print("MB: ");
  173. Serial.println(mb,DEC);
  174. mc = read_int_register(0xBC);
  175. Serial.print("MC: ");
  176. Serial.println(mc,DEC);
  177. md = read_int_register(0xBE);
  178. Serial.print("MD: ");
  179. Serial.println(md,DEC);
  180. }
  181.  
  182. long bmp085_read_up() {
  183. write_register(0xf4,0x34+(oversampling_setting<<6));
  184. delay(pressure_waittime[oversampling_setting]);
  185.  
  186. unsigned char msb, lsb, xlsb;
  187. Wire.beginTransmission(BMP085_ADDRESS);
  188. Wire.write(0xf6); // register to read
  189. Wire.endTransmission();
  190.  
  191. Wire.requestFrom(BMP085_ADDRESS, 3); // read a byte
  192. while(!Wire.available()) {
  193. // waiting
  194. }
  195. msb = Wire.read();
  196. while(!Wire.available()) {
  197. // waiting
  198. }
  199. lsb |= Wire.read();
  200. while(!Wire.available()) {
  201. // waiting
  202. }
  203. xlsb |= Wire.read();
  204. return (((long)msb<<16) | ((long)lsb<<8) | ((long)xlsb)) >>(8-oversampling_setting);
  205. }
  206.  
  207. void write_register(unsigned char r, unsigned char v)
  208. {
  209. Wire.beginTransmission(BMP085_ADDRESS);
  210. Wire.write(r);
  211. Wire.write(v);
  212. Wire.endTransmission();
  213. }
  214.  
  215. char read_register(unsigned char r)
  216. {
  217. unsigned char v;
  218. Wire.beginTransmission(BMP085_ADDRESS);
  219. Wire.write(r); // register to read
  220. Wire.endTransmission();
  221.  
  222. Wire.requestFrom(BMP085_ADDRESS, 1); // read a byte
  223. while(!Wire.available()) {
  224. // waiting
  225. }
  226. v = Wire.read();
  227. return v;
  228. }
  229.  
  230. int read_int_register(unsigned char r)
  231. {
  232. unsigned char msb, lsb;
  233. Wire.beginTransmission(BMP085_ADDRESS);
  234. Wire.write(r); // register to read
  235. Wire.endTransmission();
  236.  
  237. Wire.requestFrom(BMP085_ADDRESS, 2); // read a byte
  238. while(!Wire.available()) {
  239. // waiting
  240. }
  241. msb = Wire.read();
  242. while(!Wire.available()) {
  243. // waiting
  244. }
  245. lsb = Wire.read();
  246. return (((int)msb<<8) | ((int)lsb));
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement