极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14926|回复: 4

BMP180加oled显示

[复制链接]
发表于 2016-9-3 12:28:49 | 显示全部楼层 |阅读模式
感谢do335maomao的教程。
做了一个oled显示



[/code]
#include <SFE_BMP180.h>
#include <Wire.h>
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST);        // Fast I2C / TWI
SFE_BMP180 pressure;// 创建一个气压计对象
double baseline; // 基准气压
double pp,tt,hh;

void setup()
{
  Serial.begin(9600);
  Serial.println("REBOOT");

  // 初始化传感器
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // 糟糕,气压计出问题了,多半是连线有问题
    Serial.println("BMP180 init fail (disconnected?)\n\n");
    while(1); // 暂停
  }

  //获得基准气压
  baseline = getP();

  Serial.print("baseline pressure: ");
  Serial.print(baseline);
  Serial.println(" hPa");  
}

void loop()
{
  double a,p,t;
  p = getP();// 获得一个气压值
  a = pressure.altitude(p,baseline);//获得基于基准气压的高度值
  pp=p;
  hh=a;
  Serial.print("relative altitude: ");
  if (a >= 0.0) Serial.print(" "); // 调整正数显示格式
  Serial.print(a,1);
  Serial.print(" meters ");  

  t = getT();// 获得一个温度值
  Serial.print("temperature: ");
  Serial.print(t,1);
  Serial.println(" degrees");  
tt=t;

  delay(500);//刷新率

//======开始显示==========
  u8g.firstPage();                             
  do {
    draw();
  } while( u8g.nextPage() );
}


double getP()
{
  char status;
  double T,P,p0,a;
  // You must first get a temperature measurement to perform a pressure reading.
  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.
  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);
    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Use '&T' to provide the address of T to the function.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.
      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);
        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Use '&P' to provide the address of P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.
        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          return P;
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}

double getT()
{
  char status;
  double T,p0;
  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    {
      status = pressure.startPressure(3);
      return T;
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}
void draw(void) {
  u8g.setPrintPos(0, 20);      //定义显示位置 横向起始位置0 纵向45
  u8g.print("T");
  u8g.print(":");   
  u8g.print(tt);
  u8g.print(" C");
  u8g.setPrintPos(0, 40);
  u8g.print("P");
  u8g.print(":");
  u8g.print(pp);  
  u8g.print(" HPA");
  u8g.setPrintPos(0, 60);
  u8g.print("H");                //显示分钟的值
  u8g.print(":");                        //显示一个冒号
  u8g.print(hh);
  u8g.print(" M");
  u8g.setFont(u8g_font_freedoomr10r);  //调整显示字体
  
}


[/code]

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2016-9-6 19:26:01 | 显示全部楼层
附加do335maomao的原文
http://www.geek-workshop.com/for ... mp;highlight=bmp180
温度不是很准啊
回复 支持 反对

使用道具 举报

发表于 2017-9-30 15:20:24 | 显示全部楼层
在验证时出现以下的错误,搞不懂。
请指点,谢谢!

sketch_sep30b.ino:4:27: error: 'U8G_I2C_OPT_DEV_0' was not declared in this scope
sketch_sep30b.ino:4:45: error: 'U8G_I2C_OPT_NO_ACK' was not declared in this scope
sketch_sep30b.ino:4:64: error: 'U8G_I2C_OPT_FAST' was not declared in this scope
编译有误。
回复 支持 反对

使用道具 举报

发表于 2017-9-30 16:44:10 | 显示全部楼层
元器件是什么 ?接线图如何?尽可能讲详细点,谢谢!!
回复 支持 反对

使用道具 举报

发表于 2017-10-1 14:18:21 | 显示全部楼层
最帅的老饼 发表于 2017-9-30 16:44
元器件是什么 ?接线图如何?尽可能讲详细点,谢谢!!

楼主已经写了BMP180
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 20:54 , Processed in 0.053571 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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