现在的位置: 首页 > 综合 > 正文

arduino UNO使用Wire模块通过IIC(I2C)与BMP085通信

2013年08月31日 ⁄ 综合 ⁄ 共 1806字 ⁄ 字号 评论关闭

arduino是一开源的硬件软件平台,最近是火的要命。喜欢尝试的自己就买了个入门级的UNO试着玩了玩。因为工作的原因,想做一个气压记录硬件。通过搜索找到了BMP085模块,在淘宝上有卖的大概20几块钱一个。IIC总线就不多说了。反正就是一个通信方式而已,很多元件都用这个。

在arduino的软件平台里,自带的Wire库封装了对于IIC操作的方法。在arduino UNO硬件上IIC的SDA、SCL引脚分别对应了A4、A5口。我买的BMP085标成的最大供电电压为3.5V,所以使用了UNO中3.3V的电压输出,就可以直接驱动BMP85模块了。

IIC与BMP085通信流程

读取数据时BMP085采取的是先写入地址,然后再等待发送。

读取测量数据时,需要先向BMP085写入相关参数,然后读取相应的数据。

交互流程

首先,读取改正参数。

第二,读取测量数据。

第三,修正数据。

下面是我的初步代码

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

int ac1;// 16bit
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;


void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}
int read2byte(unsigned char Addr)
{
  Wire.beginTransmission(0xEE>>1);
  Wire.write(Addr);
  Wire.endTransmission();
  Wire.requestFrom(0xEE>>1,2);
  int  u = Wire.read(); // receive a byte as character
  int  d = Wire.read();
  return (u<<8 | d);
}
void initBMP085()
{
  ac1 = read2byte(0xAA);
  ac2 = read2byte(0xAC);
  ac3 = read2byte(0xAE);
  ac4 = read2byte(0xB0);
  ac5 = read2byte(0xB2);
  ac6 = read2byte(0xB4);
  b1 = read2byte(0xB6);
  b2 = read2byte(0xB8);
  mb = read2byte(0xBA);
  mc = read2byte(0xBC);
  md = read2byte(0xBE);
}
unsigned int readTemp()
{
  Wire.beginTransmission(0xEE>>1);
  Wire.write(0xF4);
  Wire.write(0x2E);
  Wire.endTransmission();
  
  delay(10);
  
  unsigned int _data = 0;
  Wire.beginTransmission(0xEE>>1);
  Wire.write(0xF6);
  Wire.endTransmission();
  Wire.requestFrom(0xEE>>1,2);
  int  u = Wire.read(); // receive a byte as character
  int  d = Wire.read();
  _data = u<<8;
  _data|=d;
  return _data;
}

void loop()
{
  initBMP085();
  long ut= readTemp();
  ut = readTemp();
  long x1 = (ut-ac6)*ac5>>15;
  long x2 = ((long)mc <<11 )/(x1+md);
  long b5 = x1+x2;
  
  Serial.print((b5+8)>>4);
  Serial.print("done\n");

  delay(500);
}

 

抱歉!评论已关闭.