当前位置: 首页 > news >正文

基于STM32F10X的BMP280程序

基于STM32F10X的BMP280完整程序示例,采用IIC通信方式,可以实现温度、气压和高度的测试。

硬件连接

BMP280 PinSTM32 Pin
SDAPB7
SCLPB6
VCC3.3V
GNDGND

软件部分

1. 初始化I2C GPIO引脚
void MX_GPIO_Init(void) {__HAL_RCC_GPIOB_CLK_ENABLE(); // 启用 GPIOB 时钟GPIO_InitTypeDef GPIO_InitStruct = {0};// 配置 SCL (PB6)GPIO_InitStruct.Pin = GPIO_PIN_6;GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; // 复用开漏模式GPIO_InitStruct.Pull = GPIO_PULLUP;     // 上拉电阻GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; // 设置为 I2C1 复用功能HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);// 配置 SDA (PB7)GPIO_InitStruct.Pin = GPIO_PIN_7;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
2. BMP280驱动代码

bmp280.h

#ifndef __BMP280_H
#define __BMP280_H#include "stm32f10x.h"
#include "sys.h"
#include "stdbool.h"#define SCL_GPIO_PORT GPIOB              /* GPIO端口 */
#define SCL_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO端口时钟 */
#define SCL_GPIO_PIN GPIO_Pin_6          /* 连接到SCL时钟线的GPIO */#define SDA_GPIO_PORT GPIOB              /* GPIO端口 */
#define SDA_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO端口时钟 */
#define SDA_GPIO_PIN GPIO_Pin_7          /* 连接到SDA数据线的GPIO *///IO操作函数
#define IIC_SCL    PBout(6)                                                   //SCL
#define IIC_SDA    PBout(7)                                                   //SDA
#define READ_SDA   PBin(7)                                                    //输入SDA// BME280的I2C地址,通常为0xEC
#define BME280_ADDRESS 0XEC
#define BME280_ADDR						(0x76)
#define BME280_DEFAULT_CHIP_ID			(0x60)#define BME280_CHIP_ID					(0xD0)                                 /* Chip ID Register */
#define BME280_RST_REG					(0xE0)                                 /* Softreset Register */
#define BME280_CTRL_HUM                 (0xF2)                                 /* Ctrl Humidity Register */
#define BME280_STAT_REG					(0xF3)                                 /* Status Register */
#define BME280_CTRL_MEAS_REG			(0xF4)                                 /* Ctrl Measure Register */
#define BME280_CONFIG_REG				(0xF5)                                 /* Configuration Register */
#define BME280_PRESSURE_MSB_REG			(0xF7)                                 /* Pressure MSB Register */
#define BME280_PRESSURE_LSB_REG			(0xF8)                                 /* Pressure LSB Register */
#define BME280_PRESSURE_XLSB_REG		(0xF9)                                 /* Pressure XLSB Register */
#define BME280_TEMPERATURE_MSB_REG		(0xFA)                                 /* Temperature MSB Reg */
#define BME280_TEMPERATURE_LSB_REG		(0xFB)                                 /* Temperature MSB Reg */
#define BME280_TEMPERATURE_XLSB_REG		(0xFC)                                 /* Temperature XLSB Reg */
#define BME280_HUMIDITY_MSB_REG			(0xFD)                                 /* Humidity MSB Reg */
#define BME280_HUMIDITY_LSB_REG		    (0xFE)                                 /* Humidity LSB Reg */#define BME280_SLEEP_MODE				(0x00)
#define BME280_FORCED_MODE				(0x01)
#define BME280_NORMAL_MODE				(0x03)#define BME280_TEMPERATURE_CALIB_DIG_T1_LSB_REG             (0x88)
#define BME280_PRESSURE_TEMPERATURE_CALIB_DATA_LENGTH       (32)
#define BME280_DATA_FRAME_SIZE			(8)#define BME280_OVERSAMP_SKIPPED			(0x00)
#define BME280_OVERSAMP_1X				(0x01)
#define BME280_OVERSAMP_2X				(0x02)
#define BME280_OVERSAMP_4X				(0x03)
#define BME280_OVERSAMP_8X				(0x04)
#define BME280_OVERSAMP_16X				(0x05)#define CONST_PF 0.1902630958	                                               //(1/5.25588f) Pressure factor
#define FIX_TEMP 25				                                               // Fixed Temperature. ASL is a function of pressure and temperature, but as the temperature changes so much (blow a little towards the flie and watch it drop 5 degrees) it corrupts the ASL estimates.typedef struct
{u16 dig_T1;                                                                /* calibration T1 data */s16 dig_T2;                                                                /* calibration T2 data */s16 dig_T3;                                                                /* calibration T3 data */u16 dig_P1;                                                                /* calibration P1 data */s16 dig_P2;                                                                /* calibration P2 data */s16 dig_P3;                                                                /* calibration P3 data */s16 dig_P4;                                                                /* calibration P4 data */s16 dig_P5;                                                                /* calibration P5 data */s16 dig_P6;                                                                /* calibration P6 data */s16 dig_P7;                                                                /* calibration P7 data */s16 dig_P8;                                                                /* calibration P8 data */s16 dig_P9;                                                                /* calibration P9 data */u8  dig_H1;                                                                /* calibration H1 data */s16 dig_H2;                                                                /* calibration H2 data */u8  dig_H3;                                  							   /* calibration H3 data */s16 dig_H4;                                                                /* calibration H4 data */s16 dig_H5;                                                                /* calibration H5 data */u8  dig_H6;                                                                /* calibration H6 data */s32 t_fine;                                                                /* calibration t_fine data */
} bme280Calib;// BMP280
void bme280Init(void);
void bme280GetData(float* pressure,float* temperature,float* asl);//IIC所有操作函数
void IIC_Init(void);                                                           //初始化IIC的IO口
void IIC_Start(void);				                                           //发送IIC开始信号
void IIC_Stop(void);	  			                                           //发送IIC停止信号
void IIC_Send_Byte(u8 txd);			                                           //IIC发送一个字节
u8 IIC_Read_Byte(unsigned char ack);                                           //IIC读取一个字节
u8 IIC_Wait_Ack(void); 				                                           //IIC等待ACK信号
void IIC_Ack(void);					                                           //IIC发送ACK信号
void IIC_NAck(void);				                                           //IIC不发送ACK信号void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data);
u8 IIC_Read_One_Byte(u8 daddr,u8 addr);u8 iicDevReadByte(u8 devaddr,u8 addr);	                                       /*读一字节*/
void iicDevWriteByte(u8 devaddr,u8 addr,u8 data);	                           /*写一字节*/
void iicDevRead(u8 devaddr,u8 addr,u8 len,u8 *rbuf);                           /*连续读取多个字节*/
void iicDevWrite(u8 devaddr,u8 addr,u8 len,u8 *wbuf);                          /*连续写入多个字节*/
void iicDevReadCal(u8 devaddr,u8 addr,u8 len,bme280Calib *bme280Ctype);
void iicDevReadCal1(u8 devaddr,u8 addr,u8 len,u8 *rbuf);#endif // __BMP280_H
3. 主程序main.c
#include "stm32f10x.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "usart.h"
#include "bmp280.h"int main(void)
{float bmp280_temp;float bmp280_press;float bmp280_humi;float high;delay_init();NVIC_PriorityGroupConfig(NVIC
http://www.xdnf.cn/news/711739.html

相关文章:

  • 滚珠导轨:电子制造“纳米级”精度的运动基石
  • 如何用命令行将 PDF 表格转换为 HTML 表格
  • 责任链模式:构建灵活可扩展的请求处理体系(Java 实现详解)
  • ZYNQ移植FreeRTOS和固化和openAMP双核
  • 设备制造行业项目管理难点解析,如何有效解决?
  • 塔能科技:为多行业工厂量身定制精准节能方案
  • Kotlin 活动事件通讯跳转深度讲解
  • 职业本科院校无人机专业人才培养解决方案
  • KeePass安装与KeePass设置中文教程
  • springboot多模块父pom打包正常,单模块报错
  • clickhouse如何查看操作记录,从日志来查看写入是否成功
  • 湖北理元理律师事务所债务优化实践:在还款与生活间寻找平衡支点
  • [NOIP 2001 普及组] 数的计算 Java
  • 高防IP能抗住500G攻击吗?
  • PostgreSQL的聚集函数
  • Nest全栈到失业(三):半小时图书管理系统-User
  • Ubuntu 22.04 上安装 PostgreSQL(使用官方 APT 源)
  • CRMEB 单商户Java版 v2.3公测版发布,欢迎体验!
  • 收集飞花令碎片——C语言(数组+函数)
  • 酷派Cool20/20S/30/40手机安装Play商店-谷歌三件套-GMS方法
  • 小程序为什么要安装SSL安全证书
  • LeetCode 55 45:跳跃游戏与跳跃游戏 II - 贪心算法详解
  • 前端开发中 <> 符号解析问题全解:React、Vue 与 UniApp 场景分析与解决方案
  • 题目 3298: 蓝桥杯2024年第十五届决赛真题-兔子集结
  • WPF log4net用法
  • STM32 AD单通道与多通道实战指南
  • 【QT】理解QT的“元对象系统”
  • 【Tips】关于PCI和PCIe的配置空间差异和io/memory io读写
  • 【CF】Day69——⭐Codeforces Round 897 (Div. 2) D (图论 | 思维 | DFS | 环)
  • Redis--基础知识点--28--慢查询相关