BMP280学习

1.Forced mode模式,单次采集后进入休眠,适用于低采样率。

2.normal mode模式,持续采集,我们使用这种

采集事件基本都是ms级,所以我们1s更新一次。

温度和压力的计算

#include <SPI.h>
//定义数据类型
#define s32_t long signed int
#define u32_t long unsigned int
#define u16_t unsigned short
#define s16_t signed short
// 定义从设备选择引脚
const int chipSelectPin = 10;
//=============定义BMP280寄存器===========///
unsigned int  temp_xlsb;  
unsigned int  temp_lsb;
unsigned int  temp_msb;
unsigned int  press_xlsb;
unsigned int  press_lsb;
unsigned int  press_msb;
u16_t dig_t1_lsb;
u16_t dig_t1_msb;
s16_t dig_t2_lsb;
s16_t dig_t2_msb;
s16_t dig_t3_lsb;
s16_t dig_t3_msb;
u32_t dig_t1;
s32_t dig_t2;
s32_t dig_t3;
s32_t temp;
s32_t temp_comp;
u16_t bmp_status;
byte  addr_temp_xlsb = 0xFC;  
byte  addr_temp_lsb = 0xFB;
byte  addr_temp_msb = 0xFA;
byte  addr_press_xlsb = 0xF9;
byte  addr_press_lsb = 0xF8;
byte  addr_press_msb = 0xF7;
byte  addr_dig_t1_lsb = 0x88;
byte  addr_dig_t1_msb = 0x89;
byte  addr_dig_t2_lsb = 0x8A;
byte  addr_dig_t2_msb = 0x8B;
byte  addr_dig_t3_lsb = 0x8C;
byte  addr_dig_t3_msb = 0x8D;
byte  addr_ctrl = 0xF4;
byte  addr_status = 0xF3;

s32_t t_fine;
void setup() {
  // 初始化串口通信
  Serial.begin(9600);

  // 配置从设备选择引脚
  pinMode(chipSelectPin, OUTPUT);

  // 初始化 SPI
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8);  // 设置时钟分频,可根据需要调整
  //===============获得BMP280参数==================//
  dig_t1_lsb = readRegister(addr_dig_t1_lsb,2);
  dig_t1_msb = readRegister(addr_dig_t1_msb,2);
  dig_t2_lsb = readRegister(addr_dig_t2_lsb,2);
  dig_t2_msb = readRegister(addr_dig_t2_msb,2);
  dig_t3_lsb = readRegister(addr_dig_t3_lsb,2);
  dig_t3_msb = readRegister(addr_dig_t3_msb,2);
  bmp_status = readRegister(addr_status,1);
  dig_t1 = ((u32_t)dig_t1_msb << 8) | dig_t1_lsb;
  dig_t2 = ((s32_t)dig_t2_msb << 8) | dig_t2_lsb;
  dig_t3 = ((s32_t)dig_t3_msb << 8) | dig_t3_lsb;
 //===============设置BMP280为连续模式==================//
  byte ctrl_status = readRegister(addr_ctrl,1);
  Serial.print("ctrl_status = ");
  Serial.print(ctrl_status, HEX);  
  Serial.print("\n");  
  writeRegister(addr_ctrl,1);
  
  ctrl_status = readRegister(addr_ctrl,1);
  Serial.print("ctrl_status = ");
  Serial.print(ctrl_status, HEX);  
  Serial.print("\n");  
 //===============打印BMP280校准参数==================//
  Serial.print("dig_t1 = ");
  Serial.print(dig_t1, HEX);
  Serial.print("\n");
  Serial.print("dig_t2 = ");
  Serial.print(dig_t2, HEX);
  Serial.print("\n");
  Serial.print("dig_t3 = ");
  Serial.print(dig_t3, HEX);  
  Serial.print("\n");  
  Serial.print("bmp_status = ");
  Serial.print(bmp_status, HEX);  
  Serial.print("\n");  
}

void loop() {
//===============获得全部数据==================//
  temp_xlsb = readRegister(addr_temp_xlsb,1);
  temp_lsb = readRegister(addr_temp_lsb,1);
  temp_msb = readRegister(addr_temp_msb,1);
  press_xlsb = readRegister(addr_press_xlsb,1);
  press_lsb = readRegister(addr_press_lsb,1);
  press_msb = readRegister(addr_press_msb,1);
  Serial.println(addr_temp_xlsb, DEC);
  Serial.println(addr_temp_lsb, DEC);
  Serial.println(addr_temp_msb, DEC);
//================计算温度值==================//  
  temp = (u32_t)temp_msb << 12 || (u32_t)temp_lsb << 4 || (u32_t)temp_xlsb >> 4;
  temp_comp = bmp280_compensate(temp);
//================串口打印温度值==================//  
  Serial.print("Composate Temprature is: ");
  Serial.println(temp_comp, DEC);
  Serial.print("\n");

  // 延时等待
  delay(2000);
}
unsigned int readRegister(byte thisRegister, int bytesToRead) {
  byte inByte = 0;           // incoming byte from the SPI
  unsigned int result = 0;   // result to return
  byte dataToSend = thisRegister;

  digitalWrite(chipSelectPin, LOW);
  // send the device the register you want to read:
  SPI.transfer(dataToSend);
  // send a value of 0 to read the first byte returned:
  result = SPI.transfer(0x00);
  // decrement the number of bytes left to read:
  bytesToRead--;
  // if you still have another byte to read:
  if (bytesToRead > 0) {
    // shift the first byte left, then get the second byte:
    result = result << 8;
    inByte = SPI.transfer(0x00);
    // combine the byte you just got with the previous one:
    result = result | inByte;
    // decrement the number of bytes left to read:
    bytesToRead--;
  }
  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
  // return the result:
  return (result);
}
void writeRegister(byte thisRegister, byte thisValue) {
  
  byte dataToSend = thisRegister;

  // take the chip select low to select the device:
  digitalWrite(chipSelectPin, LOW);

  SPI.transfer(dataToSend); //Send register location
  SPI.transfer(thisValue);  //Send value to record into register
  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
}

s32_t bmp280_compensate(s32_t adc_t)
{
  s32_t var1,var2,t;
  var1 = ((((adc_t>>3) - ((s32_t)dig_t1<<1))) * ((s32_t)dig_t2)) >>11;
  var2 = (((((adc_t>>4) - ((s32_t)dig_t1)) * ((adc_t>>4) - ((s32_t)dig_t1)))>>12) * ((s32_t)dig_t3)) >>14;
  t_fine = var1 + var2;
  t = (t_fine *5 +128) >>8;
  return t;
}

运行结果,实际不对。。

相关推荐
爱吃西瓜的小菜鸡12 分钟前
【C语言】判断回文
c语言·学习·算法
小A15934 分钟前
STM32完全学习——SPI接口的FLASH(DMA模式)
stm32·嵌入式硬件·学习
Rorsion40 分钟前
各种电机原理介绍
单片机·嵌入式硬件
岁岁岁平安1 小时前
spring学习(spring-DI(字符串或对象引用注入、集合注入)(XML配置))
java·学习·spring·依赖注入·集合注入·基本数据类型注入·引用数据类型注入
武昌库里写JAVA1 小时前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
qq_589568101 小时前
数据可视化echarts学习笔记
学习·信息可视化·echarts
兔C2 小时前
微信小程序的轮播图学习报告
学习·微信小程序·小程序
海海不掉头发2 小时前
苍穹外卖-day05redis 缓存的学习
学习·缓存
小木_.3 小时前
【Python 图片下载器】一款专门为爬虫制作的图片下载器,多线程下载,速度快,支持续传/图片缩放/图片压缩/图片转换
爬虫·python·学习·分享·批量下载·图片下载器
一棵开花的树,枝芽无限靠近你3 小时前
【PPTist】组件结构设计、主题切换
前端·笔记·学习·编辑器