#include "mbed.h"
#include "bmp280.h"
/************** SPI control mask******/
#define SPI_READ 0x80
#define SPI_WRITE 0x7F
/********************SPI function declarations***********************/
//SPI master连接到SPI通信引脚
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!\r\n");
else
pc_my.printf("initialization failure!\r\n");
/*设置工作模式为Normal mode*/
com_rslt+= bmp280_set_power_mode(BMP280_NORMAL_MODE);
if(com_rslt==0)
pc_my.printf("set_power_mode succeed!\r\n");
else
pc_my.printf("set_power_mode failure!\r\n");
/* 标准分辨率,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= %d\r\n", v_data_uncomp_tem_s32, v_data_uncomp_pres_s32 );
pc_my.printf("The actual temperature is= %f DegC, The actual pressure is= %f Pa\r\n ", actual_temp, actual_press);
wait(2);
}
}