参照官方pdf文档做了一个bmp180输出温度气压程序,现在温度能正常,气压就是不准确,怎么调都不行,请大佬们帮忙看看哪里出了问题
[mw_shl_code=c,true]u8 oss=0x01;
bmp_data bmp_180;
static u16 bmp_tworead(u8 msb,u8 lsb)
{
u8 s1,s2;
s1=I2C_ReadByte(bmp180_addr,msb);
s2=I2C_ReadByte(bmp180_addr,lsb);
return((s1<<8)|(s2&0xFF));
}
void bmp_getcalib(void)
{
bmp_180.ac1=(short)bmp_tworead(0xAA,0xAB);
bmp_180.ac2=(short)bmp_tworead(0xAC,0xAD);
bmp_180.ac3=(short)bmp_tworead(0xAE,0xAF);
bmp_180.ac4=bmp_tworead(0xB0,0xB1);
bmp_180.ac5=bmp_tworead(0xB2,0xB3);
bmp_180.ac6=bmp_tworead(0xB4,0xB5);
bmp_180.b1=(short)bmp_tworead(0xB6,0xB7);
bmp_180.b2=(short)bmp_tworead(0xB8,0xB9);
bmp_180.mb=(short)bmp_tworead(0xBA,0xBB);
bmp_180.mc=(short)bmp_tworead(0xBC,0xBD);
bmp_180.md=(short)bmp_tworead(0xBE,0xBF);
}
u8 bmp180_init(void)
{
bmp_getcalib();
if(I2C_ReadByte(bmp180_addr,0xD0)==0x55)
{
return(0);
}else{return(1);}
}
void bmp_getdata(long *temp,long *pres)
{
u8 xlsb;
u16 mlsb;
long x1,x2,x3,b3,b5,b6,p;
unsigned long b4,b7;
I2C_WriteByte(bmp180_addr,0xF4,0x2E);//开始测温度
delay_ms(5);
bmp_180.ut=(long)bmp_tworead(0xF6,0xF7);//读出ut
x1=(bmp_180.ut-bmp_180.ac6)*bmp_180.ac5/32768;
x2=bmp_180.mc*2048/(x1+bmp_180.md);
b5=x1+x2;
*temp=(b5+8)/16;//计算出温度
I2C_WriteByte(bmp180_addr,0xF4,0x34+(oss<<6));//开始测气压
delay_ms(8);
mlsb=bmp_tworead(0xF6,0xF7);
xlsb=I2C_ReadByte(bmp180_addr,0xF8);
bmp_180.up=(mlsb<<8+xlsb)>>(8-oss);//读出up
b6=b5-4000;
x1=(bmp_180.b2*(b6*b6/4096))/2048;
x2=bmp_180.ac2*b6/2048;
x3=x1+x2;
b3=((bmp_180.ac1*4+x3)<<oss+2)/4;
x1=bmp_180.ac3*b6/8192;
x2=(bmp_180.b1*(b6*b6/4096))/65536;
x3=((x1+x2)+2)/4;
b4=bmp_180.ac4*(unsigned long)(x3+32768)/32768;
b7=((unsigned long)bmp_180.up-b3)*(50000>>oss);
if(b7<0x80000000)
{
p=(b7*2)/b4;
}else
{
p=(b7/b4)*2;
}
x1=(p/256)*(p/256);
x1=(x1*3038)/65536;
x2=(-7357*p)/65536;
p=p+(x1+x2+3791)/16;
*pres=p;
}[/mw_shl_code]
BST-BMP180-DS000-12.pdf
(635.94 KB, 下载次数: 47)
2018-8-21 20:46 上传
点击文件名下载附件
bmp180 pdf
输出数据:
输出数据
[mw_shl_code=c,true]#include "bmp180.h"
u8 oss=0;
s16 AC1=0,AC2=0,AC3=0,B1=0,B2=0,MB=0,MC=0,MD=0;
u16 AC4=0,AC5=0,AC6=0;
static u16 BMP180_Read(u8 addr)
{
u16 temp=0;
u8 tempH=0,tempL=0;
tempH=I2C_ReadByte(0x77,addr);
tempL=I2C_ReadByte(0x77,addr+1);
temp=(tempH<<8)|(tempL&0xFF);
return temp;
}
static void bmp_getcalib(void)
{
AC1=BMP180_Read(0XAA);
AC2=BMP180_Read(0XAC);
AC3=BMP180_Read(0XAE);
AC4=BMP180_Read(0XB0);
AC5=BMP180_Read(0XB2);
AC6=BMP180_Read(0XB4);
B1=BMP180_Read(0XB6);
B2=BMP180_Read(0XB8);
MB=BMP180_Read(0XBA);
MC=BMP180_Read(0XBC);
MD=BMP180_Read(0XBE);
}
u8 bmp180_init(void)
{
if(I2C_ReadByte(0x77,0xD0)==0x55)
{
bmp_getcalib();
return(0);
}else{return(1);}
}
void BMP180_Calculation(s32 *temp,double *pres)
{
s32 UT=0,UP=0;
s32 T=0;
s32 P=0,X1=0,X2=0,X3=0,B3=0,B5=0,B6=0,B7=0;
u32 B4=0;
I2C_WriteByte(0x77,0xF4,0x2E);
delay_ms(6);
UT=BMP180_Read(0xF6);
I2C_WriteByte(0x77,0xF4,0x34);
delay_ms(7);
UP=BMP180_Read(0xf6);
X1 =(((s32)UT-(s32)AC6)*(s32)AC5)>>15;
X2 = ((s32)MC<<11)/(X1+MD);
B5 = X1 + X2;
T = (B5 + 8)>>4;
*temp=T;
B6 = B5 - 4000;
X1 = (B2*(B6*B6>>12))>>11;
X2 = AC2*B6>>11;
X3 = X1 + X2;
//B3 = (((((s32)AC1)*4 + X3)<<oss)+2)>>2;
B3 = (((AC1<<2)+X3)+2)>>2;
X1 = AC3*B6>>13;
X2 = (B1*((B6*B6)>>12))>>16;
X3 = ((X1+X2)+2)>>2;
B4 = AC4*(u32)(X3 + 32768)>>15;
//B7 = ((u32)(UP - B3)*(50000>>oss));
B7 = (u32)(UP - B3)*50000;
if (B7 < 0x80000000)
{
P = (B7<<1)/B4;
}
else
{
P = (B7/B4)<<1;
}
X1 = (P<<8)*(P<<8);
X1 = (X1*3038)>>16;
X2 = (-7357*P)>>16;
P += (X1+X2+3791)>>4;
*pres=P;
}
[/mw_shl_code]
一周热门 更多>