bm280

1.bme280温湿度气压传感器

1.2 寄存器说明

在这里插入图片描述

1.3 接口类型

​ BME280支持I²C和SPI数字接口;它充当了这两个协议的从属服务器。I²C接口支持标准、快速和高速模式。SPI接口同时支持4线和3线配置中的SPI模式“00”(CPOL = CPHA =“0”)和模式“11”(CPOL = CPHA =“1”)。

支持:

1.单字节写入

2.多字节写入(使用成对的寄存器地址和寄存器数据)

3.单字节读取

4.多字节读取(使用自动递增的单个寄存器地址)

1.4通讯时序

IIC接口

在这里插入图片描述

IIC通讯多字节写数据

在这里插入图片描述

IIC通讯多字节读取数据

在这里插入图片描述

SPI接口
在这里插入图片描述
SPI通讯多字节写入数据
在这里插入图片描述

SPI多字节读取数据

在这里插入图片描述

1.5 程序移植

下载官方示例程序

在这里插入图片描述

添加bme280.c,bme280.h,bme280_defs.h到工程

修改接口文件common.c文件

官方修改示例(IIC)
在这里插入图片描述

/**
 * Copyright (C) 2020 Bosch Sensortec GmbH. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

//#include "coines.h"  /*对应平台接口驱动*/
#include "bme280.h"
#include "common.h"

/******************************************************************************/
/*!                               Macros                                      */

#define BME280_SHUTTLE_ID  UINT8_C(0x33)

/******************************************************************************/
/*!                Static variable definition                                 */

/*! Variable that holds the I2C device address or SPI chip selection */
static uint8_t dev_addr;

/******************************************************************************/
/*!                User interface functions                                   */

/*!
 * I2C read function map to COINES platform
 */
BME280_INTF_RET_TYPE bme280_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
    /*对应平台IIC读数据接口*/
}

/*!
 * I2C write function map to COINES platform
 */
BME280_INTF_RET_TYPE bme280_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
    /*对应平台IIC写数据接口*/
}

/*!
 * SPI read function map to COINES platform
 */
BME280_INTF_RET_TYPE bme280_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
    /*对应平台SPI读数据接口*/
}

/*!
 * SPI write function map to COINES platform
 */
BME280_INTF_RET_TYPE bme280_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
  
    /*对应平台SPI写数据接口*/
    
}

/*!
 * Delay function map to COINES platform
 */
void bme280_delay_us(uint32_t period, void *intf_ptr)
{
  
    /*对应平台微秒延时*/
}

/*!
 *  @brief Prints the execution status of the APIs.
 */
void bme280_error_codes_print_result(const char api_name[], int8_t rslt)
{
    if (rslt != BME280_OK)
    {
        printf("%s\t", api_name);

        switch (rslt)
        {
            case BME280_E_NULL_PTR:
                printf("Error [%d] : Null pointer error.", rslt);
                printf(
                    "It occurs when the user tries to assign value (not address) to a pointer, which has been initialized to NULL.\r\n");
                break;

            case BME280_E_COMM_FAIL:
                printf("Error [%d] : Communication failure error.", rslt);
                printf(
                    "It occurs due to read/write operation failure and also due to power failure during communication\r\n");
                break;

            case BME280_E_DEV_NOT_FOUND:
                printf("Error [%d] : Device not found error. It occurs when the device chip id is incorrectly read\r\n",
                       rslt);
                break;

            case BME280_E_INVALID_LEN:
                printf("Error [%d] : Invalid length error. It occurs when write is done with invalid length\r\n", rslt);
                break;

            default:
                printf("Error [%d] : Unknown error code\r\n", rslt);
                break;
        }
    }
}

/*!
 *  @brief Function to select the interface between SPI and I2C.
 */
int8_t bme280_interface_selection(struct bme280_dev *dev, uint8_t intf)
{
    int8_t rslt = BME280_OK;
    struct coines_board_info board_info;

    if (dev != NULL)
    {
        int16_t result = coines_open_comm_intf(COINES_COMM_INTF_USB, NULL);

        if (result < COINES_SUCCESS)
        {
            printf(
                "\n Unable to connect with Application Board ! \n" " 1. Check if the board is connected and powered on. \n" " 2. Check if Application Board USB driver is installed. \n"
                " 3. Check if board is in use by another application. (Insufficient permissions to access USB) \n");
            exit(result);
        }

        result = coines_get_board_info(&board_info);

#if defined(PC)
        setbuf(stdout, NULL);
#endif

        if (COINES_SUCCESS != result)
        {
            printf("\n Unable to retrieve board information ! \n");
            exit(COINES_E_FAILURE);
        }

        if ((board_info.shuttle_id != BME280_SHUTTLE_ID))
        {
            printf("! Warning invalid sensor shuttle \n ," "This application will not support this sensor \n");
            exit(COINES_E_FAILURE);
        }

        coines_set_shuttleboard_vdd_vddio_config(0, 0);
        coines_delay_msec(100);

        /* Bus configuration : I2C */
        if (intf == BME280_I2C_INTF)
        {
            printf("I2C Interface\n");

            dev_addr = BME280_I2C_ADDR_PRIM;
            dev->read = bme280_i2c_read;
            dev->write = bme280_i2c_write;
            dev->intf = BME280_I2C_INTF;
			/* SDO pin is made low*/
            coines_set_pin_config(COINES_SHUTTLE_PIN_SDO, COINES_PIN_DIRECTION_OUT, COINES_PIN_VALUE_LOW);

            /* set the sensor interface as I2C with 100kHz speed */
            result = coines_config_i2c_bus(COINES_I2C_BUS_0, COINES_I2C_STANDARD_MODE);
        }
        /* Bus configuration : SPI */
        else if (intf == BME280_SPI_INTF)
        {
            printf("SPI Interface\n");

            dev_addr = COINES_SHUTTLE_PIN_7;
            dev->read = bme280_spi_read;
            dev->write = bme280_spi_write;
            dev->intf = BME280_SPI_INTF;

            result = coines_config_spi_bus(COINES_SPI_BUS_0, COINES_SPI_SPEED_5_MHZ, COINES_SPI_MODE3);
        }

        if(result != COINES_SUCCESS)
        {
            rslt = COINES_E_COMM_INIT_FAILED;
        }

        /* Holds the I2C device addr or SPI chip selection */
        dev->intf_ptr = &dev_addr;

        /* Configure delay in microseconds */
        dev->delay_us = bme280_delay_us;

        coines_delay_msec(100);

        coines_set_shuttleboard_vdd_vddio_config(3300, 3300);

        coines_delay_msec(100);
    }
    else
    {
        rslt = BME280_E_NULL_PTR;
    }

    return rslt;
}

/*!
 *  @brief Function deinitializes coines platform.
 */
void bme280_coines_deinit(void)
{
    /*对应平台deinit*/
}

主函数

int8_t rslt;
uint32_t period;
struct bme280_dev dev;
struct bme280_settings settings;
int main()
{
    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);
    
   while(1)
   {
       
       rslt = get_humidity(period, &dev);
    bme280_error_codes_print_result("get_humidity", rslt);//湿度
		Delay(100);
		rslt = get_pressure(period, &dev);
    bme280_error_codes_print_result("get_pressure", rslt);//气压
		Delay(100);
		rslt = get_temperature(period, &dev);
    bme280_error_codes_print_result("get_temperature", rslt);//温度
       
   }
    
}


官方例程

Humidity Sensor BME280 | Bosch Sensortec (bosch-sensortec.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值