STM32通过I2C与BMP280通信
扫描二维码
随时随地手机看文章
BMP280气压传感器(Digital Pressure sensor)
测试流程图:
项目中使用的是I2C通信,BMP280的作为slave的地址要确认好,它的地址根据硬件电路SDO接什么脚来决定:
1.BMP280的测试启动流程(注意它的datasheet中的status寄存器):
(1)读取BMP280的id,其值等于0x58。
(2)把补偿寄存器的值都读出来。
(3)对BMP280进行reset。
(4)对BMP280的数据采集精度,模式。。。等进行配置。
(5)给点延时,等待数据采集完毕放入寄存器中。
(6)读取寄存器中的采集数据。
注意:BMP280对温度和气压的测量是通过一堆补偿数据然后根据自己的算法算出来的,要注意这些数据是什么类型的,刚开始我就因为粗心没看清楚,算出来的数据总是不正确。
下面把代码贴出来:
1.bmp280的驱动文件:
#include
#include"stm32f4xx_hal.h"
#include"stm32f4xx_hal_i2c.h"
#include
#definedig_T1bmp280->T1
#definedig_T2bmp280->T2
#definedig_T3bmp280->T3
#definedig_P1bmp280->P1
#definedig_P2bmp280->P2
#definedig_P3bmp280->P3
#definedig_P4bmp280->P4
#definedig_P5bmp280->P5
#definedig_P6bmp280->P6
#definedig_P7bmp280->P7
#definedig_P8bmp280->P8
#definedig_P9bmp280->P9
staticuint8_tbmp280_read_register(I2C_HandleTypeDefBmp280_I2cHandle,uint8_treg_addr)
{
uint8_treg_data;
while(HAL_I2C_Master_Transmit(&Bmp280_I2cHandle,BMP280_ADDRESS,®_addr,1,10000)!=HAL_OK){
if(HAL_I2C_GetError(&Bmp280_I2cHandle)!=HAL_I2C_ERROR_AF){
printf("Transmitslaveaddresserror!!!rn");
return-1;
}
}
while(HAL_I2C_Master_Receive(&Bmp280_I2cHandle,BMP280_ADDRESS,®_data,1,10000)!=HAL_OK){
if(HAL_I2C_GetError(&Bmp280_I2cHandle)!=HAL_I2C_ERROR_AF){
printf("Receiveslavedataerror!!!rn");
return-1;
}
}
returnreg_data;
}
staticvoidbmp280_write_register(I2C_HandleTypeDefBmp280_I2cHandle,uint8_treg_addr,uint8_treg_data)
{
uint8_ttx_data[2]={reg_addr,reg_data};
while(HAL_I2C_Master_Transmit(&Bmp280_I2cHandle,BMP280_ADDRESS,tx_data,2,10000)!=HAL_OK){
if(HAL_I2C_GetError(&Bmp280_I2cHandle)!=HAL_I2C_ERROR_AF){
printf("Transmitslaveaddresserror!!!rn");
}
}
}
/**
*在bmp280_init()函数里默认初始化t_standby为0.5ms,
*温度和气压的采样精度设为最低,
*滤波器系数设为最低,
*并且进入sleepmode。
*/
structbmp280*bmp280_init(I2C_HandleTypeDefI2cHandle)
{
uint8_tbmp280_id;
uint8_tlsb,msb;
uint8_tctrlmeas_reg,config_reg;
structbmp280*bmp280;
bmp280_id=bmp280_read_register(I2cHandle,BMP280_CHIPID_REG);
if(bmp280_id==0x58){
bmp280=malloc(sizeof(structbmp280));
bmp280->I2cHandle=I2cHandle;
bmp280->mode=BMP280_SLEEP_MODE;
bmp280->t_sb=BMP280_T_SB1;
bmp280->p_oversampling=BMP280_P_MODE_1;
bmp280->t_oversampling=BMP280_T_MODE_1;
bmp280->filter_coefficient=BMP280_FILTER_MODE_1;
}else{
printf("ReadBMP280iderror!rn");
returnNULL;
}
/*readthetemperaturecalibrationparameters*/
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_T1_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_T1_MSB_REG);
dig_T1=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_T2_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_T2_MSB_REG);
dig_T2=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_T3_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_T3_MSB_REG);
dig_T3=msb<<8|lsb;
/*readthepressurecalibrationparameters*/
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P1_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_P1_MSB_REG);
dig_P1=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P2_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_P2_MSB_REG);
dig_P2=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P3_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_P3_MSB_REG);
dig_P3=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P4_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_P4_MSB_REG);
dig_P4=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P5_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_P5_MSB_REG);
dig_P5=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P6_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_P6_MSB_REG);
dig_P6=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P7_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_P7_MSB_REG);
dig_P7=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P8_LSB_REG);
msb=bmp280_read_register(I2cHandle,BMP280_DIG_P8_MSB_REG);
dig_P8=msb<<8|lsb;
lsb=bmp280_read_register(I2cHandle,BMP280_DIG_P9_LSB_REG);