BMP280气压传感器的详细使用介绍

最后更新日期:2023-10-6
我借鉴了这位大佬的文章
本文主要用于记录调试过程,供本人自己回看为主,待调试完全结束后会详细介绍用法。

一、背景介绍

1、我买的BMP280模块只有四个引脚:VCC、GND、SCL、SDA,只能使用I2C协议
2、I2C协议下的设备地址:0b111011x,x由SDO引脚电平决定,即0x76,0x77。对于我买的这个BMP280模块来说,看不到SOD引脚。我使用Linux下的I2C-Tools工具查看,此设备的地址为0x76。
在这里插入图片描述
在这里插入图片描述

二、概念介绍

1、 先明白使用BMP280到底是什么个步骤?对于i2c设备来说,一般都是去读取设备里的寄存器来获取数据,同时也可以往设备里的寄存器写值来设置参数。所以本质就是通过i2c协议操作目标设备的寄存器。那怎么知道BMP280有哪些寄存器?请看数据手册。有些同学会疑问,我手册倒是看的七七八八,但实际操作一头雾水,无从下手。

三、寄存器介绍

在这里插入图片描述
最左边一栏 Register Name 就是BMP280的全部寄存器。下面先依次介绍各寄存器,完了再介绍如何使用及使用流程。

1、Register id - 0xD0
#暂无中文解释
The “id” register contains the chip identification number chip_id[7:0], which is 0x58. 
This number can be read as soon as the device finished the power-on-reset.
2、Register reset - 0xE0
#往寄存器0xE0写入0xB6即可软重启
The “reset” register contains the soft reset word reset[7:0]. 
If the value 0xB6 is written to the register, the device is
reset using the complete power-on-reset procedure. Writing 
other values than 0xB6 has no effect. The readout value is always 0x00.
3、Register status - 0xF3
#从寄存器0xF3读取一个字节,判断该字节的Bit3和Bit0可以知道当前设备状态。
#下图为两个数据位的解释。
The “status” register contains two bits which indicate the status of the device.

在这里插入图片描述

4、Register ctrl_meas - 0xF4 
#暂无中文解释
The “ctrl_meas” register sets the data acquisition options of the device.

在这里插入图片描述

5、Register config - 0xF5
#暂无中文解释
The “config” register sets the rate, filter and interface options of the device. 
Writes to the “config” register in normal mode may be ignored. 
In sleep mode writes are not ignored.

在这里插入图片描述

6、Register press - 0xF7-0xF9
#这就是大气压数据,但读出来的值不能直接使用,需要进行计算
#暂无其它中文解释
The “press” register contains the raw pressure measurement output data up[19:0]. 
For details on how to read out the pressure and temperature information from the device.

在这里插入图片描述

7、Register temp - 0xFA-0xFC
#这就是温度数据,共占3个寄存器,分别是0xfa(MSB)、0xfb(LSB)、0xfc(XLSB)
#所以要读取三个寄存器的值再拼在一起,MSB为最高位,其是LSB,再是XLSB。
#注意寄存器0xfc(XLSB)的有效位只有高4位(bit7 bit6 bit5 bit4)。低四位为均默认为0,不能包含在内。
#举例:寄存器0xfa的值为0x87,寄存器0xfb的值为0xfa,寄存器0xfc的值为0x80。拼在一起就是0x87fa8,而不是0x87fa80。
#注意:该三个寄存器拼出来的值并不是最后的温度值,而是还需要一些操作的。后面介绍。
The “temp” register contains the raw temperature measurement output data ut[19:0]. 
For details on how to read out the pressure and temperature information from the device.

在这里插入图片描述
以上就是所有寄存器的功能介绍,接下来介绍用法。

四、使用介绍

1、BMP280上电后,先设置寄存器0xf4(ctrl_meas)和0xf5(config)进行初始化
这里暂未研究,参考的别人的设置,如下。

往 0xf4 写入 0x55 #选择16倍气压过采样,2倍温度过采样,force mode
往 0xf5 写入 0x10 #由于使用force mode时间不关心,全填0,滤波器使用16倍,SPI模式不关心,写0

2、读取温度数据,就像上面介绍temp寄存器一样的用法。

读 0xfa、0xfb、0xfc,随后将值拼在一起。
上面说了这值不能直接使用,那该怎么办?
BMP280还有一些寄存器是用来存放修正参数的,修正参数是固定的、是需要参与计算的,所以得先取出来。
以下是所有修正参数的寄存器

在这里插入图片描述

其中dig_T1 ~ dig_T3是温度的修正参数;
其中dig_P1 ~ dig_P9是大气压的修正参数;
一样是MSB为高位,LSB为低位。例如0x89为6e,0x88为0xbc,则dig_T1为0x6ebc;
修正参数知道了,那参与计算的算法是啥?BMP280数据手册有给出。如下:
//BMP280.adc_T就是那三个temp寄存器(0x7a-0x7c)拼出来的值
//BMP280.T1~BMP280.T3就是dig_T1~dig_T3
float BMP280_Compute_T(void)	//温度计算
{
	  int32_t var1, var2,T;
	  var1 = ((((BMP280.adc_T >> 3) - ((int32_t)BMP280.T1 << 1))) *((int32_t)BMP280.T2)) >>11;
	  var2 = (((((BMP280.adc_T >> 4) - ((int32_t)BMP280.T1)) *((BMP280.adc_T >> 4) - ((int32_t)BMP280.T1))) >>12) *((int32_t)BMP280.T3)) >>14;
	  BMP280.t_fine = var1 + var2;
	  T = (BMP280.t_fine * 5 + 128) >> 8;
	  return (float)T/100;
} 

//暂无中文解释
float BMP280_Compute_P(void)	//大气压计算
{
	  int64_t var1, var2, p;
	  var1 = ((int64_t)BMP280.t_fine) - 128000;
	  var2 = var1 * var1 * (int64_t)BMP280.P6;
	  var2 = var2 + ((var1 * (int64_t)BMP280.P5) << 17);
	  var2 = var2 + (((int64_t)BMP280.P4) << 35);
	  var1 = ((var1 * var1 * (int64_t)BMP280.P3) >> 8) +((var1 * (int64_t)BMP280.P2) << 12);
	  var1 =(((((int64_t)1) << 47) + var1)) * ((int64_t)BMP280.P1) >> 33;
	  if (var1 == 0)
	  {
		    return 0;
	  }
	  else
	  {
	    	p = 1048576 - BMP280.adc_P;
    		p = (((p << 31) - var2) * 3125) / var1;
    		var1 = (((int64_t)BMP280.P9) * (p >> 13) * (p >> 13)) >> 25;
    		var2 = (((int64_t)BMP280.P8) * p) >> 19;
    		p = ((p + var1 + var2) >> 8) + (((int64_t)BMP280.P7) << 4);
    		return (float)p/256;
	  }
}

每次测量结束后,要重新往寄存器ctrl_meas的低两位mode[1:0]上写0b01或0b10使能传感器又一次测量,否则每次读取的值都是一样的。

往寄存器ctrl_meas 0xF4 写入 0x56或0x55 后再读取新值

五、总结

对于BMP280的使用,目前先介绍到这。会继续完善此文章。

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值