本帖最后由 辉常可乐 于 2017-3-17 20:35 编辑
在mini板子上做BMP180实验,在LCD屏上显示,得到的数据完全和标准大气压是两回事,大家能帮忙看看嘛,以下是代码,温湿度是用DHT11检测出来的
[mw_shl_code=applescript,true]#include "BMP.h"
#include "delay.h"
#include "myiic.h"
//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;
float True_Temp=0; //????,??:?
float True_Press=0; //????,??
a
float True_Altitude=0; //????,??:m
u8 BMP180_ID=0; //BMP180?ID
void IIC_PortInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //????GPIO_InitTypeDef??????
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_12|GPIO_Pin_11); //PC1,PC2
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;
GPIO_Init(GPIOC, &GPIO_InitStructure);
SCL_H(); //??
SDA_H();
}
void IIC_Init(void)
{
SCL_H(); //SCL = 1;
delay_us(5);
SDA_H(); //SDA = 1;
delay_us(5);
}
void IIC_Start(void)
{
SDA_H(); //SDA = 1;
delay_us(5);
SCL_H(); //SCL = 1;
delay_us(5);
SDA_L(); //SDA = 0;
delay_us(5);
}
void IIC_Stop(void)
{
SDA_L(); //SDA = 0;
delay_us(5);
SCL_H(); //SCL = 1;
delay_us(5);
SDA_H(); //SDA = 1;
delay_us(5);
}
unsigned char IIC_ReceiveACK(void)
{
unsigned char ACK;
SDA_H(); //SDA=1;//???????????,??????????,???!
SCL_H(); //SCL=1;
delay_us(5);
if (SDA==1) //SDA??
{
ACK=1;
}
else ACK=0; //SDA??
SCL_L(); //SCL = 0;//SCL?????SDA?????????,???????????
delay_us(5);
return ACK;
}
void IIC_SendACK(unsigned char ack)
{
if (ack==1)SDA_H();
else if (ack==0)
SDA_L();//SDA = ack;
SCL_H(); //SCL = 1;
delay_us(5);
SCL_L(); //SCL = 0;
delay_us(5);
}
unsigned char IIC_SendByte(unsigned char dat)
{
unsigned char i;
unsigned char bResult=1;
SCL_L(); //SCL = 0;//?????
delay_us(5);
for( i=0;i<8;i++ ) //??SCK,?dat????????SDA?
{
if( (dat<<i)&0x80 )
SDA_H(); //SDA = 1;//????
else SDA_L(); //SDA = 0;
delay_us(5);
SCL_H(); //SCL = 1;
delay_us(5);
SCL_L(); //SCL = 0;
delay_us(5);
}
bResult=IIC_ReceiveACK(); //??????????,????????
return bResult; //??????
}
unsigned char IIC_ReadByte(void)
{
unsigned char dat;
unsigned char i;
SCL_H(); //SCL = 1;//????????????
delay_us(5);
for( i=0;i<8;i++ )
{
dat<<=1;
dat=dat | (SDA);
delay_us(5);
SCL_L(); //SCL = 0;
delay_us(5);
SCL_H(); //SCL = 1;
delay_us(5);
}
return dat;
}
/*?BMP180??1??????*/
u8 BMP180_ReadOneByte(u8 ReadAddr)
{
u8 temp=0;
u8 IIC_ComFlag=1; //IIC????,?0????,1??????
IIC_Start(); //IIC start
IIC_ComFlag=IIC_SendByte(BMP180_SlaveAddr); //slave address+W:0
//printf("IIC_ComFlag=%u
",IIC_ComFlag);
if (IIC_ComFlag==0) //????0??????,?????????????
{
IIC_SendByte(ReadAddr); //??????
IIC_Start();
IIC_SendByte(BMP180_SlaveAddr|0x01); //slave address+R:1
temp=IIC_ReadByte(); //???
IIC_SendACK(1);
IIC_Stop();
}
return (temp);
}
/*?BMP180??2??????*/
short BMP180_ReadTwoByte(u8 ReadAddr)
{
u8 IIC_ComFlag=1; //IIC????,?0????,1??????
u8 MSB,LSB;
short temp;
IIC_Start();
IIC_ComFlag=IIC_SendByte(BMP180_SlaveAddr);
if (IIC_ComFlag==0)
{
IIC_SendByte(ReadAddr);
IIC_Start();
IIC_SendByte(BMP180_SlaveAddr|0x01);
MSB=IIC_ReadByte(); //????
IIC_SendACK(0); //ACK
LSB=IIC_ReadByte(); //????
IIC_SendACK(1); //NACK
IIC_Stop();
}
temp=MSB*256+LSB;
return temp;
}
/*?BMP180????????????*/
void Write_OneByteToBMP180(u8 RegAdd, u8 Data)
{
IIC_Start(); //IIC start
IIC_SendByte(BMP180_SlaveAddr); //slave address+W:0
IIC_SendByte(RegAdd);
IIC_SendByte(Data);
IIC_Stop();
}
/*??BMP180?????*/
void Read_CalibrationData(void)
{
AC1=BMP180_ReadTwoByte(0xaa);
AC2=BMP180_ReadTwoByte(0xac);
AC3=BMP180_ReadTwoByte(0xae);
AC4=BMP180_ReadTwoByte(0xb0);
AC5=BMP180_ReadTwoByte(0xb2);
AC6=BMP180_ReadTwoByte(0xb4);
B1=BMP180_ReadTwoByte(0xb6);
B2=BMP180_ReadTwoByte(0xb8);
MB=BMP180_ReadTwoByte(0xba);
MC=BMP180_ReadTwoByte(0xbc);
MD=BMP180_ReadTwoByte(0xbe);
/* printf("AC1:%d
",AC1);
printf("AC2:%d
",AC2);
printf("AC3:%d
",AC3);
printf("AC4:%d
",AC4);
printf("AC5:%d
",AC5);
printf("AC6:%d
",AC6);
printf("B1:%d
",B1);
printf("B2:%d
",B2);
printf("MB:%d
",MB);
printf("MC:%d
",MC);
printf("MD:%d
",MD); */
}
/*?BMP180??????????*/
long Get_BMP180UT(void)
{
long UT;
Write_OneByteToBMP180(0xf4,0x2e); //write 0x2E into reg 0xf4
delay_ms(10); //wait 4.5ms
UT=BMP180_ReadTwoByte(0xf6); //read reg 0xF6(MSB),0xF7(LSB)
// printf("UT:%ld
",UT);
return UT;
}
/*?BMP180??????????*/
long Get_BMP180UP(void)
{
long UP=0;
Write_OneByteToBMP180(0xf4,0x34); //write 0x34 into reg 0xf4
delay_ms(10); //wait 4.5ms
UP=BMP180_ReadTwoByte(0xf6);
UP&=0x0000FFFF;// printf("UP:%ld
",UP);
return UP;
}
/*?????????????????????????
*True_Temp:?????,??:?
*True_Press:?????,??
a
*True_Altitude:??????,??:m
*/
void Convert_UncompensatedToTrue(long UT,long UP)
{
long X1,X2,X3,B3,B5,B6,B7,T,P;
unsigned long B4;
X1=((UT-AC6)*AC5)>>15; //printf("X1:%ld
",X1);
X2=((long)MC<<11)/(X1+MD); //printf("X2:%ld
",X2);
B5=X1+X2; //printf("B5:%ld
",B5);
T=(B5+8)>>4; //printf("T:%ld
",T);
True_Temp=T/10.0; // printf("Temperature:%.1f
",True_Temp);
B6=B5-4000; //printf("B6:%ld
",B6);
X1=(B2*B6*B6)>>23; //printf("X1:%ld
",X1);
X2=(AC2*B6)>>11; //printf("X2:%ld
",X2);
X3=X1+X2; //printf("X3:%ld
",X3);
B3=(((long)AC1*4+X3)+2)/4; //printf("B3:%ld
",B3);
X1=(AC3*B6)>>13; //printf("X1:%ld
",X1);
X2=(B1*(B6*B6>>12))>>16; //printf("X2:%ld
",X2);
X3=((X1+X2)+2)>>2; //printf("X3:%ld
",X3);
B4=AC4*(unsigned long)(X3+32768)>>15; //printf("B4:%lu
",B4);
B7=((unsigned long)UP-B3)*50000; //printf("B7:%lu
",B7);
if (B7 < 0x80000000)
{
P=(B7*2)/B4;
}
else P=(B7/B4)*2; //printf("
:%ld
",P);
X1=(P/256.0)*(P/256.0); //printf("X1:%ld
",X1);
X1=(X1*3038)>>16; //printf("X1:%ld
",X1);
X2=(-7357*P)>>16; //printf("X2:%ld
",X2);
P=P+((X1+X2+3791)>>4); //printf("
:%ld
",P);
True_Press=P; // printf("
ress:%.1fPa
",True_Press);
True_Altitude=44330*(1.0-pow((P/101325.0),(1.0/5.255)));
//Altitude =(44330.0 * (1.0-pow((float)(pressure) / 101325.0, 1.0/5.255)) );
// printf("Altitude:%.3fm
",True_Altitude);
}
主程序:
int main(void)
{
u8 t=0;
u8 temperature;
u8 humidity;
long UT,UP;
delay_init(72); //???????
NVIC_Configuration(); //?? NVIC ???? 2:2 ??????,2 ??????
uart_init(9600); //????????? 9600
LED_Init(); //LED ?????
LCD_Init(); //??? LCD
KEY_Init(); //??? KEY
POINT_COLOR=RED;//???????
LCD_ShowString(60,50,"WarShip STM32");
LCD_ShowString(60,70,"DHT11 TEST");
LCD_ShowString(60,90,"ATOM@ALIENTEK");
LCD_ShowString(60,110,"2017/3/16");
while(DHT11_Init()) //DHT11 ???
{
LCD_ShowString(60,130,"DHT11 Error");
delay_ms(200);
LCD_ShowString(60,150,"
lease Check! ");
delay_ms(200);
}
LCD_ShowString(60,130,"DHT11 OK");
POINT_COLOR=BLUE;//???????
LCD_ShowString(60,150,"Temp: C");
LCD_ShowString(60,170,"Humi: %");
LCD_ShowString(60,190,"
ress: Pa");
Read_CalibrationData(); //??BMP180?????
while(1)
{
if(t%10==0)//? 100ms ????
{
DHT11_Read_Data(&temperature,&humidity); //??????
LCD_ShowNum(100,150,temperature,2,16); //????
LCD_ShowNum(100,170,humidity,2,16); //????
printf("Temp:%d'C
",temperature);
printf("Hum:%d%%
",humidity);
BMP180_ID = BMP180_ReadOneByte(0xd0);
UT=Get_BMP180UT();
UP=Get_BMP180UP();
Convert_UncompensatedToTrue(UT,UP);
LCD_ShowNum(105,190,True_Press,10,16);
printf("
ress:%.1fPa
",True_Press);
delay_ms(10);
}
delay_ms(10);
t++;
if(t==20)
{
t=0;
LED0=!LED0;
}
}
}
[/mw_shl_code]
您看一下“
UT=Get_BMP180UT();
UP=Get_BMP180UP();
”这两行运行完后,得到的值是什么。
算法应该是不会有什么大问题的,如果这两行的内容没有问题的话。可能是信号的接口引脚没有配置正确?
希望能看到您的源程序,一起研究下~~
一周热门 更多>