STM32配置读取BMP280气压传感器数据

这篇具有很好参考价值的文章主要介绍了STM32配置读取BMP280气压传感器数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

STM32配置读取BMP280气压传感器数据

BMP280是在BMP180基础上增强的绝对气压传感器,在飞控领域的高度识别方面应用也比较多。
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
BMP280和BMP180的区别:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE

市面上也有一些模块:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
这里介绍STM32芯片和BMP280的连接和数据读取。

电路连接

BMP280和STM32的供电范围一致,可以在1.8V, 2.5V和3.3V多个供电电压点直接连接。

BMP280和STM32可以通过SPI或者I2C总线实现访问连接,I2C接口连接管脚少,这里采用I2C接口实现连接。

这里采用GPIO模拟I2C协议的方式,所以随意找2个管脚作为SCL和SDA。
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
用I2C总线连接时,BMP280的SDO管脚的电平状态用作I2C地址低位的选择。

运行过程

运行过程包括测试参数的配置选择:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
也包括循环运行过程的节奏控制:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
这里以STM32F401CCU6和STM32CUBEIDE开发环境为例,实现BMP280的访问读取数据,采用USB虚拟串口或普通串口方式打印输出。

初始化主要配置的寄存器0xF4 "ctrl_meas"的定义如下:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
过采样率oversampling会对采样分辨率和噪声产生影响:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
不同场景有推荐配置:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE

初始化主要配置的寄存器0xF5 "config"的定义如下:

bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
测量时间和上面的测量间隔,就构成了数据输出率:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
滤波参数和推荐:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
按照数据手册表达方式,应该是
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE

bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE

STM32工程配置

首先建立基本工程并配置时钟系统:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE

配置USB虚拟串口:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
配置USART1作为通讯串口:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE
配置两个管脚作为与BMP280的通讯管脚:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE

保存并生成初始工程代码:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE

STM32工程代码

I2C模拟时序用到的微秒延时函数,参考: STM32 HAL us delay(微秒延时)的指令延时实现方式及优化
USB虚拟串口的实现,参考: STM32 USB VCOM和HID的区别,配置及Echo功能实现(HAL)
STM32串口打印的实现,参考: STM32 UART串口printf函数应用及浮点打印代码空间节省 (HAL)
采用减少代码编译size的方式,参考: STM32 region `FLASH‘ overflowed by xxx bytes 问题解决

代码逻辑识别USB虚拟串口是否连接,如果连接,则通过USB虚拟串口打印输出,否则通过普通串口打印输出。
上电或重启后,STM32对BMP280进行初始化,如果失败,则打印输出报错信息,如果成功,则循环进行检测和输出压力,高度和温度值。

建立BMP280.h放置一些寄存器访问地址参数:

#ifndef __BMP280_H
#define __BMP280_H

#include "main.h"
#include "math.h"
#include "string.h"
#include "stdio.h"
/*
 *  BMP280 register address
 */
#define BMP280_REGISTER_DIG_T1      0x88
#define BMP280_REGISTER_DIG_T2      0x8A
#define BMP280_REGISTER_DIG_T3      0x8C

#define BMP280_REGISTER_DIG_P1      0x8E
#define BMP280_REGISTER_DIG_P2      0x90
#define BMP280_REGISTER_DIG_P3      0x92
#define BMP280_REGISTER_DIG_P4      0x94
#define BMP280_REGISTER_DIG_P5      0x96
#define BMP280_REGISTER_DIG_P6      0x98
#define BMP280_REGISTER_DIG_P7      0x9A
#define BMP280_REGISTER_DIG_P8      0x9C
#define BMP280_REGISTER_DIG_P9      0x9E

#define BMP280_REGISTER_CHIPID      0xD0
#define BMP280_REGISTER_VERSION     0xD1
#define BMP280_REGISTER_SOFTRESET   0xE0
#define BMP280_REGISTER_STATUS      0xF3
#define BMP280_REGISTER_CONTROL     0xF4
#define BMP280_REGISTER_CONFIG      0xF5

#define BMP280_TEMP_XLSB_REG        0xFC	    /*Temperature XLSB Register */
#define BMP280_TEMP_LSB_REG         0xFB        /*Temperature LSB Register  */
#define BMP280_TEMP_MSB_REG         0xFA        /*Temperature LSB Register  */
#define BMP280_PRESS_XLSB_REG       0xF9		/*Pressure XLSB  Register   */
#define BMP280_PRESS_LSB_REG        0xF8		/*Pressure LSB Register     */
#define BMP280_PRESS_MSB_REG        0xF7		/*Pressure MSB Register     */

/*calibration parameters */
#define BMP280_DIG_T1_LSB_REG                0x88
#define BMP280_DIG_T1_MSB_REG                0x89
#define BMP280_DIG_T2_LSB_REG                0x8A
#define BMP280_DIG_T2_MSB_REG                0x8B
#define BMP280_DIG_T3_LSB_REG                0x8C
#define BMP280_DIG_T3_MSB_REG                0x8D
#define BMP280_DIG_P1_LSB_REG                0x8E
#define BMP280_DIG_P1_MSB_REG                0x8F
#define BMP280_DIG_P2_LSB_REG                0x90
#define BMP280_DIG_P2_MSB_REG                0x91
#define BMP280_DIG_P3_LSB_REG                0x92
#define BMP280_DIG_P3_MSB_REG                0x93
#define BMP280_DIG_P4_LSB_REG                0x94
#define BMP280_DIG_P4_MSB_REG                0x95
#define BMP280_DIG_P5_LSB_REG                0x96
#define BMP280_DIG_P5_MSB_REG                0x97
#define BMP280_DIG_P6_LSB_REG                0x98
#define BMP280_DIG_P6_MSB_REG                0x99
#define BMP280_DIG_P7_LSB_REG                0x9A
#define BMP280_DIG_P7_MSB_REG                0x9B
#define BMP280_DIG_P8_LSB_REG                0x9C
#define BMP280_DIG_P8_MSB_REG                0x9D
#define BMP280_DIG_P9_LSB_REG                0x9E
#define BMP280_DIG_P9_MSB_REG                0x9F

typedef struct {
	uint16_t T1; 		/*<calibration T1 data*/
	int16_t T2;  	 	/*<calibration T2 data*/
	int16_t T3;  		/*<calibration T3 data*/
	uint16_t P1;  	    /*<calibration P1 data*/
	int16_t P2;  		/*<calibration P2 data*/
	int16_t P3;  		/*<calibration P3 data*/
	int16_t P4;  		/*<calibration P4 data*/
	int16_t P5;  		/*<calibration P5 data*/
	int16_t P6;  		/*<calibration P6 data*/
	int16_t P7;  		/*<calibration P7 data*/
	int16_t P8;  		/*<calibration P8 data*/
	int16_t P9;			/*<calibration P9 data*/
	int32_t T_fine;	/*<calibration t_fine data*/
} BMP280_HandleTypeDef;

typedef struct
{
	uint8_t Index;
	int32_t AvgBuffer[8];
} BMP280_AvgTypeDef;

#define MSLP     101325          // Mean Sea Level Pressure = 1013.25 hPA (1hPa = 100Pa = 1mbar)
#define ALTITUDE_OFFSET          10000

void I2C_Init(void);
void BMP280_Init();
void BMP280_CalTemperatureAndPressureAndAltitude(int32_t *temperature, int32_t *pressure, int32_t *Altitude);


#endif /* __BMP280_H */

其中ALTITUDE_OFFSET用于设定用户高度偏差。

建立BMP280.c实现访问和计算函数:


#include "BMP280.h"

extern void PY_Delay_us_t(uint32_t Delay);
extern char console[128];

//BMP280 I2C access protocol
#define us_num 2

#define SCL_OUT_H HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET)
#define SCL_OUT_L HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET)
#define SDA_OUT_H HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_SET)
#define SDA_OUT_L HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET)
#define SDA_IN HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13)

void I2C_Init(void)
{

	SCL_OUT_H;
	SDA_OUT_H;
	PY_Delay_us_t(100000);

}

void I2C_Start(void)
{
	PY_Delay_us_t(us_num) ;
	SDA_OUT_H;
	SCL_OUT_H;
	PY_Delay_us_t(us_num/2) ;
	SDA_OUT_L;
	PY_Delay_us_t(us_num/2) ;
	SCL_OUT_L;
}

void I2C_Stop(void)
{
	SCL_OUT_L;
	PY_Delay_us_t(us_num) ;
	SDA_OUT_L;
	PY_Delay_us_t(us_num) ;
	SCL_OUT_H;
	PY_Delay_us_t(us_num) ;
	SDA_OUT_H;
	PY_Delay_us_t(us_num) ;
}

void I2C_Write_Ack(void)
{

    PY_Delay_us_t(us_num/2) ;
	SDA_OUT_L;
	PY_Delay_us_t(us_num/2) ;
	SCL_OUT_H;
	PY_Delay_us_t(us_num) ;
	SCL_OUT_L;
	SDA_OUT_H;

}

uint8_t I2C_Read_Ack(void)
{
	uint8_t status=0;

	SCL_OUT_L;
	PY_Delay_us_t(us_num/2) ;
	SDA_OUT_H;
	PY_Delay_us_t(us_num/2) ;
	status = SDA_IN;
	SCL_OUT_H;
	PY_Delay_us_t(us_num) ;
	SCL_OUT_L;
	SDA_OUT_L;

	return status;

}


void I2C_Send_Byte(uint8_t txd){


    for(uint8_t i=0;i<8;i++)
    {
    	PY_Delay_us_t(us_num/2) ;
        if((txd&0x80)>>7) SDA_OUT_H;
        else SDA_OUT_L;
        txd<<=1;
        PY_Delay_us_t(us_num/2) ;
        SCL_OUT_H;
        PY_Delay_us_t(us_num) ;
		SCL_OUT_L;
    }

    SDA_OUT_L;
}

uint8_t I2C_Read_Byte(unsigned char rdack)
{
	uint8_t rxd=0;


    for(uint8_t i=0;i<8;i++ )
	{
    	SCL_OUT_L;
    	PY_Delay_us_t(us_num/2) ;
    	SDA_OUT_H;
    	PY_Delay_us_t(us_num/2) ;
    	SCL_OUT_H;
        rxd<<=1;
        if(SDA_IN) rxd++;
        PY_Delay_us_t(us_num) ;
    }

    SCL_OUT_L;
    SDA_OUT_H;

    if (rdack) I2C_Write_Ack();

    return rxd;
}


#define BMP280_I2C_ADDR_SEL 1

BMP280_HandleTypeDef bmp280;
#define dig_T1 bmp280.T1
#define dig_T2 bmp280.T2
#define dig_T3 bmp280.T3
#define dig_P1 bmp280.P1
#define dig_P2 bmp280.P2
#define dig_P3 bmp280.P3
#define dig_P4 bmp280.P4
#define dig_P5 bmp280.P5
#define dig_P6 bmp280.P6
#define dig_P7 bmp280.P7
#define dig_P8 bmp280.P8
#define dig_P9 bmp280.P9
#define t_fine bmp280.T_fine

int32_t gs32Pressure0 = MSLP;

void BMP280_WriteReg(uint8_t WrAddr, uint8_t data)
{
	  uint8_t daddr; //device address (0x1e<<1)

	  if(BMP280_I2C_ADDR_SEL==0) daddr = 0xec; //device address for SDO low status (0x76<<1)
	  else daddr = 0xee; //device address for SDO high status (0x77<<1)

	  I2C_Start();
	  I2C_Send_Byte(daddr);
	  I2C_Read_Ack();
  	  I2C_Send_Byte(WrAddr);
  	  I2C_Read_Ack();
  	  I2C_Send_Byte(data);
  	  I2C_Read_Ack();
  	  I2C_Stop();

}

uint8_t BMP280_ReadReg(uint8_t RdAddr)
{

	  uint8_t RegValue = 0;
	  uint8_t daddr;

	  if(BMP280_I2C_ADDR_SEL==0) daddr = 0xec; 
	  else daddr = 0xee; //device address for SDO high status (0x77<<1)

	  I2C_Start();
	  I2C_Send_Byte(daddr);
	  I2C_Read_Ack();
  	  I2C_Send_Byte(RdAddr);
  	  I2C_Read_Ack();

  	  I2C_Start();
	  I2C_Send_Byte(daddr+1);
	  I2C_Read_Ack();
	  RegValue=I2C_Read_Byte(0);
  	  I2C_Stop();

	  return RegValue;
}

/* Returns temperature in DegC, double precision. Output value of "1.23"equals 51.23 DegC. */
double BMP280_Compensate_Temperature(int32_t adc_T)
{
	double var1, var2, temperature;
	var1 = (((double) adc_T) / 16384.0 - ((double) dig_T1) / 1024.0) * ((double) dig_T2);
	var2 = ((((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0)  * (((double) adc_T) / 131072.0
					- ((double) dig_T1) / 8192.0)) * ((double) dig_T3);
	t_fine = (int32_t) (var1 + var2);
	temperature = (var1 + var2) / 5120.0;

	return temperature;
}


/* Returns pressure in Pa as double. Output value of "6386.2"equals 96386.2 Pa = 963.862 hPa */
double BMP280_Compensate_Pressure(int32_t adc_P)
{
	double var1, var2, pressure;

	var1 = ((double)t_fine / 2.0) - 64000.0;
	var2 = var1 * var1 * ((double) dig_P6) / 32768.0;
	var2 = var2 + var1 * ((double) dig_P5) * 2.0;
	var2 = (var2 / 4.0) + (((double) dig_P4) * 65536.0);
	var1 = (((double) dig_P3) * var1 * var1 / 524288.0  + ((double) dig_P2) * var1) / 524288.0;
	var1 = (1.0 + var1 / 32768.0) * ((double) dig_P1);

	if (var1 == 0.0) {
		return 0; // avoid exception caused by division by zero
	}

	pressure = 1048576.0 - (double) adc_P;
	pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;
	var1 = ((double) dig_P9) * pressure * pressure / 2147483648.0;
	var2 = pressure * ((double) dig_P8) / 32768.0;
	pressure = pressure + (var1 + var2 + ((double) dig_P7)) / 16.0;

	return pressure;
}

double BMP280_Get_Pressure(void)
{
	uint8_t lsb, msb, xlsb;
	int32_t adc_P;

	xlsb = BMP280_ReadReg(BMP280_PRESS_XLSB_REG);
	lsb = BMP280_ReadReg(BMP280_PRESS_LSB_REG);
	msb = BMP280_ReadReg(BMP280_PRESS_MSB_REG);
	adc_P = (msb << 12) | (lsb << 4) | (xlsb >> 4);
	//adc_P = 51988;
	return BMP280_Compensate_Pressure(adc_P);
}

void BMP280_Get_Temperature_And_Pressure(double *temperature, double *pressure)
{
	uint8_t lsb, msb, xlsb;
	int32_t adc_P,adc_T;

	xlsb = BMP280_ReadReg(BMP280_TEMP_XLSB_REG);
	lsb = BMP280_ReadReg(BMP280_TEMP_LSB_REG);
	msb = BMP280_ReadReg(BMP280_TEMP_MSB_REG);
	adc_T = (msb << 12) | (lsb << 4) | (xlsb >> 4);
	//adc_T = 415148;
	* temperature = BMP280_Compensate_Temperature(adc_T);

	xlsb = BMP280_ReadReg(BMP280_PRESS_XLSB_REG);
	lsb = BMP280_ReadReg(BMP280_PRESS_LSB_REG);
	msb = BMP280_ReadReg(BMP280_PRESS_MSB_REG);
	adc_P = (msb << 12) | (lsb << 4) | (xlsb >> 4);
	//adc_P = 51988;
	* pressure = BMP280_Compensate_Pressure(adc_P);
}

#define BMP280_AVG_TIMES 8 //maximum: 8
void BMP280_CalAvgValue(uint8_t *pIndex, int32_t *pAvgBuffer, int32_t InVal, int32_t *pOutVal)
{
	uint8_t i;
	static uint8_t status = 0;

  	*(pAvgBuffer + ((*pIndex) ++)) = InVal;
  	*pIndex %= BMP280_AVG_TIMES;

  	if(status<=24) //skip average computation before getting pre-defined data times (24 times)
  	{
  		*pOutVal = InVal;
  		status++;
  	}
  	else //compute average value
  	{
  	  	*pOutVal = 0;
  		for(i = 0; i < BMP280_AVG_TIMES; i ++)
  	  	{
  	    	*pOutVal += *(pAvgBuffer + i);
  	  	}
  	  	*pOutVal /= BMP280_AVG_TIMES;

  	}
}

void BMP280_CalculateAbsoluteAltitude(int32_t *pAltitude, int32_t PressureVal)
{
	*pAltitude = 4433000 * (1 - pow((PressureVal / (float)gs32Pressure0), 0.1903));
}

void BMP280_CalTemperatureAndPressureAndAltitude(int32_t *temperature, int32_t *pressure, int32_t *Altitude)
{
    double CurPressure, CurTemperature;
    int32_t CurAltitude;
    static BMP280_AvgTypeDef BMP280_Filter[3];

    BMP280_Get_Temperature_And_Pressure(&CurTemperature, &CurPressure);
    BMP280_CalAvgValue(&BMP280_Filter[0].Index, BMP280_Filter[0].AvgBuffer, (int32_t)(CurPressure), pressure);

    BMP280_CalculateAbsoluteAltitude(&CurAltitude, (*pressure));
    BMP280_CalAvgValue(&BMP280_Filter[1].Index, BMP280_Filter[1].AvgBuffer, CurAltitude, Altitude);
    BMP280_CalAvgValue(&BMP280_Filter[2].Index, BMP280_Filter[2].AvgBuffer, (int32_t)CurTemperature*10, temperature);

    (*Altitude) += ALTITUDE_OFFSET;

    return;
}


void BMP280_Read_Calibration(void)
{
	uint8_t lsb, msb;

	/* read the temperature calibration parameters */
	lsb = BMP280_ReadReg(BMP280_DIG_T1_LSB_REG);
    msb = BMP280_ReadReg(BMP280_DIG_T1_MSB_REG);
	dig_T1 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_T2_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_T2_MSB_REG);
	dig_T2 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_T3_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_T3_MSB_REG);
	dig_T3 = msb << 8 | lsb;

	/* read the pressure calibration parameters */
	lsb = BMP280_ReadReg(BMP280_DIG_P1_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P1_MSB_REG);
	dig_P1 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_P2_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P2_MSB_REG);
	dig_P2 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_P3_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P3_MSB_REG);
	dig_P3 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_P4_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P4_MSB_REG);
	dig_P4 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_P5_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P5_MSB_REG);
	dig_P5 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_P6_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P6_MSB_REG);
	dig_P6 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_P7_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P7_MSB_REG);
	dig_P7 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_P8_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P8_MSB_REG);
	dig_P8 = msb << 8 | lsb;
	lsb = BMP280_ReadReg(BMP280_DIG_P9_LSB_REG);
	msb = BMP280_ReadReg(BMP280_DIG_P9_MSB_REG);
	dig_P9 = msb << 8 | lsb;

}

void BMP280_Init()
{

    uint8_t u8ChipID, u8CtrlMod, u8Status;

    u8ChipID = BMP280_ReadReg(BMP280_REGISTER_CHIPID);
    u8CtrlMod = BMP280_ReadReg(BMP280_REGISTER_CONTROL);
    u8Status = BMP280_ReadReg(BMP280_REGISTER_STATUS);

    if(u8ChipID == 0x58)
    {
    	if(USB_CONN_STATUS())
    	{
    		sprintf(console, "\r\nBMP280 initial successful : ChipID [0x%x] CtrlMod [0x%x] Status [0x%x] \r\n", u8ChipID,u8CtrlMod,u8Status);
    		CDC_Transmit_FS((uint8_t*)console, strlen(console));
    	}
    	else
    	{
    		printf("\r\nBMP280 initial successful : ChipID [0x%x] CtrlMod [0x%x] Status [0x%x] \r\n", u8ChipID,u8CtrlMod,u8Status);
    	}

        BMP280_WriteReg(BMP280_REGISTER_CONTROL, 0xFF); //ctrl_meas register
        BMP280_WriteReg(BMP280_REGISTER_CONFIG, 0x0C);  //config register
        BMP280_Read_Calibration();

    }
    else
    {
    	if(USB_CONN_STATUS())
    	{
    		sprintf(console, "\r\nBMP280 initial failure : ChipID [0x%x] CtrlMod [0x%x] Status [0x%x] \r\n", u8ChipID,u8CtrlMod,u8Status);
    		CDC_Transmit_FS((uint8_t*)console, strlen(console));
    	}
    	else
    	{
    		printf("\r\nBMP280 initial failure : ChipID [0x%x] CtrlMod [0x%x] Status [0x%x] \r\n", u8ChipID,u8CtrlMod,u8Status);
    	}

    	  return BMP280_Init();
    }

}

main.c文件的实现功能测试代码,注意这里把BMP280管脚SDO拉到了高电平,所以定义为#define BMP280_I2C_ADDR_SEL 1:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "BMP280.h"
#include "usart.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{
  __IO uint32_t firstms, secondms;
  __IO uint32_t counter = 0;

  firstms = HAL_GetTick()+1;
  secondms = firstms+1;

  while(uwTick!=firstms) ;

  while(uwTick!=secondms) counter++;

  usDelayBase = ((float)counter)/1000;
}

void PY_Delay_us_t(uint32_t Delay)
{
  __IO uint32_t delayReg;
  __IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

void PY_usDelayOptimize(void)
{
  __IO uint32_t firstms, secondms;
  __IO float coe = 1.0;

  firstms = HAL_GetTick();
  PY_Delay_us_t(1000000) ;
  secondms = HAL_GetTick();

  coe = ((float)1000)/(secondms-firstms);
  usDelayBase = coe*usDelayBase;
}


void PY_Delay_us(uint32_t Delay)
{
  __IO uint32_t delayReg;

  __IO uint32_t msNum = Delay/1000;
  __IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);

  if(msNum>0) HAL_Delay(msNum);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int32_t PressureVal = 0, TemperatureVal = 0, AltitudeVal = 0;
char mychar[100];
char console[128];
/*
*Convert float to string type
*Written by Pegasus Yu in 2022
*stra: string address as mychar from char mychar[];
*float: float input like 12.345
*flen: fraction length as 3 for 12.345
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void py_f2s4printf(char * stra, float x, uint8_t flen)
{
	uint32_t base;
	int64_t dn;
	char mc[32];

	base = pow(10,flen);
	dn = x*base;
	sprintf(stra, "%d.", (int)(dn/base));
	dn = abs(dn);
	if(dn%base==0)
	{
		for(uint8_t j=1;j<=flen;j++)
		{
			stra = strcat(stra, "0");
		}
		return;
	}
	else
	{
		if(flen==1){
			sprintf(mc, "%d", (int)(dn%base));
			stra = strcat(stra, mc);
			return;
		}

		for(uint8_t j=1;j<flen;j++)
		{
			if((dn%base)<pow(10,j))
			{
				for(uint8_t k=1;k<=(flen-j);k++)
				{
					stra = strcat(stra, "0");
				}
				sprintf(mc, "%d", (int)(dn%base));
				stra = strcat(stra, mc);
				return;
			}
		}
		sprintf(mc, "%d", (int)(dn%base));
		stra = strcat(stra, mc);
		return;
	}
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  PY_usDelayTest();
  PY_usDelayOptimize();

  I2C_Init();
  BMP280_Init();
  PY_Delay_us_t(100000); //Waiting for the stability of BMP280 after initiation
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  BMP280_CalTemperatureAndPressureAndAltitude(&TemperatureVal, &PressureVal, &AltitudeVal);

	  	if(USB_CONN_STATUS())
	  	{
	  		sprintf(console, "\r\n\r\n--------------BMP280 TEST---------------");
	  		while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

	  		py_f2s4printf(mychar, (float)PressureVal/100, 2);
	  		sprintf(console, "\r\n Pressure: %s\r\n", mychar);
	  		while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

	  		py_f2s4printf(mychar, (float)AltitudeVal/100, 2);
	  		sprintf(console, "\r\n Altitude: %s\r\n", mychar);
	  		while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);

	  		py_f2s4printf(mychar, (float)TemperatureVal/10, 1);
	  		sprintf(console, "\r\n Temperature: %s\r\n", mychar);
	  		while( CDC_Transmit_FS((uint8_t*)console, strlen(console)) == USBD_BUSY ) PY_Delay_us_t(1);
	  	}
	  	else
	  	{
	  		printf("\r\n\r\n-----------------------------------------------");
	  		py_f2s4printf(mychar, (float)PressureVal/100, 2);
	  		printf("\r\n Pressure: %s\r\n", mychar);
	  		py_f2s4printf(mychar, (float)AltitudeVal/100, 2);
	  		sprintf(console, "\r\n Altitude: %s\r\n", mychar);
	  		printf("\r\n Altitude: %s\r\n", mychar);
	  		py_f2s4printf(mychar, (float)TemperatureVal/10, 1);
	  		sprintf(console, "\r\n Temperature: %s\r\n", mychar);
	  	}

	  PY_Delay_us_t(200000);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12|GPIO_PIN_13, GPIO_PIN_SET);

  /*Configure GPIO pins : PB12 PB13 */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

注意芯片内部已经配置选用了滤波功能,采用滤波功能后的输出速率不高,对于外部进行静态高度测量,可以再进行取多次平均的算法,如果是运动过程高度测量,则不必再采用平均算法。

测试输出

代码运行的测试输出:
bmp280 spi,STM32,stm32,BMP280,气压传感器,压力传感器,STM32CUBEIDE

工程代码下载

STM32F401CCU6配置读取BMP280工程下载

–End–文章来源地址https://www.toymoban.com/news/detail-540288.html

到了这里,关于STM32配置读取BMP280气压传感器数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • stm32读取DHT11温湿度传感器

    我们知道DHT11是单总线协议,只有一根数据线。 且内部有个上拉电路(下图)。那么数据线默认就是高电平那接下来就可以讲解主机如何和DHT11通讯的 读取DHT11的芯片手册,可以知道,DHT11一次完成的数据输出是40bit,高位先出。 格式:8bit湿度整数数据+8bit湿度小数数据 +8bi温

    2024年02月09日
    浏览(51)
  • ESP32设备驱动-DPS310气压传感器驱动

    Infineon 的 DPS310 传感器是一款高精度气压传感器,非常适合测量高度变化,精度高达 ±0.002 hPa(或 ±0.02 m)高精度模式和 ± 1 hPa 绝对精度。 这意味着您可以在设置海平面压力时以 1 = 米的精度知道您的绝对高度,并以高达 2 厘米的精度测量高度变化。 这使其成为适用于无人机

    2024年02月07日
    浏览(38)
  • STM32学习笔记———几种简单传感器的数据读取

    传感器正如计算机的眼睛。从广义上讲,传感器就是一种能感知外界信息,并将这些信息按照一定规律转换成可用的电信号或其他形式的输出信号的装置,达到对信息的存储,传输,控制的目的。本文着重分析如何通过单片机分析电信号时序图实现对传感器的控制与传感器采

    2023年04月23日
    浏览(42)
  • STM32—ADC详解入门(ADC读取烟雾传感器的值)

    目录 一、ADC是什么 二、ADC的性能指标 三、ADC特性 四、ADC通道 五、ADC转换顺序 六、ADC触发方式 七、ADC转化时间 八、ADC转化模式 九、实验(使用ADC读取烟雾传感器的值) 1、配置 2、代码         ADC是Analog-to-DigitalConverter的缩写。指模/数转换器或者模拟/数字转换器。是指

    2024年02月11日
    浏览(41)
  • ESP32设备驱动-BMP183温度传感器驱动

    BMP183 是 BMP085 的功能兼容后继产品,BMP085 是面向消费类应用的新一代高精度数字压力传感器。 BMP183 的超低功耗、低电压电子器件针对移动电话、PDA、GPS 导航设备和户外设备的使用进行了优化。 BMP183 在快速转换时的低海拔噪声仅为 0.25 米,可提供卓越的性能。 SPI 接口允许

    2024年02月06日
    浏览(54)
  • STM32——ADC读取光敏传感器控制LED灯,看门狗中断

    一、编写读取AD值的函数,之后判断AD值,进行相应操作,比如点灯。 二、用ADC读取光敏传感器AO口输出,并配置ADC通道看门狗监控这条通道,当光线太暗时打开LED灯。 之后会再介绍可编程RGB灯带WS2812B。 光敏传感器有两个输出口,一个是DO(Digital Output),一个是AO(Analog O

    2023年04月18日
    浏览(45)
  • STM32读取TCS3472颜色传感器读取RGB颜色和色温值和Lux

    1.3472能提供红,绿,蓝色(RGB)和清晰光感应值的数字输出 2.它通过 I2C协议通讯。 3.最好选择带led灯的版本,自带的led低电平能关闭。 4.这边VIN接5V电压输入,GND接GND,SCL接SCL(PF1)SDA接SDA(PF0),这边根据自己启动的IO口进行变换    5.我这边采用STM32Cube生成使用硬件I2C的方式

    2024年02月13日
    浏览(37)
  • STM32CubeMX 读取DS18B20温度传感器数据串口打印显示

    本文要做的所有工作标题基本都包括了,读取温度传感器的温度数值,再通过串口打印到串口助手; 好多博主大神的教程我按步骤做了之后总是出现程序不报错并且检测不到传感器的情况,后来找到原因并且修改后调试正常。 我用的是普中科技的实验板,主控芯片为STM3210

    2024年02月05日
    浏览(80)
  • 新手小白必看——基于STM32+ESP8266模块连接阿里云平台读取传感器数据(从0到1,包教包会)

    目录 前言             本次我们学习一下STM32F103驱动ESP8266去连接阿里云的实操过程,在次过程中我会详细讲解开发过程中遇到的问题以及面对问题的解决,希望大家在这篇博客里面也有所收获。 一、ESP8266WIFI模块的固件库刷新(MQTT) 1.固件库刷新 1.1 固件库烧录下载 1.2 固

    2024年04月26日
    浏览(42)
  • homeassistant配置MQTT集成以及传感器实体(STM32连接进入homeassistant)

    大家可以看作者的小破站教学视频(如果喜欢的话可以点个关注,给个三联!啊哈哈哈哈哈哈): 【homeassistant配置MQTT集成以及传感器实体(STM32连接进入homeassistant)】 最近homeassistan更新之后,传统的MQTT服务器配置是完全不适用的 目前(2023.4.16)的教程都没有找到特别适用

    2024年02月10日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包