特性曲线
模块电原理图
需要用的 “SFE_BMP180” 库文件如下 SFE_BMP180.h - /*
- 【Arduino】66种传感器模块系列实验(68)
- 实验六十八:BMP180 新款 BOSCH温度模块气压传感器(代替BMP085)
- SFE_BMP180.h
- */
- #ifndef SFE_BMP180_h
- #define SFE_BMP180_h
- #if defined(ARDUINO) && ARDUINO >= 100
- #include "Arduino.h"
- #else
- #include "WProgram.h"
- #endif
- class SFE_BMP180
- {
- public:
- SFE_BMP180();
- char begin();
- shorted?)
-
- char startTemperature(void);
-
- char getTemperature(double &T);
- startTemperature command
-
- char startPressure(char oversampling);
-
- char getPressure(double &P, double &T);
- startPressure command
- double sealevel(double P, double A);
- weather data)
-
- double altitude(double P, double P0);
- sea-level, runway, etc.)
-
- char getError(void);
-
- private:
-
- char readInt(char address, int16_t &value);
-
- char readUInt(char address, uint16_t &value);
-
- char readBytes(unsigned char *values, char length);
-
-
- char writeBytes(unsigned char *values, char length);
- subsequent registers)
-
- int16_t AC1,AC2,AC3,VB1,VB2,MB,MC,MD;
- uint16_t AC4,AC5,AC6;
- double c5,c6,mc,md,x0,x1,x2,y0,y1,y2,p0,p1,p2;
- char _error;
- };
- #define BMP180_ADDR 0x77
- #define BMP180_REG_CONTROL 0xF4
- #define BMP180_REG_RESULT 0xF6
- #define BMP180_COMMAND_TEMPERATURE 0x2E
- #define BMP180_COMMAND_PRESSURE0 0x34
- #define BMP180_COMMAND_PRESSURE1 0x74
- #define BMP180_COMMAND_PRESSURE2 0xB4
- #define BMP180_COMMAND_PRESSURE3 0xF4
SFE_BMP180.cp - /*
- 【Arduino】66种传感器模块系列实验(68)
- 实验六十八:BMP180 新款 BOSCH温度模块气压传感器(代替BMP085)
- SFE_BMP180.cp
- */
- #include <SFE_BMP180.h>
- #include <Wire.h>
- #include <stdio.h>
- #include <math.h>
- SFE_BMP180::SFE_BMP180()
- {
- }
- char SFE_BMP180::begin()
- {
- double c3,c4,b1;
- Wire.begin();
- if (readInt(0xAA,AC1) &&
- readInt(0xAC,AC2) &&
- readInt(0xAE,AC3) &&
- readUInt(0xB0,AC4) &&
- readUInt(0xB2,AC5) &&
- readUInt(0xB4,AC6) &&
- readInt(0xB6,VB1) &&
- readInt(0xB8,VB2) &&
- readInt(0xBA,MB) &&
- readInt(0xBC,MC) &&
- readInt(0xBE,MD))
- {
- AC6 = 23153;
- Calcs.pdf
- 25671; AC6 = 18974;
- c3 = 160.0 * pow(2,-15) * AC3;
- c4 = pow(10,-3) * pow(2,-15) * AC4;
- b1 = pow(160,2) * pow(2,-30) * VB1;
- c5 = (pow(2,-15) / 160) * AC5;
- c6 = AC6;
- mc = (pow(2,11) / pow(160,2)) * MC;
- md = MD / 160.0;
- x0 = AC1;
- x1 = 160.0 * pow(2,-13) * AC2;
- x2 = pow(160,2) * pow(2,-25) * VB2;
- y0 = c4 * pow(2,15);
- y1 = c4 * c3;
- y2 = c4 * b1;
- p0 = (3791.0 - 8.0) / 1600.0;
- p1 = 1.0 - 7357.0 * pow(2,-20);
- p2 = 3038.0 * 100.0 * pow(2,-36);
- return(1);
- }
- else
- {
- return(0);
- }
- }
- char SFE_BMP180::readInt(char address, int16_t &value)
- {
- unsigned char data[2];
- data[0] = address;
- if (readBytes(data,2))
- {
- value = (int16_t)((data[0]<<8)|data[1]);
- negative
- return(1);
- }
- value = 0;
- return(0);
- }
- char SFE_BMP180::readUInt(char address, uint16_t &value)
- {
- unsigned char data[2];
- data[0] = address;
- if (readBytes(data,2))
- {
- value = (((uint16_t)data[0]<<8)|(uint16_t)data[1]);
- return(1);
- }
- value = 0;
- return(0);
- }
- char SFE_BMP180::readBytes(unsigned char *values, char length)
- {
- char x;
- Wire.beginTransmission(BMP180_ADDR);
- Wire.write(values[0]);
- _error = Wire.endTransmission();
- if (_error == 0)
- {
- Wire.requestFrom(BMP180_ADDR,length);
- while(Wire.available() != length) ;
- for(x=0;x<length;x++)
- {
- values[x] = Wire.read();
- }
- return(1);
- }
- return(0);
- }
- char SFE_BMP180::writeBytes(unsigned char *values, char length)
- {
- char x;
- Wire.beginTransmission(BMP180_ADDR);
- Wire.write(values,length);
- _error = Wire.endTransmission();
- if (_error == 0)
- return(1);
- else
- return(0);
- }
- char SFE_BMP180::startTemperature(void)
- {
- unsigned char data[2], result;
- data[0] = BMP180_REG_CONTROL;
- data[1] = BMP180_COMMAND_TEMPERATURE;
- result = writeBytes(data, 2);
- if (result)
- return(5);
- else
- return(0);
- }
- char SFE_BMP180::getTemperature(double &T)
- {
- unsigned char data[2];
- char result;
- double tu, a;
- data[0] = BMP180_REG_RESULT;
- result = readBytes(data, 2);
- if (result)
- {
- tu = (data[0] * 256.0) + data[1];
- a = c5 * (tu - c6);
- T = a + (mc / (a + md));
- }
- return(result);
- }
- char SFE_BMP180::startPressure(char oversampling)
- {
- unsigned char data[2], result, delay;
- data[0] = BMP180_REG_CONTROL;
- switch (oversampling)
- {
- case 0:
- data[1] = BMP180_COMMAND_PRESSURE0;
- delay = 5;
- break;
- case 1:
- data[1] = BMP180_COMMAND_PRESSURE1;
- delay = 8;
- break;
- case 2:
- data[1] = BMP180_COMMAND_PRESSURE2;
- delay = 14;
- break;
- case 3:
- data[1] = BMP180_COMMAND_PRESSURE3;
- delay = 26;
- break;
- default:
- data[1] = BMP180_COMMAND_PRESSURE0;
- delay = 5;
- break;
- }
- result = writeBytes(data, 2);
- if (result)
- return(delay);
- else
- return(0);
- }
- char SFE_BMP180::getPressure(double &P, double &T)
- sealevel().
- {
- unsigned char data[3];
- char result;
- double pu,s,x,y,z;
- data[0] = BMP180_REG_RESULT;
- result = readBytes(data, 3);
- if (result)
- {
- pu = (data[0] * 256.0) + data[1] + (data[2]/256.0);
- s = T - 25.0;
- x = (x2 * pow(s,2)) + (x1 * s) + x0;
- y = (y2 * pow(s,2)) + (y1 * s) + y0;
- z = (pu - x) / y;
- P = (p2 * pow(z,2)) + (p1 * z) + p0;
- }
- return(result);
- }
- double SFE_BMP180::sealevel(double P, double A)
- {
- return(P/pow(1-(A/44330.0),5.255));
- }
- double SFE_BMP180::altitude(double P, double P0)
- {
- return(44330.0*(1-pow(P/P0,1/5.255)));
- }
- char SFE_BMP180::getError(void)
- {
- return(_error);
- }
模块接线方式 1.先连接芯片与单片机(通过I2C接口),按照如下方式连接 5V—VIN GND–GND A5—SCL A4—SDA 2.然后UNO通过usb与PC电脑连接 3.拷贝后面的代码烧录进Arduino
实验开源代码
- /*
- 【Arduino】168种传感器模块系列实验(67)
- 实验六十七:BMP180 新款 BOSCH温度模块气压传感器(代替BMP085)
- 程序之一
- */
- #include <SFE_BMP180.h>
- SFE_BMP180 AirPresure;
- char presureDelayTime;
- double presureP, presureT;
- void setup() {
- Serial.begin(9600);
- AirPresure.begin();
- }
- void loop()
- {
- presureDelayTime = AirPresure.startPressure(3);
- if (presureDelayTime != 0)
- {
- delay(presureDelayTime);
- presureDelayTime = AirPresure.getPressure(presureP, presureT);
- if (presureDelayTime != 0)
- {
-
- Serial.print("Current Preasure: ");
- Serial.print(presureP);
- Serial.println(" bar");
- Serial.print(presureP);
- Serial.print(" bar is");
- Serial.print(presureP / 1000.0);
- Serial.println(" atm");
- }
- else
- {
- Serial.println("ERROR");
- }
- }
- else
- {
- Serial.println("ERROR");
- }
- delay(1000);
- }
- /*
- 【Arduino】168种传感器模块系列实验(67)
- 实验六十七:BMP180 新款 BOSCH温度模块气压传感器(代替BMP085)
- 程序之二,气温、气压与海拔值可以调整并校准
- */
- #include <Wire.h>
- #define BMP180ADD 0x77
-
- unsigned char OSS;
- int ac1;
- int ac2;
- int ac3;
- unsigned int ac4;
- unsigned int ac5;
- unsigned int ac6;
- int b1;
- int b2;
- int mb;
- int mc;
- int md;
- float temperature;
- double pressure;
- double pressure2;
- long b5;
- double altitude;
- void setup()
- {
- Serial.begin(9600);
- Wire.begin();
- OSS = 2;
- BMP180start();
- }
- void loop()
- {
- calculate();
- show();
- delay(1000);
- }
- void calculate()
- {
- temperature = bmp180GetTemperature(bmp180ReadUT());
- temperature = temperature*0.0137;
- pressure = bmp180GetPressure(bmp180ReadUP());
- pressure2 = pressure/115325;
- pressure2 = pow(pressure2,0.29029496);
- altitude = 39*(1+pressure2);
- }
- void show()
- {
- Serial.print("气温: ");
- Serial.print(temperature, 1);
- Serial.println(" C");
- Serial.print("气压: ");
- Serial.print(pressure, 0);
- Serial.println(" Pa");
- Serial.print("海拔:");
- Serial.print(altitude);
- Serial.println("m");
- }
- void BMP180start()
- {
- ac1 = bmp180ReadDate(0xAA);
- ac2 = bmp180ReadDate(0xAC);
- ac3 = bmp180ReadDate(0xAE);
- ac4 = bmp180ReadDate(0xB0);
- ac5 = bmp180ReadDate(0xB2);
- ac6 = bmp180ReadDate(0xB4);
- b1 = bmp180ReadDate(0xB6);
- b2 = bmp180ReadDate(0xB8);
- mb = bmp180ReadDate(0xBA);
- mc = bmp180ReadDate(0xBC);
- md = bmp180ReadDate(0xBE);
- }
- short bmp180GetTemperature(unsigned int ut)
- {
- long x1, x2;
- x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
- x2 = ((long)mc << 11)/(x1 + md);
- b5 = x1 + x2;
- return ((b5 + 8)>>4);
- }
- long bmp180GetPressure(unsigned long up)
- {
- long x1, x2, x3, b3, b6, p;
- unsigned long b4, b7;
-
- b6 = b5 - 4000;
- x1 = (b2 * (b6 * b6)>>12)>>11;
- x2 = (ac2 * b6)>>11;
- x3 = x1 + x2;
- b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
-
- x1 = (ac3 * b6)>>13;
- x2 = (b1 * ((b6 * b6)>>12))>>16;
- x3 = ((x1 + x2) + 2)>>2;
- b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
-
- b7 = ((unsigned long)(up - b3) * (50000>>OSS));
- if (b7 < 0x80000000)
- p = (b7<<1)/b4;
- else
- p = (b7/b4)<<1;
-
- x1 = (p>>8) * (p>>8);
- x1 = (x1 * 3038)>>16;
- x2 = (-7357 * p)>>16;
- p += (x1 + x2 + 3791)>>4;
-
- return p;
- }
- int bmp180Read(unsigned char address)
- {
- unsigned char data;
-
- Wire.beginTransmission(BMP180ADD);
- Wire.write(address);
- Wire.endTransmission();
-
- Wire.requestFrom(BMP180ADD, 1);
- while(!Wire.available());
-
- return Wire.read();
- }
- int bmp180ReadDate(unsigned char address)
- {
- unsigned char msb, lsb;
- Wire.beginTransmission(BMP180ADD);
- Wire.write(address);
- Wire.endTransmission();
- Wire.requestFrom(BMP180ADD, 2);
- while(Wire.available()<2);
- msb = Wire.read();
- lsb = Wire.read();
- return (int) msb<<8 | lsb;
- }
- unsigned int bmp180ReadUT()
- {
- unsigned int ut;
- Wire.beginTransmission(BMP180ADD);
- Wire.write(0xF4);
- Wire.write(0x2E);
- Wire.endTransmission();
- delay(5);
- ut = bmp180ReadDate(0xF6);
- return ut;
- }
- unsigned long bmp180ReadUP()
- {
- unsigned char msb, lsb, xlsb;
- unsigned long up = 0;
-
- Wire.beginTransmission(BMP180ADD);
- Wire.write(0xF4);
- Wire.write(0x34 + (OSS<<6));
- Wire.endTransmission();
- delay(2 + (3<<OSS));
-
- Wire.beginTransmission(BMP180ADD);
- Wire.write(0xF6);
- Wire.endTransmission();
-
- Wire.requestFrom(BMP180ADD, 3);
- while(Wire.available() < 3);
- msb = Wire.read();
- lsb = Wire.read();
- xlsb = Wire.read();
- up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);//16 to 19 bit
- return up;
- }
|
|
yzw92 2020-12-28 06:35