转
Bosch已经将BMP280的官方驱动文件分享到Github上:
https://github.com/BoschSensortec/BMP280_driver
为了更好的理解,我直接使用了官方的C语言写的驱动文件,当然也可以参照官方驱动,用C++编写专门适用于mbed的驱动。同时,为了支持C语言编译或者说混合编译,需要在驱动头文件bmp280.h中添加几句话:
#ifdef _cplusplus
extern "C"{
#endif
... ...
#ifdef _cplusplus
}
#endif
下面是我的主函数main.cpp的代码:
#include "mbed.h"
#include "bmp280.h"
/************** SPI control mask******/
#define SPI_READ 0x80
#define SPI_WRITE 0x7F
/********************SPI func
ti on declarations***********************/
SPI spi_bmp(SPI_MOSI, SPI_MISO, SPI_SCK);
DigitalOut CS(SPI_CS);
s8 BMP280_SPI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
s8 BMP280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/********************End of SPI function declarations***********************/
void Bus_routine(void);
void BMP280_delay_msek(u32 msek);
struct bmp280_t bmp280;
Serial pc_my(SERIAL_TX, SERIAL_RX);
void Bus_routine(void) {
bmp280.bus_write = BMP280_SPI_bus_write;
bmp280.bus_read = BMP280_SPI_bus_read;
bmp280.delay_msec = BMP280_delay_msek;
}
/* Brief : The delay routine
* param : delay in ms */
void BMP280_delay_msek(u32 msek)
{
/*Here you can write your own delay routine*/
}
s8 BMP280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
int iError = BMP280_INIT_VALUE;
CS=0;
iError =spi_bmp.write(reg_addr | SPI_READ);
for (u8 i = 0; i < cnt; i++)
{
reg_data = spi_bmp.write(0x00);
}
CS=1;
return (s8)iError;
}
s8 BMP280_SPI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
int iError = BMP280_INIT_VALUE;
CS=0;
u8 *array = ®_addr;
for(u8 i = 0; i < cnt; i++) {
iError +=spi_bmp.write(array & SPI_WRITE);
spi_bmp.write(reg_data);
}
CS=1;
return (s8)iError;
}
int main()
{
//首先初始化数据
/* Assign the standby time*/
u8 v_standby_time_u8 = BMP280_INIT_VALUE;
/* For Individual data read APIs*/
/* read uncompensated temperature*/
s32 v_data_uncomp_tem_s32 = BMP280_INIT_VALUE;
/* read uncompensated pressure*/
s32 v_data_uncomp_pres_s32 = BMP280_INIT_VALUE;
/* read real temperature*/
double actual_temp = BMP280_INIT_VALUE;
/* read real pressure*/
double actual_press = BMP280_INIT_VALUE;
/* Return status*/
s32 com_rslt = ERROR;
/* Set Baud */
pc_my.baud(115200);
//设置routine,将BMP280_SPI_bus_write/read传递给传感器读写
Bus_routine();
com_rslt=bmp280_init(&bmp280);
if(com_rslt==0)
pc_my.printf("initialization succeed!rn");
else
pc_my.printf("initialization failure!rn");
/*设置工作模式为Normal mode*/
com_rslt+= bmp280_set_power_mode(BMP280_NORMAL_MODE);
if(com_rslt==0)
pc_my.printf("set_power_mode succeed!rn");
else
pc_my.printf("set_power_mode failure!rn");
/* 标准分辨率,Standby时间1ms*/
bmp280_set_work_mode(BMP280_STANDARD_RESOLUTION_MODE);
bmp280_set_standby_durn(BMP280_STANDBY_TIME_1_MS);
bmp280_get_standby_durn(&v_standby_time_u8);
while(1){
//读取数据:
/* APIs are used to read the uncompensated temperature/pressure*/
bmp280_read_uncomp_temperature(&v_data_uncomp_tem_s32);
bmp280_read_uncomp_pressure(&v_data_uncomp_pres_s32);
/* APIs are used to read the true temperature/pressure*/
/* Input value as uncompensated temperature/pressure*/
actual_temp = bmp280_compensate_temperature_double(v_data_uncomp_tem_s32);
actual_press = bmp280_compensate_pressure_double(v_data_uncomp_pres_s32);
pc_my.printf("The uncompensated temperature is= %d, The uncompensated pressure is= %drn", v_data_uncomp_tem_s32, v_data_uncomp_pres_s32 );
pc_my.printf("The actual temperature is= %f DegC, The actual pressure is= %f Parn ", actual_temp, actual_press);
wait(2);
}
}
打开putty,打开串口Serial的端口,设置好串口波特率,我自己设定的是115200。然后就可以获取数据了,数值大概如下:
BMP280对温度和气压的测量是通过补偿数据compensated data然后根据自己的算法得到的。根据数据,气压和温度可见是相当灵敏和准确的。
1