Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include "Wire.h"
- LiquidCrystal lcd(12,11, 5, 4, 3, 2);
- float pressao;
- float p0,P;
- int altitude;
- byte i=0;
- float accuml =0; // varialvel para guardar o valor
- float temp = 0 ; //variavel para guardar o valor
- #define BMP085_ADDRESS 0x77
- const unsigned char oversampling_setting = 3;
- const unsigned char pressure_waittime[4] = {
- 5, 8, 14, 26 };
- int ac1, ac2, ac3;
- unsigned int ac4, ac5, ac6;
- int b1, b2, mb, mc, md;
- int temperature=0;
- long pressure=0;
- //double Po;
- double pressaoB = 101325 ;
- long b5;
- void setup()
- {
- Serial.begin(9600); // start serial for output
- lcd.begin(16,2);
- lcd.setCursor(10, 2); // Posiciona cursor posição 6 linha 0
- lcd.print("P"); // Imprime P Pressao
- lcd.setCursor(11, 0); // Posiciona cursor posição 11 linha 0
- lcd.print("T");
- lcd.setCursor(0,0);
- lcd.print("A");
- lcd.setCursor(1,0);
- lcd.print("-");
- lcd.setCursor(0,1);
- lcd.print("H");
- Serial.println("Setting up BMP085");
- Wire.begin();
- bmp085_get_cal_data( );
- void bmp085_read_temperature_and_pressure(int& temperature,long& pressure);
- Serial.print("Temperature:");
- lcd.setCursor(11,2);
- lcd.print(pressure/100);
- float temp =(float)temperature / 10.0;
- Serial.print(temp,2);
- lcd.setCursor(12,0);
- lcd.print(temp,2);
- float pressao = (float)pressao/100.0;
- Serial.print("Pressao:");
- Serial.print(pressure);
- Serial.print(" ");
- lcd.setCursor(12,2);
- lcd.print(pressure);
- lcd.setCursor(11,2);
- lcd.print("-");
- }
- void loop()
- {
- bmp085_read_temperature_and_pressure(&temperature,& pressure);
- Serial.print("Temperature:");
- float temp =(float)temperature / 10.0;
- // for ( i = 0; i<20; i++); // for de 20 vezes: de 0 a 49 = 50 vezes
- // {
- // temp = temp =(float)temperature / 10.0; ; // le o valor da temperatura
- // accuml = accuml + xpto; // Acumula 20vezes as temperatura lidas
- // }
- //temp_M = accuml / 20; // calcula a temperatura média das ultima 20 leituras
- Serial.print(temp,2);
- lcd.setCursor(12,0);
- lcd.print(temp,2);
- Serial.print("Pressao:");
- Serial.print(pressure/100);
- Serial.print(" ");
- lcd.setCursor(12,2);
- lcd.print(pressure/100);
- pressao= pressure;//
- p0 = pressaoB;
- altitude = (float)44330 * (1 - pow(((float) pressao/p0), 0.1902950));
- Serial.print("ALTITUDE barometrica;");
- Serial.print(altitude );
- Serial.println("m ");
- lcd.setCursor(2,0);
- lcd.print(altitude);
- lcd.setCursor(4,0);
- lcd.print("m");
- //ALTITUDE RELATIVA
- Serial.print("ALTITUDEreal;");
- Serial.print(altitude );
- Serial.print(altitude );
- Serial.println("m ");
- lcd.setCursor(2,1);
- lcd.print(altitude);
- lcd.setCursor(4,1);
- lcd.print("m");
- }
- void bmp085_read_temperature_and_pressure(int* temperature, long* pressure) {
- int ut= bmp085_read_ut();
- long up = bmp085_read_up();
- long x1, x2, x3, b3, b5, b6, p;
- unsigned long b4, b7;
- //calculate the temperature
- x1 = ((long)ut - ac6) * ac5 >> 15;
- x2 = ((long) mc << 11) / (x1 + md);
- b5 = x1 + x2;
- *temperature = (b5 + 8) >> 4;
- //calculate the pressure
- b6 = b5 - 4000;
- x1 = (b2 * (b6 * b6 >> 12)) >> 11;
- x2 = ac2 * b6 >> 11;
- x3 = x1 + x2;
- //b3 = (((int32_t) ac1 * 4 + x3)<> 2;
- if (oversampling_setting == 3) b3 = ((int32_t) ac1 * 4 + x3 + 2) << 1;
- if (oversampling_setting == 2) b3 = ((int32_t) ac1 * 4 + x3 + 2);
- if (oversampling_setting == 1) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 1;
- if (oversampling_setting == 0) b3 = ((int32_t) ac1 * 4 + x3 + 2) >> 2;
- x1 = ac3 * b6 >> 13;
- x2 = (b1 * (b6 * b6 >> 12)) >> 16;
- x3 = ((x1 + x2) + 2) >> 2;
- b4 = (ac4 * (uint32_t) (x3 + 32768)) >> 15;
- b7 = ((uint32_t) up - b3) * (50000 >> oversampling_setting);
- p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
- x1 = (p >> 8) * (p >> 8);
- x1 = (x1 * 3038) >> 16;
- x2 = (-7357 * p) >> 16;
- *pressure = p + ((x1 + x2 + 3791) >> 4);
- }
- unsigned int bmp085_read_ut() {
- write_register(0xf4,0x2e);
- delay(5); //longer than 4.5 ms
- return read_int_register(0xf6);
- }
- void bmp085_get_cal_data() {
- Serial.println("Reading Calibration Data");
- ac1 = read_int_register(0xAA);
- Serial.print("AC1: ");
- Serial.println(ac1,DEC);
- ac2 = read_int_register(0xAC);
- Serial.print("AC2: ");
- Serial.println(ac2,DEC);
- ac3 = read_int_register(0xAE);
- Serial.print("AC3: ");
- Serial.println(ac3,DEC);
- ac4 = read_int_register(0xB0);
- Serial.print("AC4: ");
- Serial.println(ac4,DEC);
- ac5 = read_int_register(0xB2);
- Serial.print("AC5: ");
- Serial.println(ac5,DEC);
- ac6 = read_int_register(0xB4);
- Serial.print("AC6: ");
- Serial.println(ac6,DEC);
- b1 = read_int_register(0xB6);
- Serial.print("B1: ");
- Serial.println(b1,DEC);
- b2 = read_int_register(0xB8);
- Serial.print("B2: ");
- Serial.println(b1,DEC);
- mb = read_int_register(0xBA);
- Serial.print("MB: ");
- Serial.println(mb,DEC);
- mc = read_int_register(0xBC);
- Serial.print("MC: ");
- Serial.println(mc,DEC);
- md = read_int_register(0xBE);
- Serial.print("MD: ");
- Serial.println(md,DEC);
- }
- long bmp085_read_up() {
- write_register(0xf4,0x34+(oversampling_setting<<6));
- delay(pressure_waittime[oversampling_setting]);
- unsigned char msb, lsb, xlsb;
- Wire.beginTransmission(BMP085_ADDRESS);
- Wire.write(0xf6); // register to read
- Wire.endTransmission();
- Wire.requestFrom(BMP085_ADDRESS, 3); // read a byte
- while(!Wire.available()) {
- // waiting
- }
- msb = Wire.read();
- while(!Wire.available()) {
- // waiting
- }
- lsb |= Wire.read();
- while(!Wire.available()) {
- // waiting
- }
- xlsb |= Wire.read();
- return (((long)msb<<16) | ((long)lsb<<8) | ((long)xlsb)) >>(8-oversampling_setting);
- }
- void write_register(unsigned char r, unsigned char v)
- {
- Wire.beginTransmission(BMP085_ADDRESS);
- Wire.write(r);
- Wire.write(v);
- Wire.endTransmission();
- }
- char read_register(unsigned char r)
- {
- unsigned char v;
- Wire.beginTransmission(BMP085_ADDRESS);
- Wire.write(r); // register to read
- Wire.endTransmission();
- Wire.requestFrom(BMP085_ADDRESS, 1); // read a byte
- while(!Wire.available()) {
- // waiting
- }
- v = Wire.read();
- return v;
- }
- int read_int_register(unsigned char r)
- {
- unsigned char msb, lsb;
- Wire.beginTransmission(BMP085_ADDRESS);
- Wire.write(r); // register to read
- Wire.endTransmission();
- Wire.requestFrom(BMP085_ADDRESS, 2); // read a byte
- while(!Wire.available()) {
- // waiting
- }
- msb = Wire.read();
- while(!Wire.available()) {
- // waiting
- }
- lsb = Wire.read();
- return (((int)msb<<8) | ((int)lsb));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement