BMP280 arduino调试

2024-03-16 22:44
文章标签 调试 arduino bmp280

本文主要是介绍BMP280 arduino调试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

终于成功了。

#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_status = 0xF3;
byte  addr_ctrl = 0xF4;
byte  addr_timestandby = 0xF5;
byte  addr_id = 0xd0;
s32_t t_fine;
void setup() {// 初始化串口通信Serial.begin(115200);// 配置从设备选择引脚pinMode(chipSelectPin, OUTPUT);// 初始化 SPISPI.begin();SPI.setClockDivider(SPI_CLOCK_DIV8);  // 设置时钟分频,可根据需要调整//===============获得BMP280参数==================//dig_t1_lsb = readRegister(addr_dig_t1_lsb,1);dig_t1_msb = readRegister(addr_dig_t1_msb,1);dig_t2_lsb = readRegister(addr_dig_t2_lsb,1);dig_t2_msb = readRegister(addr_dig_t2_msb,1);dig_t3_lsb = readRegister(addr_dig_t3_lsb,1);dig_t3_msb = readRegister(addr_dig_t3_msb,1);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器件ID==================//byte device_id = readRegister(addr_id,1);Serial.print("device_id = ");Serial.println(device_id, HEX);   
//===============设置TIME_STANDBY采样时间==================//byte timestandby = readRegister(addr_timestandby,0x01);Serial.print("timestandby = ");Serial.println(timestandby, HEX); timestandby=timestandby |  (0x001 << 5) ;writeRegister(addr_timestandby,timestandby);
//===============设置BMP280为连续模式==================//byte ctrl_meas = readRegister(addr_ctrl,1);Serial.print("ctrl_meas = ");Serial.println(ctrl_meas, BIN);   writeRegister(addr_ctrl,0x03);//===============获得当前状态==================//ctrl_meas = readRegister(addr_ctrl,1);Serial.print("ctrl_meas = ");Serial.println(ctrl_meas, BIN);  //===============打印BMP280校准参数==================//Serial.print("dig_t1 = ");Serial.println(dig_t1, HEX);Serial.print("dig_t2 = ");Serial.println(dig_t2, HEX);Serial.print("dig_t3 = ");Serial.println(dig_t3, HEX);   Serial.print("bmp_status = ");Serial.println(bmp_status, BIN);  
}void loop() {
//  writeRegister(addr_ctrl,0x01);
//===============获得全部数据==================//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, HEX);Serial.println(addr_temp_lsb, HEX);Serial.println(addr_temp_msb, HEX);
//================计算温度值==================//  temp = (u32_t)temp_msb << 12 | (u32_t)temp_lsb << 4 | (u32_t)temp_xlsb >> 4;temp_comp = bmp280_compensate(temp);float temp_comp_float = temp_comp/100.00;
//================串口打印温度值==================//  Serial.print("Composate Temprature is: ");Serial.println(temp_comp, DEC);Serial.print(temp_comp_float);Serial.println("℃");// 延时等待delay(2000);
}
unsigned int readRegister(byte thisRegister, int bytesToRead) {byte inByte = 0;           // incoming byte from the SPIunsigned int result = 0;   // result to returnbyte 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) {// take the chip select low to select the device:digitalWrite(chipSelectPin, LOW);SPI.transfer(thisRegister); //Send register locationSPI.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;
}

这篇关于BMP280 arduino调试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/816965

相关文章

IDEA如何实现远程断点调试jar包

《IDEA如何实现远程断点调试jar包》:本文主要介绍IDEA如何实现远程断点调试jar包的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录问题步骤总结问题以jar包的形式运行Spring Boot项目时报错,但是在IDEA开发环境javascript下编译

Python MCPInspector调试思路详解

《PythonMCPInspector调试思路详解》:本文主要介绍PythonMCPInspector调试思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录python-MCPInspector调试1-核心知识点2-思路整理1-核心思路2-核心代码3-参考网址

Linux系统调试之ltrace工具使用与调试过程

《Linux系统调试之ltrace工具使用与调试过程》:本文主要介绍Linux系统调试之ltrace工具使用与调试过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、ltrace 定义与作用二、ltrace 工作原理1. 劫持进程的 PLT/GOT 表2. 重定

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

C++中实现调试日志输出

《C++中实现调试日志输出》在C++编程中,调试日志对于定位问题和优化代码至关重要,本文将介绍几种常用的调试日志输出方法,并教你如何在日志中添加时间戳,希望对大家有所帮助... 目录1. 使用 #ifdef _DEBUG 宏2. 加入时间戳:精确到毫秒3.Windows 和 MFC 中的调试日志方法MFC

ASIO网络调试助手之一:简介

多年前,写过几篇《Boost.Asio C++网络编程》的学习文章,一直没机会实践。最近项目中用到了Asio,于是抽空写了个网络调试助手。 开发环境: Win10 Qt5.12.6 + Asio(standalone) + spdlog 支持协议: UDP + TCP Client + TCP Server 独立的Asio(http://www.think-async.com)只包含了头文件,不依