STM32配置读取BMP280气压传感器数据
BMP280是在BMP180基础上增强的绝对气压传感器,在飞控领域的高度识别方面应用也比较多。
BMP280和BMP180的区别:
市面上也有一些模块:
这里介绍STM32芯片和BMP280的连接和数据读取。
电路连接
BMP280和STM32的供电范围一致,可以在1.8V, 2.5V和3.3V多个供电电压点直接连接。
BMP280和STM32可以通过SPI或者I2C总线实现访问连接,I2C接口连接管脚少,这里采用I2C接口实现连接。
这里采用GPIO模拟I2C协议的方式,所以随意找2个管脚作为SCL和SDA。
用I2C总线连接时,BMP280的SDO管脚的电平状态用作I2C地址低位的选择。
运行过程
运行过程包括测试参数的配置选择:
也包括循环运行过程的节奏控制:
这里以STM32F401CCU6和STM32CUBEIDE开发环境为例,实现BMP280的访问读取数据,采用USB虚拟串口或普通串口方式打印输出。
初始化主要配置的寄存器0xF4 "ctrl_meas"的定义如下:
过采样率oversampling会对采样分辨率和噪声产生影响:
不同场景有推荐配置:
初始化主要配置的寄存器0xF5 "config"的定义如下:
测量时间和上面的测量间隔,就构成了数据输出率:
滤波参数和推荐:
按照数据手册表达方式,应该是:
STM32工程配置
首先建立基本工程并配置时钟系统:
配置USB虚拟串口:
配置USART1作为通讯串口:
配置两个管脚作为与BMP280的通讯管脚:
保存并生成初始工程代码:
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 */
注意芯片内部已经配置选用了滤波功能,采用滤波功能后的输出速率不高,对于外部进行静态高度测量,可以再进行取多次平均的算法,如果是运动过程高度测量,则不必再采用平均算法。
测试输出
代码运行的测试输出:
工程代码下载
STM32F401CCU6配置读取BMP280工程下载文章来源:https://www.toymoban.com/news/detail-540288.html
–End–文章来源地址https://www.toymoban.com/news/detail-540288.html
到了这里,关于STM32配置读取BMP280气压传感器数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!