ProjectsByJRP / stm32-bme280

Bosch BME280 driver for STM32 using HAL - both SPI and I2C work
29 stars 12 forks source link

A simple example #2

Open Alex-7 opened 4 years ago

Alex-7 commented 4 years ago

Could you, please, publish a simple example of how to read data via I2C from BME280 with the library?

I know how to generate the project in the STM32CubeIDE, how to activate the i2C, connect the BME280 to the SDA and SCL.

I would like to see how I could initialize the sensor in the main.c and how to make the read of the temperature, pressure, humidity, and altitude.

Thank you and best regards!

martinius96 commented 5 months ago

4 years later and no progress :O

SimiThePro commented 3 months ago

This is my main.c:

/* USER CODE BEGIN PFP */

s8 bme280_i2c_read(u8 dev_id, u8 reg_addr, u8 *reg_data, u8 len) {
  HAL_StatusTypeDef status;

  status = HAL_I2C_Mem_Read(&hi2c1, dev_id, reg_addr, 1, reg_data, len, 1000);

  if (status == HAL_OK) {
    return 0;  
  } else {
    return -1;  
  }
}

s8 bme280_i2c_write(u8 dev_id, u8 reg_addr, u8 *register_data, u8 wr_len) {
    HAL_StatusTypeDef status;

    status = HAL_I2C_Mem_Write(&hi2c1, dev_id, reg_addr, 1, register_data, wr_len, 1000);

    if (status == HAL_OK) {
        return 0; // Erfolg
    } else {
        return -1; // Fehler
    }
}

/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
struct bme280_t myBME280 = {.chip_id = 0x60,
                            .dev_addr = (0x76 << 1),
                            .bus_read = bme280_i2c_read,
                            .bus_write = bme280_i2c_write,
                            .delay_msec = HAL_Delay};
/* USER CODE END 0 */
int main(void) {
  ...
  /* USER CODE BEGIN 2 */
  bme280_init(&myBME280);

  bme280_set_power_mode(BME280_NORMAL_MODE);
  bme280_set_standby_durn(BME280_STANDBY_TIME_1_MS);
  bme280_set_oversamp_temperature(BME280_OVERSAMP_2X);
  bme280_set_oversamp_pressure(BME280_OVERSAMP_16X);
  bme280_set_oversamp_humidity(BME280_OVERSAMP_1X);
  bme280_set_filter(BME280_FILTER_COEFF_16);
  /* USER CODE END 2 */
  while (1) {
    u32 pressure, humidity;
    s32 temperature;
    bme280_read_pressure_temperature_humidity(&pressure, &temperature, &humidity);
    HAL_Delay(2000);
  }
}