找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2897|回复: 1
打印 上一主题 下一主题
收起左侧

GY68BMP180气压模块Arduino的源程序分享

[复制链接]
跳转到指定楼层
楼主
ID:286851 发表于 2018-3-7 14:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
分享 GY68BMP180 气压模块   Arduino 的例程序

  1. //Arduino 1.0+ Only
  2. //Arduino 1.0+ Only

  3. /*Based largely on code by  Jim Lindblom

  4. Get pressure, altitude, and temperature from the BMP085.
  5. Serial.print it out at 9600 baud to serial monitor.
  6. */

  7. #include <Wire.h>

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

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

  10. // Calibration values
  11. int ac1;
  12. int ac2;
  13. int ac3;
  14. unsigned int ac4;
  15. unsigned int ac5;
  16. unsigned int ac6;
  17. int b1;
  18. int b2;
  19. int mb;
  20. int mc;
  21. int md;

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

  25. void setup(){
  26.   Serial.begin(9600);
  27.   Wire.begin();

  28.   bmp085Calibration();
  29. }

  30. void loop()
  31. {
  32.   float temperature = bmp085GetTemperature(bmp085ReadUT()); //MUST be called first
  33.   float pressure = bmp085GetPressure(bmp085ReadUP());
  34.   float atm = pressure / 101325; // "standard atmosphere"
  35.   float altitude = calcAltitude(pressure); //Uncompensated caculation - in Meters

  36.   Serial.print("Temperature: ");
  37.   Serial.print(temperature, 2); //display 2 decimal places
  38.   Serial.println("deg C");

  39.   Serial.print("Pressure: ");
  40.   Serial.print(pressure, 0); //whole number only.
  41.   Serial.println(" Pa");

  42.   Serial.print("Standard Atmosphere: ");
  43.   Serial.println(atm, 4); //display 4 decimal places

  44.   Serial.print("Altitude: ");
  45.   Serial.print(altitude, 2); //display 2 decimal places
  46.   Serial.println(" M");

  47.   Serial.println();//line break

  48.   delay(1000); //wait a second and get values again.
  49. }

  50. // Stores all of the bmp085's calibration values into global variables
  51. // Calibration values are required to calculate temp and pressure
  52. // This function should be called at the beginning of the program
  53. void bmp085Calibration()
  54. {
  55.   ac1 = bmp085ReadInt(0xAA);
  56.   ac2 = bmp085ReadInt(0xAC);
  57.   ac3 = bmp085ReadInt(0xAE);
  58.   ac4 = bmp085ReadInt(0xB0);
  59.   ac5 = bmp085ReadInt(0xB2);
  60.   ac6 = bmp085ReadInt(0xB4);
  61.   b1 = bmp085ReadInt(0xB6);
  62.   b2 = bmp085ReadInt(0xB8);
  63.   mb = bmp085ReadInt(0xBA);
  64.   mc = bmp085ReadInt(0xBC);
  65.   md = bmp085ReadInt(0xBE);
  66. }

  67. // Calculate temperature in deg C
  68. float bmp085GetTemperature(unsigned int ut){
  69.   long x1, x2;

  70.   x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  71.   x2 = ((long)mc << 11)/(x1 + md);
  72.   b5 = x1 + x2;

  73.   float temp = ((b5 + 8)>>4);
  74.   temp = temp /10;

  75.   return temp;
  76. }

  77. // Calculate pressure given up
  78. // calibration values must be known
  79. // b5 is also required so bmp085GetTemperature(...) must be called first.
  80. // Value returned will be pressure in units of Pa.
  81. long bmp085GetPressure(unsigned long up){
  82.   long x1, x2, x3, b3, b6, p;
  83.   unsigned long b4, b7;

  84.   b6 = b5 - 4000;
  85.   // Calculate B3
  86.   x1 = (b2 * (b6 * b6)>>12)>>11;
  87.   x2 = (ac2 * b6)>>11;
  88.   x3 = x1 + x2;
  89.   b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;

  90.   // Calculate B4
  91.   x1 = (ac3 * b6)>>13;
  92.   x2 = (b1 * ((b6 * b6)>>12))>>16;
  93.   x3 = ((x1 + x2) + 2)>>2;
  94.   b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;

  95.   b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  96.   if (b7 < 0x80000000)
  97.     p = (b7<<1)/b4;
  98.   else
  99.     p = (b7/b4)<<1;

  100.   x1 = (p>>8) * (p>>8);
  101.   x1 = (x1 * 3038)>>16;
  102.   x2 = (-7357 * p)>>16;
  103.   p += (x1 + x2 + 3791)>>4;

  104.   long temp = p;
  105.   return temp;
  106. }

  107. // Read 1 byte from the BMP085 at 'address'
  108. char bmp085Read(unsigned char address)
  109. {
  110.   unsigned char data;

  111.   Wire.beginTransmission(BMP085_ADDRESS);
  112.   Wire.write(address);
  113.   Wire.endTransmission();

  114.   Wire.requestFrom(BMP085_ADDRESS, 1);
  115.   while(!Wire.available())
  116.     ;

  117.   return Wire.read();
  118. }

  119. // Read 2 bytes from the BMP085
  120. // First byte will be from 'address'
  121. // Second byte will be from 'address'+1
  122. int bmp085ReadInt(unsigned char address)
  123. {
  124.   unsigned char msb, lsb;

  125.   Wire.beginTransmission(BMP085_ADDRESS);
  126.   Wire.write(address);
  127.   Wire.endTransmission();

  128.   Wire.requestFrom(BMP085_ADDRESS, 2);
  129.   while(Wire.available()<2)
  130.     ;
  131.   msb = Wire.read();
  132.   lsb = Wire.read();

  133.   return (int) msb<<8 | lsb;
  134. }

  135. // Read the uncompensated temperature value
  136. unsigned int bmp085ReadUT(){
  137.   unsigned int ut;

  138.   // Write 0x2E into Register 0xF4
  139.   // This requests a temperature reading
  140.   Wire.beginTransmission(BMP085_ADDRESS);
  141.   Wire.write(0xF4);
  142.   Wire.write(0x2E);
  143.   Wire.endTransmission();

  144.   // Wait at least 4.5ms
  145.   delay(5);

  146.   // Read two bytes from registers 0xF6 and 0xF7
  147.   ut = bmp085ReadInt(0xF6);
  148.   return ut;
  149. }

  150. // Read the uncompensated pressure value
  151. unsigned long bmp085ReadUP(){

  152.   unsigned char msb, lsb, xlsb;
  153.   unsigned long up = 0;

  154.   // Write 0x34+(OSS<<6) into register 0xF4
  155.   // Request a pressure reading w/ oversampling setting
  156.   Wire.beginTransmission(BMP085_ADDRESS);
  157.   Wire.write(0xF4);
  158.   Wire.write(0x34 + (OSS<<6));
  159.   Wire.endTransmission();

  160.   // Wait for conversion, delay time dependent on OSS
  161.   delay(2 + (3<<OSS));

  162.   // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  163.   msb = bmp085Read(0xF6);
  164.   lsb = bmp085Read(0xF7);
  165.   xlsb = bmp085Read(0xF8);

  166.   up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);

  167.   return up;
  168. }

  169. void writeRegister(int deviceAddress, byte address, byte val) {
  170.   Wire.beginTransmission(deviceAddress); // start transmission to device
  171.   Wire.write(address);       // send register address
  172.   Wire.write(val);         // send value to write
  173.   Wire.endTransmission();     // end transmission
  174. }

  175. int readRegister(int deviceAddress, byte address){

  176.   int v;
  177.   Wire.beginTransmission(deviceAddress);
  178.   Wire.write(address); // register to read
  179.   Wire.endTransmission();

  180.   Wire.requestFrom(deviceAddress, 1); // read a byte

  181.   while(!Wire.available()) {
  182.     // waiting
  183.   }

  184.   v = Wire.read();
  185.   return v;
  186. }

  187. float calcAltitude(float pressure){

  188.   float A = pressure/101325;
  189.   float B = 1/5.25588;
  190.   float C = pow(A,B);
  191.   C = 1 - C;
  192.   C = C /0.0000225577;

  193.   return C;
  194. }
复制代码

所有资料51hei提供下载:
bmp085_arduino.rar (2.25 KB, 下载次数: 16)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:310897 发表于 2018-4-18 14:55 | 只看该作者
感谢楼主分享!
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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