基于STM32F10X的BMP280程序

简介: 基于STM32F10X的BMP280程序

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

硬件连接

BMP280 Pin STM32 Pin
SDA PB7
SCL PB6
VCC 3.3V
GND GND

软件部分

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
相关文章
|
NoSQL 数据安全/隐私保护 Android开发
Jlink使用技巧之读取STM32内部的程序
Jlink使用技巧之读取STM32内部的程序
2511 1
Jlink使用技巧之读取STM32内部的程序
STM32 ST-LINK Utility程序烧录方法
STM32 ST-LINK Utility程序烧录方法
2042 0
|
缓存 编译器 程序员
嵌入式开发环境Vscode开发STM32单片机程序
嵌入式开发环境Vscode开发STM32单片机程序
326 1
|
传感器 数据采集 监控
资料转发分享【毕业设计】单片机和stm32设计选题,proteues仿真、程序完整资料
资料转发分享【毕业设计】单片机和stm32设计选题,proteues仿真、程序完整资料 基于单片机寻迹巡线避障智能小车系统设计 基于单片机体温心率脉搏检测仪系统设计 基于单片机温湿度光照自动窗帘系统设计 基于单片机环境监测温湿度PM2.5系统设计 基于51单片机的波形发生器(四种波形) 基于单片机SO2 NO2 PM温湿度空气质量检测仪 基于51单片机冰箱温度控制器设计
1570 1
资料转发分享【毕业设计】单片机和stm32设计选题,proteues仿真、程序完整资料
|
内存技术 芯片
MDK st-link下载STM32程序出现Internal command error和Error:Flash download failed. Target DLL
MDK st-link下载STM32程序出现Internal command error和Error:Flash download failed. Target DLL   是因为目标板的芯片处于休眠的状态,在尝试连接目标板时候也会出现报错Internal command ...
3873 0
|
传感器 数据采集
STM32 BMP280模块 获取气压温度高度传感器数据
STM32 BMP280模块 获取气压温度高度传感器数据 TFT显示
423 0
|
程序员 芯片
STM32f103 CubeMX封装 led程序
可以看到 对于 103 板子,有 3 个 LED: 分别为 LED1 , LED2 , LED3 ;分别对应 io 端口 PB0 , PB1 ,PB5。 可以看到 LED 左边 接 3V 的电压,要想点亮 LED,只需在 右边的 io 端口输出 低电平 即可。 反之,要想 熄灭 LED,就在 右边的 io 端口输出 高电平 。 我这里的是 RGB灯,可以闪烁不同的颜色:红,绿,蓝。
268 0
|
存储 芯片 Windows
如何使用串口来给STM32下载程序
如何使用串口来给STM32下载程序
763 0
如何使用串口来给STM32下载程序
|
芯片
stm32程序下载遇到的问题
本文记录测试板子出现时出现的多个问题及解决方法
545 0
stm32程序下载遇到的问题
stm32实用技巧:JLINK接口定义和使用JTAG或SW下载程序
stm32实用技巧:JLINK接口定义和使用JTAG或SW下载程序
stm32实用技巧:JLINK接口定义和使用JTAG或SW下载程序