130 lines
4.3 KiB
C

/**\
* Copyright (C) 2020 Bosch Sensortec GmbH
*
* The license is available at root folder
*
**/
/******************************************************************************/
/*! Header Files */
#include "stdio.h"
#include "bme280.h"
#include "common.h"
/******************************************************************************/
/*! Macros */
#define SAMPLE_COUNT UINT8_C(50)
/******************************************************************************/
/*! Static Function Declaration */
/*!
* @brief This internal API is used to get compensated pressure data.
*
* @param[in] period : Contains the delay in microseconds.
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Status of execution.
*/
static int8_t get_pressure(uint32_t period, struct bme280_dev *dev);
/******************************************************************************/
/*! Functions */
/* This function starts the execution of program. */
int main(void)
{
int8_t rslt;
uint32_t period;
struct bme280_dev dev;
struct bme280_settings settings;
/* Interface selection is to be updated as parameter
* For I2C : BME280_I2C_INTF
* For SPI : BME280_SPI_INTF
*/
rslt = bme280_interface_selection(&dev, BME280_I2C_INTF);
bme280_error_codes_print_result("bme280_interface_selection", rslt);
rslt = bme280_init(&dev);
bme280_error_codes_print_result("bme280_init", rslt);
/* Always read the current settings before writing, especially when all the configuration is not modified */
rslt = bme280_get_sensor_settings(&settings, &dev);
bme280_error_codes_print_result("bme280_get_sensor_settings", rslt);
/* Configuring the over-sampling rate, filter coefficient and standby time */
/* Overwrite the desired settings */
settings.filter = BME280_FILTER_COEFF_2;
/* Over-sampling rate for humidity, temperature and pressure */
settings.osr_h = BME280_OVERSAMPLING_1X;
settings.osr_p = BME280_OVERSAMPLING_1X;
settings.osr_t = BME280_OVERSAMPLING_1X;
/* Setting the standby time */
settings.standby_time = BME280_STANDBY_TIME_0_5_MS;
rslt = bme280_set_sensor_settings(BME280_SEL_ALL_SETTINGS, &settings, &dev);
bme280_error_codes_print_result("bme280_set_sensor_settings", rslt);
/* Always set the power mode after setting the configuration */
rslt = bme280_set_sensor_mode(BME280_POWERMODE_NORMAL, &dev);
bme280_error_codes_print_result("bme280_set_power_mode", rslt);
/* Calculate measurement time in microseconds */
rslt = bme280_cal_meas_delay(&period, &settings);
bme280_error_codes_print_result("bme280_cal_meas_delay", rslt);
printf("\nPressure calculation (Data displayed are compensated values)\n");
printf("Measurement time : %lu us\n\n", (long unsigned int)period);
rslt = get_pressure(period, &dev);
bme280_error_codes_print_result("get_pressure", rslt);
bme280_coines_deinit();
return 0;
}
/*!
* @brief This internal API is used to get compensated pressure data.
*/
static int8_t get_pressure(uint32_t period, struct bme280_dev *dev)
{
int8_t rslt = BME280_E_NULL_PTR;
int8_t idx = 0;
uint8_t status_reg;
struct bme280_data comp_data;
while (idx < SAMPLE_COUNT)
{
rslt = bme280_get_regs(BME280_REG_STATUS, &status_reg, 1, dev);
bme280_error_codes_print_result("bme280_get_regs", rslt);
if (status_reg & BME280_STATUS_MEAS_DONE)
{
/* Measurement time delay given to read sample */
dev->delay_us(period, dev->intf_ptr);
/* Read compensated data */
rslt = bme280_get_sensor_data(BME280_PRESS, &comp_data, dev);
bme280_error_codes_print_result("bme280_get_sensor_data", rslt);
#ifdef BME280_64BIT_ENABLE
comp_data.pressure = comp_data.pressure / 100;
#endif
#ifdef BME280_DOUBLE_ENABLE
printf("Pressure[%d]: %lf Pa\n", idx, comp_data.pressure);
#else
printf("Pressure[%d]: %lu Pa\n", idx, (long unsigned int)comp_data.pressure);
#endif
idx++;
}
}
return rslt;
}