Advertisement
Guest User

Untitled

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