/*
****************************************************************************
* Copyright (C) 2013 - 2015 Bosch Sensortec GmbH
*
* bme280.c
* Date: 2015/03/27
* Revision: 2.0.4(Pressure and Temperature compensation code revision is 1.1
* and Humidity compensation code revision is 1.0)
*
* Usage: Sensor Driver file for BME280 sensor
*
****************************************************************************
* License:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of the
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
**************************************************************************/
#include "bme280.h"
static struct bme280_t *p_bme280; /**< pointer to BME280 */
/*!
* @brief This function is used for initialize
* the bus read and bus write functions
* and assign the chip id and I2C address of the BME280 sensor
* chip id is read in the register 0xD0 bit from 0 to 7
*
* @param bme280 structure pointer.
*
* @note While changing the parameter of the bme280_t
* @note consider the following point:
* Changing the reference value of the parameter
* will changes the local copy or local reference
* make sure your changes will not
* affect the reference value of the parameter
* (Better case don't change the reference value of the parameter)
*
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_init(struct bme280_t *bme280)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = BME280_INIT_VALUE;
p_bme280 = bme280;
/* assign BME280 ptr */
com_rslt = p_bme280->BME280_BUS_READ_FUNC(p_bme280->dev_addr,
BME280_CHIP_ID_REG, &v_data_u8,
BME280_GEN_READ_WRITE_DATA_LENGTH);
/* read Chip Id */
p_bme280->chip_id = v_data_u8;
//printf("p_bme280->chip_id:%x\r\n",p_bme280->chip_id);
com_rslt += bme280_get_calib_param();
/* readout bme280 calibparam structure */
return com_rslt;
}
/*!
* @brief This API is used to read uncompensated temperature
* in the registers 0xFA, 0xFB and 0xFC
* @note 0xFA -> MSB -> bit from 0 to 7
* @note 0xFB -> LSB -> bit from 0 to 7
* @note 0xFC -> LSB -> bit from 4 to 7
*
* @param v_uncomp_temperature_s32 : The value of uncompensated temperature
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_temperature(
s32 *v_uncomp_temperature_s32)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the MSB and LSb value
a_data_u8r[0] - Temperature MSB
a_data_u8r[1] - Temperature LSB
a_data_u8r[2] - Temperature XLSB
*/
u8 a_data_u8r[BME280_TEMPERATURE_DATA_SIZE] = {
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
com_rslt = p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_TEMPERATURE_MSB_REG,
a_data_u8r,
BME280_TEMPERATURE_DATA_LENGTH);
*v_uncomp_temperature_s32 = (s32)(((
(u32) (a_data_u8r[BME280_TEMPERATURE_MSB_DATA]))
<< BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
(((u32)(a_data_u8r[BME280_TEMPERATURE_LSB_DATA]))
<< BME280_SHIFT_BIT_POSITION_BY_04_BITS)
| ((u32)a_data_u8r[BME280_TEMPERATURE_XLSB_DATA] >>
BME280_SHIFT_BIT_POSITION_BY_04_BITS));
}
return com_rslt;
}
/*!
* @brief Reads actual temperature from uncompensated temperature
* @note Returns the value in 0.01 degree Centigrade
* Output value of "5123" equals 51.23 DegC.
*
*
*
* @param v_uncomp_temperature_s32 : value of uncompensated temperature
*
*
* @return Returns the actual temperature
*
*/
s32 bme280_compensate_temperature_int32(s32 v_uncomp_temperature_s32)
{
s32 v_x1_u32r = BME280_INIT_VALUE;
s32 v_x2_u32r = BME280_INIT_VALUE;
s32 temperature = BME280_INIT_VALUE;
/* calculate x1*/
v_x1_u32r =
((((v_uncomp_temperature_s32
>> BME280_SHIFT_BIT_POSITION_BY_03_BITS) -
((s32)p_bme280->cal_param.dig_T1
<< BME280_SHIFT_BIT_POSITION_BY_01_BIT))) *
((s32)p_bme280->cal_param.dig_T2)) >>
BME280_SHIFT_BIT_POSITION_BY_11_BITS;
/* calculate x2*/
v_x2_u32r = (((((v_uncomp_temperature_s32
>> BME280_SHIFT_BIT_POSITION_BY_04_BITS) -
((s32)p_bme280->cal_param.dig_T1))
* ((v_uncomp_temperature_s32 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS) -
((s32)p_bme280->cal_param.dig_T1)))
>> BME280_SHIFT_BIT_POSITION_BY_12_BITS) *
((s32)p_bme280->cal_param.dig_T3))
>> BME280_SHIFT_BIT_POSITION_BY_14_BITS;
/* calculate t_fine*/
p_bme280->cal_param.t_fine = v_x1_u32r + v_x2_u32r;
/* calculate temperature*/
temperature = (p_bme280->cal_param.t_fine * 5 + 128)
>> BME280_SHIFT_BIT_POSITION_BY_08_BITS;
return temperature;
}
/*!
* @brief Reads actual temperature from uncompensated temperature
* @note Returns the value with 500LSB/DegC centred around 24 DegC
* output value of "5123" equals(5123/500)+24 = 34.246DegC
*
*
* @param v_uncomp_temperature_s32: value of uncompensated temperature
*
*
*
* @return Return the actual temperature as s16 output
*
*/
s16 bme280_compensate_temperature_int32_sixteen_bit_output(
s32 v_uncomp_temperature_s32)
{
s16 temperature = BME280_INIT_VALUE;
bme280_compensate_temperature_int32(
v_uncomp_temperature_s32);
temperature = (s16)((((
p_bme280->cal_param.t_fine - 122880) * 25) + 128)
>> BME280_SHIFT_BIT_POSITION_BY_08_BITS);
return temperature;
}
/*!
* @brief This API is used to read uncompensated pressure.
* in the registers 0xF7, 0xF8 and 0xF9
* @note 0xF7 -> MSB -> bit from 0 to 7
* @note 0xF8 -> LSB -> bit from 0 to 7
* @note 0xF9 -> LSB -> bit from 4 to 7
*
*
*
* @param v_uncomp_pressure_s32 : The value of uncompensated pressure
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure(
s3
没有合适的资源?快使用搜索试试~ 我知道了~
bme280-driver.rar_STM32 BME280_bme280 i2c程序_bme280 程序_bme280stm3
共7个文件
c:4个
h:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 3 下载量 61 浏览量
2022-07-14
23:26:07
上传
评论
收藏 27KB RAR 举报
温馨提示
BME280的基于STM32平台的I2C驱动程序,非常难得,已经经作者本人调试ok,量产的代码,推荐给大家。
资源推荐
资源详情
资源评论
收起资源包目录
bme280-driver.rar (7个子文件)
bme280-driver
bsp_i2c_bme280.h 1KB
bsp_i2c_gpio.h 2KB
bsp_i2c_bme280.c 4KB
bme280_support.c 21KB
bme280.h 62KB
bsp_i2c_gpio.c 7KB
bme280.c 71KB
共 7 条
- 1
资源评论
- wxw_derek2023-03-03感谢资源主分享的资源解决了我当下的问题,非常有用的资源。
- m0_672595982024-02-26资源简直太好了,完美解决了当下遇到的难题,这样的资源很难不支持~
- 倡咏2024-02-01资源使用价值高,内容详实,给了我很多新想法,感谢大佬分享~
林当时
- 粉丝: 100
- 资源: 1万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功