我怀疑是BMP180模块坏掉了
采用STM32F103RCT6软件模拟IIC (复制正点原子), 硬件连接检查没有问题,3.3V供电,程序如下
#include"BMP180.h"
#include"myiic.h"
#include"delay.h"
short AC1,AC2,AC3;
unsigned short AC4,AC5,AC6;
short B1,B2,MB,MC,MD;
long UT,UP;
long X1,X2,B5,B6,X3,B3,P,B7;
unsigned long B4;
long TEMP,PRES;
short BMP_Read_Data(u8 RegAddress)
{
u8 MSB, LSB;
short DATA;
IIC_Start();
IIC_Send_Byte(BMP180_SlaveAddress); //´Ó»úµØÖ·¼ÓдÐźÅ
IIC_Wait_Ack();
IIC_Send_Byte(RegAddress);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(BMP180_SlaveAddress+1); //´Ó»úµØÖ·¼Ó¶ÁÐźÅ
MSB= IIC_Read_Byte(1);
LSB= IIC_Read_Byte(1);
IIC_Stop();
delay_ms(5);
DATA=MSB << 8;
DATA |= LSB;
return DATA;
}
void Init_BMP180(void)
{
AC1 = BMP_Read_Data(0xAA);
AC2 = BMP_Read_Data(0xAC);
AC3 = BMP_Read_Data(0xAE);
AC4 = BMP_Read_Data(0xB0);
AC5 = BMP_Read_Data(0xB2);
AC6 = BMP_Read_Data(0xB4);
B1 = BMP_Read_Data(0xB6);
B2 = BMP_Read_Data(0xB8);
MB = BMP_Read_Data(0xBA);
MC = BMP_Read_Data(0xBC);
MD = BMP_Read_Data(0xBE);
}
long BMP_Start_TEM_Measurement(void)
{
long temperature;
IIC_Start();
IIC_Send_Byte(BMP180_SlaveAddress); //´Ó»úµØÖ·¼ÓдÐźÅ
IIC_Wait_Ack();
IIC_Send_Byte(0xF4);
IIC_Wait_Ack();
IIC_Send_Byte(0x2E);
IIC_Stop();
delay_ms(10); // max time is 4.5ms
temperature=BMP_Read_Data(0xF6);
return temperature;
}
long BMP_Start_PRES_Measurement(void)
{
long pressure;
IIC_Start();
IIC_Send_Byte(BMP180_SlaveAddress); //´Ó»úµØÖ·¼ÓдÐźÅ
IIC_Wait_Ack();
IIC_Send_Byte(0xF4); // write register address
IIC_Wait_Ack();
IIC_Send_Byte(0x34); // write register data for pressure
IIC_Stop();
delay_ms(10); // max time is 4.5ms
pressure = BMP_Read_Data(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
void BMP_TEMP_Convert(long *TEMPE)
{
UT=BMP_Start_TEM_Measurement();
UT=BMP_Start_TEM_Measurement();
X1 = (UT - AC6) * AC5 >> 15;
X2 = ((long) MC << 11) / (X1 + MD);
B5 = X1 + X2;
*TEMPE = (B5 + 8) >> 4;
}
void BMP_PRES_Convert(long *PRESE)
{
UP=BMP_Start_PRES_Measurement();
UP=BMP_Start_PRES_Measurement();
B6 = B5 - 4000;
X1 = (B2 * (B6 * B6 >> 12)) >> 11;
X2 = AC2 * B6 >> 11;
X3 = X1 + X2;
B3 = (((long)AC1 * 4 + X3) + 2)/4;
X1 = AC3 * B6 >> 13;
X2 = (B1 * (B6 * B6 >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = (AC4 * (unsigned long) (X3 + 32768)) >> 15;
B7 = ((unsigned long) UP - B3) * (50000 >> OSS);
if( B7 < 0x80000000) P = (B7 * 2) / B4 ;
else
P = (B7 / B4) * 2;
X1 = (P>> 8) * (P >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * P) >> 16;
*PRESE = P + ((X1 + X2 + 3791) >> 4);
}
主程序中
调用初始化函数后
while(1)
{
BMP_TEMP_Convert(&TEMPERATURE);
BMP_PRES_Convert(&PRESSURE);
printf("%f\r\n",(double)PRESSURE);
//printf("%f\r\n",(double)PRESSURE);
delay_ms(100);
}
读出来的温度数据是32度 明显不符合实际,手机定位显示温度是3摄氏度
读出来的气压数据是623hpa 不符合实际 一般认为气压101KPa即1010hpa
|