极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13517|回复: 6

BMP085测气压不准是什么原因?

[复制链接]
发表于 2013-8-4 23:21:15 | 显示全部楼层 |阅读模式
用的是论坛里BMP085一样的代码,估计论坛里面的也是这个代码将注释翻译过来的吧
可是我测的气压不准,不知道什么原因。
测出来的都是97240Pa这样,我测的是大气压啊..不可能这么低,大气压正常的日变幅最高也就600pa左右,一个标准大气压是101000Pa吧,测出来的结果绝对不合理呀!遇到过这问题的能帮忙解决一下么?是不是这传感器是坏的...

符代码:
  1. #include <Wire.h>

  2. #define BMP085_ADDRESS 0x77  // I2C address of BMP085

  3. const unsigned char OSS = 0;  // Oversampling Setting

  4. // Calibration values
  5. int ac1;
  6. int ac2;
  7. int ac3;
  8. unsigned int ac4;
  9. unsigned int ac5;
  10. unsigned int ac6;
  11. int b1;
  12. int b2;
  13. int mb;
  14. int mc;
  15. int md;

  16. // b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
  17. // so ...Temperature(...) must be called before ...Pressure(...).
  18. long b5;

  19. short temperature;
  20. long pressure;

  21. void setup()
  22. {
  23.   Serial.begin(9600);
  24.   Wire.begin();
  25.   bmp085Calibration();
  26. }

  27. void loop()
  28. {
  29.   temperature = bmp085GetTemperature(bmp085ReadUT());
  30.   pressure = bmp085GetPressure(bmp085ReadUP());
  31.   Serial.print("Temperature: ");
  32.   Serial.print(temperature, DEC);
  33.   Serial.println(" *0.1 deg C");
  34.   Serial.print("Pressure: ");
  35.   Serial.print(pressure, DEC);
  36.   Serial.println(" Pa");
  37.   Serial.println();
  38.   delay(1000);
  39. }

  40. // Stores all of the bmp085's calibration values into global variables
  41. // Calibration values are required to calculate temp and pressure
  42. // This function should be called at the beginning of the program
  43. void bmp085Calibration()
  44. {
  45.   ac1 = bmp085ReadInt(0xAA);
  46.   ac2 = bmp085ReadInt(0xAC);
  47.   ac3 = bmp085ReadInt(0xAE);
  48.   ac4 = bmp085ReadInt(0xB0);
  49.   ac5 = bmp085ReadInt(0xB2);
  50.   ac6 = bmp085ReadInt(0xB4);
  51.   b1 = bmp085ReadInt(0xB6);
  52.   b2 = bmp085ReadInt(0xB8);
  53.   mb = bmp085ReadInt(0xBA);
  54.   mc = bmp085ReadInt(0xBC);
  55.   md = bmp085ReadInt(0xBE);
  56. }

  57. // Calculate temperature given ut.
  58. // Value returned will be in units of 0.1 deg C
  59. short bmp085GetTemperature(unsigned int ut)
  60. {
  61.   long x1, x2;
  62.   
  63.   x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  64.   x2 = ((long)mc << 11)/(x1 + md);
  65.   b5 = x1 + x2;

  66.   return ((b5 + 8)>>4);  
  67. }

  68. // Calculate pressure given up
  69. // calibration values must be known
  70. // b5 is also required so bmp085GetTemperature(...) must be called first.
  71. // Value returned will be pressure in units of Pa.
  72. long bmp085GetPressure(unsigned long up)
  73. {
  74.   long x1, x2, x3, b3, b6, p;
  75.   unsigned long b4, b7;
  76.   
  77.   b6 = b5 - 4000;
  78.   // Calculate B3
  79.   x1 = (b2 * (b6 * b6)>>12)>>11;
  80.   x2 = (ac2 * b6)>>11;
  81.   x3 = x1 + x2;
  82.   b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  83.   
  84.   // Calculate B4
  85.   x1 = (ac3 * b6)>>13;
  86.   x2 = (b1 * ((b6 * b6)>>12))>>16;
  87.   x3 = ((x1 + x2) + 2)>>2;
  88.   b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  89.   
  90.   b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  91.   if (b7 < 0x80000000)
  92.     p = (b7<<1)/b4;
  93.   else
  94.     p = (b7/b4)<<1;
  95.    
  96.   x1 = (p>>8) * (p>>8);
  97.   x1 = (x1 * 3038)>>16;
  98.   x2 = (-7357 * p)>>16;
  99.   p += (x1 + x2 + 3791)>>4;
  100.   
  101.   return p;
  102. }

  103. // Read 1 byte from the BMP085 at 'address'
  104. char bmp085Read(unsigned char address)
  105. {
  106.   unsigned char data;
  107.   
  108.   Wire.beginTransmission(BMP085_ADDRESS);
  109.   Wire.write(address);
  110.   Wire.endTransmission();
  111.   
  112.   Wire.requestFrom(BMP085_ADDRESS, 1);
  113.   while(!Wire.available())
  114.     ;
  115.    
  116.   return Wire.read();
  117. }

  118. // Read 2 bytes from the BMP085
  119. // First byte will be from 'address'
  120. // Second byte will be from 'address'+1
  121. int bmp085ReadInt(unsigned char address)
  122. {
  123.   unsigned char msb, lsb;
  124.   
  125.   Wire.beginTransmission(BMP085_ADDRESS);
  126.   Wire.write(address);
  127.   Wire.endTransmission();
  128.   
  129.   Wire.requestFrom(BMP085_ADDRESS, 2);
  130.   while(Wire.available()<2)
  131.     ;
  132.   msb = Wire.read();
  133.   lsb = Wire.read();
  134.   
  135.   return (int) msb<<8 | lsb;
  136. }

  137. // Read the uncompensated temperature value
  138. unsigned int bmp085ReadUT()
  139. {
  140.   unsigned int ut;
  141.   
  142.   // Write 0x2E into Register 0xF4
  143.   // This requests a temperature reading
  144.   Wire.beginTransmission(BMP085_ADDRESS);
  145.   Wire.write(0xF4);
  146.   Wire.write(0x2E);
  147.   Wire.endTransmission();
  148.   
  149.   // Wait at least 4.5ms
  150.   delay(5);
  151.   
  152.   // Read two bytes from registers 0xF6 and 0xF7
  153.   ut = bmp085ReadInt(0xF6);
  154.   return ut;
  155. }

  156. // Read the uncompensated pressure value
  157. unsigned long bmp085ReadUP()
  158. {
  159.   unsigned char msb, lsb, xlsb;
  160.   unsigned long up = 0;
  161.   
  162.   // Write 0x34+(OSS<<6) into register 0xF4
  163.   // Request a pressure reading w/ oversampling setting
  164.   Wire.beginTransmission(BMP085_ADDRESS);
  165.   Wire.write(0xF4);
  166.   Wire.write(0x34 + (OSS<<6));
  167.   Wire.endTransmission();
  168.   
  169.   // Wait for conversion, delay time dependent on OSS
  170.   delay(2 + (3<<OSS));
  171.   
  172.   // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  173.   Wire.beginTransmission(BMP085_ADDRESS);
  174.   Wire.write(0xF6);
  175.   Wire.endTransmission();
  176.   Wire.requestFrom(BMP085_ADDRESS, 3);
  177.   
  178.   // Wait for data to become available
  179.   while(Wire.available() < 3)
  180.     ;
  181.   msb = Wire.read();
  182.   lsb = Wire.read();
  183.   xlsb = Wire.read();
  184.   
  185.   up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
  186.   
  187.   return up;
  188. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2013-8-5 01:00:54 | 显示全部楼层
这个结果是准的....我们这海拔高度是321.7米,用压高公式计算一下,这个结果是对的。大气压随高度增加而降低....
回复 支持 反对

使用道具 举报

发表于 2013-8-5 08:43:55 | 显示全部楼层
呵呵,气压预报通常有参考点,如海拔高度和地理位置(环境密度影响).海拔高度确实会对气压造成影响,不少利用相对气压来换算高度的变换,如登山,飞机等.
回复 支持 反对

使用道具 举报

发表于 2013-8-5 08:47:43 | 显示全部楼层
通常海拔0,气压在1000百帕左右(hPa).
回复 支持 反对

使用道具 举报

发表于 2013-8-5 09:46:12 | 显示全部楼层
970hPa,很正常啊
回复 支持 反对

使用道具 举报

发表于 2013-8-5 12:43:01 | 显示全部楼层
气压和云层和潮汐都有关,有根据气压来进行天晴雨预报的.
回复 支持 反对

使用道具 举报

发表于 2013-8-5 14:19:13 | 显示全部楼层
有没有精度高的气压计???
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-6 20:16 , Processed in 0.036045 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表