这是一个气压计模块,可以读取气压和温度,计算海拔。
连接方式iic
数据获取流程:
145.png
注意:
long BMP_Read_UT(void)和long BMP_Read_UP(void)一定要有 HAL_Delay(5); 的延时
bmp180.c

#include "bmp180.h"

//存储BMP180数据的结构
_bmp180 bmp180;


//写一个数据到BMP180
void BMP_WriteOneByte(uint8_t add,uint8_t data)
{
    uint8_t *pData;
    pData = &data;
    HAL_I2C_Mem_Write(&hi2c1,0xEE,add,I2C_MEMADD_SIZE_8BIT,pData,1,100);
    //使用这条子函数写入数据需要把数据放入数组,然后把数组的名字作为参数传递。详细看开发手册
}

//从BMP180读一个字节数据
uint8_t BMP_ReadOneByte(uint8_t Reg)
{
    HAL_StatusTypeDef status = HAL_OK;
    uint8_t value = 0;
    status = HAL_I2C_Mem_Read(&hi2c1, 0xEF, Reg, I2C_MEMADD_SIZE_8BIT, &value, 1, 100);
    /* 检测I2C通信状态 */
    if(status != HAL_OK)
    {
    /* 调用I2C通信错误处理函数 */
    }
return value;
}

//从BMP180读一个16位的数据
short BMP_ReadTwoByte(uint8_t Reg)
{
    char hd,ld;
    short value;
    hd=BMP_ReadOneByte(Reg);
    ld=BMP_ReadOneByte(Reg+1);
    value=(hd<<8)|ld;
    return value;
}

//从BMP180的获取计算参数
void BMP_ReadCalibrationData(void)
{
    bmp180.AC1 = BMP_ReadTwoByte(0xAA);
    bmp180.AC2 = BMP_ReadTwoByte(0xAC);
    bmp180.AC3 = BMP_ReadTwoByte(0xAE);
    bmp180.AC4 = BMP_ReadTwoByte(0xB0);
    bmp180.AC5 = BMP_ReadTwoByte(0xB2);
    bmp180.AC6 = BMP_ReadTwoByte(0xB4);
    bmp180.B1  = BMP_ReadTwoByte(0xB6);
    bmp180.B2  = BMP_ReadTwoByte(0xB8);
    bmp180.MB  = BMP_ReadTwoByte(0xBA);
    bmp180.MC  = BMP_ReadTwoByte(0xBC);
    bmp180.MD  = BMP_ReadTwoByte(0xBE);
}

//从BMP180读取未修正的温度
long BMP_Read_UT(void)
{
    long temp = 0;
    BMP_WriteOneByte(0xF4,0x2E);

    HAL_Delay(5);
    temp = (long)BMP_ReadTwoByte(0xF6);
    return temp;
}

//从BMP180读取未修正的大气压
long BMP_Read_UP(void)
{
    long pressure = 0;

    BMP_WriteOneByte(0xF4,0x34);
    HAL_Delay(5);

    pressure = (long)BMP_ReadTwoByte(0xF6);
    //pressure = pressure + BMP_ReadOneByte(0xf8);
    pressure &= 0x0000FFFF;

    return pressure;
}

//用获取的参数对温度和大气压进行修正,并计算海拔
void BMP_UncompemstatedToTrue(void)
{
        BMP_ReadCalibrationData();
    bmp180.UT = BMP_Read_UT();//第一次读取错误
    bmp180.UT = BMP_Read_UT();//进行第二次读取修正参数
    bmp180.UP = BMP_Read_UP();

    bmp180.X1 = ((bmp180.UT - bmp180.AC6) * bmp180.AC5) >> 15;
    bmp180.X2 = (((long)bmp180.MC) << 11) / (bmp180.X1 + bmp180.MD);
    bmp180.B5 = bmp180.X1 + bmp180.X2;
    bmp180.Temp  = (bmp180.B5 + 8) >> 4;

    bmp180.B6 = bmp180.B5 - 4000;
    bmp180.X1 = ((long)bmp180.B2 * (bmp180.B6 * bmp180.B6 >> 12)) >> 11;
    bmp180.X2 = ((long)bmp180.AC2) * bmp180.B6 >> 11;
    bmp180.X3 = bmp180.X1 + bmp180.X2;

    bmp180.B3 = ((((long)bmp180.AC1) * 4 + bmp180.X3) + 2) /4;
    bmp180.X1 = ((long)bmp180.AC3) * bmp180.B6 >> 13;
    bmp180.X2 = (((long)bmp180.B1) *(bmp180.B6*bmp180.B6 >> 12)) >>16;
    bmp180.X3 = ((bmp180.X1 + bmp180.X2) + 2) >> 2;
    bmp180.B4 = ((long)bmp180.AC4) * (unsigned long)(bmp180.X3 + 32768) >> 15;
    bmp180.B7 = ((unsigned long)bmp180.UP - bmp180.B3) * 50000;

    if(bmp180.B7 < 0x80000000)
    {
        bmp180.p = (bmp180.B7 * 2) / bmp180.B4;     
    }
    else
    {
        bmp180.p = (bmp180.B7 / bmp180.B4) * 2;
    }

    bmp180.X1 = (bmp180.p >> 8) * (bmp180.p >>8);
    bmp180.X1 = (((long)bmp180.X1) * 3038) >> 16;
    bmp180.X2 = (-7357 * bmp180.p) >> 16;

    bmp180.p = bmp180.p + ((bmp180.X1 + bmp180.X2 + 3791) >> 4);

    bmp180.altitude = 44330 * (1-pow(((bmp180.p) / 101325.0),(1.0/5.255)));  
}

bmp180.h

#include "main.h"
#include "math.h"
extern I2C_HandleTypeDef hi2c1;                   //cube MX生成的一个资源句柄
typedef struct __BMP180
{
    short AC1;
    short AC2;
    short AC3;
    unsigned short AC4;
    unsigned short AC5;
    unsigned short AC6;
    short B1;
    short B2;
    short MB;
    short MC;
    short MD;
    long UT;
    long UP;
    long X1;
    long X2;
    long X3;
    long B3;
    unsigned long B4;
    long B5;
    long B6;
    long B7;
    long p;
    long Temp;
    float altitude;
}_bmp180;

extern _bmp180 bmp180;

uint8_t BMP_ReadOneByte(uint8_t ReadAddr);
void BMP_WriteOneByte(uint8_t WriteAddr,uint8_t DataToWrite);
short BMP_ReadTwoByte(uint8_t ReadAddr);
void BMP_ReadCalibrationData(void);
long BMP_Read_UT(void);
long BMP_Read_UP(void);
void BMP_UncompemstatedToTrue(void);

使用方法:
循环调用BMP_UncompemstatedToTrue();
气压保存在bmp180.p
海拔保存在bmp180.altitude
144.jpg

最后修改:2020 年 06 月 28 日
声明:无闻风博客|版权所有,违者必究|如未注明,均为原创| 转载:转载请注明原文链接