sensor.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "sensor.h"
  2. /**/
  3. int8_t Humidity, Temperature;
  4. uint16_t Pressure;
  5. /**/
  6. static struct bme280_dev SensorDev;
  7. static struct bme280_data SensorData;
  8. /**/
  9. void sensor_Init(void) {
  10. int8_t rsltSensor;
  11. Flag.BME280 = 0;
  12. SensorDev.dev_id = (BME280_I2C_ADDR_PRIM << 1);
  13. SensorDev.intf = BME280_I2C_INTF;
  14. SensorDev.read = user_i2c_read;
  15. SensorDev.write = user_i2c_write;
  16. SensorDev.delay_ms = tdelay_ms;
  17. rsltSensor = bme280_init(&SensorDev);
  18. if (rsltSensor == BME280_OK) {
  19. Flag.BME280 = 1;
  20. /* BME280 Recommended mode of operation: Indoor navigation */
  21. SensorDev.settings.osr_h = BME280_OVERSAMPLING_1X;
  22. SensorDev.settings.osr_p = BME280_OVERSAMPLING_16X;
  23. SensorDev.settings.osr_t = BME280_OVERSAMPLING_2X;
  24. SensorDev.settings.filter = BME280_FILTER_COEFF_16;
  25. rsltSensor = bme280_set_sensor_settings((BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL), &SensorDev);
  26. RTOS_SetTask(sensor_StartMeasure, 103, 1000);
  27. RTOS_SetTask(sensor_GetData, 603, 1000);
  28. }
  29. }
  30. void sensor_StartMeasure(void) {
  31. bme280_set_sensor_mode(BME280_FORCED_MODE, &SensorDev);
  32. }
  33. void sensor_GetData(void) {
  34. bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
  35. int32_t tmp;
  36. tmp = SensorData.humidity + 512;
  37. Humidity = (int8_t)(tmp / 1024);
  38. tmp = SensorData.temperature + 50;
  39. Temperature = (int8_t)(tmp / 100);
  40. /* in 32-bit arithmetics pressure in Pa */
  41. tmp = SensorData.pressure * 1000;
  42. tmp += 66661;
  43. tmp /= 133322;
  44. /* pressure in mmHg */
  45. Pressure = (uint16_t)tmp;
  46. }