STM32实现气压传感器测量(BMP180)

目录

0.接线设计

1.功能描述

2.四种方式实现大气压采集

3.模块选择

4.编程环境

5.模块主要参数

6.代码实现

        1)标准库模拟IIC实现气压值采集

         2)标准库硬件IIC实现气压值采集

         3)HAL库模拟IIC实现气压值采集

        4)HAL库硬件IIC实现气压值采集


0.接线设计

硬件接口:IIC_SCL -- PB6
                  IIC_SDA -- PB7

串口使用串口1:PA9和PA10

1.功能描述

        通过STM32实现BMP180大气压传感器模块数据采集,并将采集的数据在串口显示。

        本传感器支持IIC通信,实物图如下:

STM32实现气压传感器测量(BMP180)_第1张图片

STM32实现气压传感器测量(BMP180)_第2张图片

         网上建议在SCL和SDA引脚上,上拉两个4.7K电阻,保证通信稳定,但我实现没有增加这两个电阻,通信正常,可能也有不稳定情况,大家自行判断。我没有上拉电阻,信息采集正常。

STM32实现气压传感器测量(BMP180)_第3张图片

2.四种方式实现大气压采集

        1)标准库模拟IIC实现气压值采集;

        2)标准库硬件IIC实现气压值采集;

        3)HAL库模拟IIC实现气压值采集;

        4)HAL库硬件IIC实现气压值采集。

3.模块选择

        ·STM32F103C8T6(其它型号也是一样)

        ·BMP180气压传感器模块

        ·USB-TTL串口调试助手

4.编程环境

        STM32CubeMX(工程可视化配置环境IDE,HAL库使用)

        keil5(编程环境)

5.模块主要参数

        BMP180有一个产品文档,大家可以参考文档实现程序。

STM32实现气压传感器测量(BMP180)_第4张图片

         测量流程图。

        温度和大气压测量:

STM32实现气压传感器测量(BMP180)_第5张图片

STM32实现气压传感器测量(BMP180)_第6张图片

STM32实现气压传感器测量(BMP180)_第7张图片

        海拔测量:

STM32实现气压传感器测量(BMP180)_第8张图片

6.代码实现

        1)标准库模拟IIC实现气压值采集

STM32实现气压传感器测量(BMP180)_第9张图片

//********************************************************************
u16 bmp180ReadTemp(void)
{
	int cnt = 0;
	IIC_Start();                  //起始信号
	IIC_Send_Byte(BMP180_SlaveAddress);   //发送设备地址+写信号   BMP180_SlaveAddress  0xee	气压传感器器件地址
	cnt = 0;
	while(IIC_Wait_Ack())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		delay_ms(1);
		if(cnt>=100)return 1;
	} 
	IIC_Send_Byte(0xF4);	          // write register address
	cnt = 0;
	while(IIC_Wait_Ack())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		delay_ms(1);
		if(cnt>=100)return 1;
	} 
	IIC_Send_Byte(0x2E);       	// write register data for temp
	cnt = 0;
	while(IIC_Wait_Ack())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		delay_ms(1);
		if(cnt>=100)return 1;
	} 
	IIC_Stop();                   //发送停止信号
	delay_ms(10);	// max time is 4.5ms
	return Multiple_read(0xF6);
}
//*************************************************************
u16 bmp180ReadPressure(void)
{
	int cnt = 0;
	IIC_Start();                   //起始信号
	IIC_Send_Byte(BMP180_SlaveAddress);   //发送设备地址+写信号
	cnt = 0;
	while(IIC_Wait_Ack())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		delay_ms(1);
		if(cnt>=100)return 1;
	} 
	IIC_Send_Byte(0xF4);	          // write register address
	cnt = 0;
	while(IIC_Wait_Ack())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		delay_ms(1);
		if(cnt>=100)return 1;
	} 
	IIC_Send_Byte(0x34);       	  // write register data for pressure
	cnt = 0;
	while(IIC_Wait_Ack())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		delay_ms(1);
		if(cnt>=100)return 1;
	} 
	IIC_Stop();                    //发送停止信号
	delay_ms(20);    	                  // max time is 4.5ms
	return Multiple_read(0xF6);//pressure;	
}
 
//**************************************************************
 
//初始化BMP085,根据需要请参考pdf进行修改**************
void Init_BMP180()
{
	IIC_Init();
	ac1 = Multiple_read(0xAA);
	ac2 = Multiple_read(0xAC);
	ac3 = Multiple_read(0xAE);
	ac4 = Multiple_read(0xB0);
	ac5 = Multiple_read(0xB2);
	ac6 = Multiple_read(0xB4);
	b1 =  Multiple_read(0xB6);
	b2 =  Multiple_read(0xB8);
	mb =  Multiple_read(0xBA);
	mc =  Multiple_read(0xBC);
	md =  Multiple_read(0xBE);
}
//***********************************************************************
void bmp180Convert()
{
	unsigned int ut;
	unsigned long up;
	long x1, x2, b5, b6, x3, b3, p;
	unsigned long b4, b7;
 
	ut = bmp180ReadTemp();	   // 读取温度
	up = bmp180ReadPressure();  // 读取压强    return pressure;	
	//*************
	x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
	x2 = ((long) mc << 11) / (x1 + md);
	b5 = x1 + x2;
	result_UT = ((b5 + 8) >> 4);
	//*************		
	b6 = b5 - 4000;
	                         // Calculate B3
	x1 = (b2 * (b6 * b6)>>12)>>11;
	x2 = (ac2 * b6)>>11;
	x3 = x1 + x2;
	b3 = (((((long)ac1)*4 + x3)<>2;	
	                         // Calculate B4
	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<<1)/b4;
	else
	p = (b7/b4)<<1;
	
	x1 = (p>>8) * (p>>8);
	x1 = (x1 * 3038)>>16;
	x2 = (-7357 * p)>>16;
	result_UP = p+((x1 + x2 + 3791)>>4);
}
 
 
/****************************************************************
   函数名称:BMP085_Get_Altitude			    
   函数功能:获取海拔高度值
   入口参数:无
   出口参数:altitude //int型  2字节,当前海拔高度值
   备 注:	 返回的高度值单位为厘米,调用时再换算成带小数的以米为单位的高度值
*****************************************************************/
double BMP085_Get_Altitude()
{
	double altitude;
	//pressure=(float)pressure;				//获取气压值
	altitude=44330.0*(1-pow((double)result_UP/101325,1/5.255));  	//根据芯片手册提供的公式计算海拔高度
	//altitude*=100;	 		//转换成厘米单位的高度值,调用时再换算成带小数的高度值,提高精度
	return altitude;
}
 

         实现效果:

STM32实现气压传感器测量(BMP180)_第10张图片

         2)标准库硬件IIC实现气压值采集

STM32实现气压传感器测量(BMP180)_第11张图片

//向EEPROM写入一个字节
int  BMP180_Byte_Write(uint8_t addr,uint8_t data) 
{
	I2CTimeout = I2CT_LONG_TIMEOUT;
	while (I2C_GetFlagStatus(I2C1,  I2C_FLAG_BUSY))  //检查I2C总线是否繁忙
	{
    if((I2CTimeout--) == 0) return 13;
  } 
	//发送起始信号
	I2C_GenerateSTART(BMP180_I2C,ENABLE);
	//检测EV5事件
	I2CTimeout = I2CT_LONG_TIMEOUT;
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_MODE_SELECT)==ERROR)
	{
    if((I2CTimeout--) == 0) return 14;
  } 
	//发送设备写地址
	I2C_Send7bitAddress(BMP180_I2C,BMP180_I2C_Address,I2C_Direction_Transmitter);
	//检测EV6事件
	I2CTimeout = I2CT_LONG_TIMEOUT;
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)==ERROR)
	{
    if((I2CTimeout--) == 0) return 15;
  } 
	//发送要操作设备内部的地址
	I2C_SendData(BMP180_I2C,addr);
	I2CTimeout = I2CT_LONG_TIMEOUT;
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTING )==ERROR)
	{
    if((I2CTimeout--) == 0) return 16;
  } 
  I2C_SendData(BMP180_I2C,data);
	//检测EV8_2事件
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED )==ERROR);
	//发送停止信号
	I2C_GenerateSTOP(BMP180_I2C,ENABLE);
	return 0;
}


//向EEPROM读取多个字节
uint32_t BMP180_Read(uint8_t *data,uint8_t addr,uint16_t Num_ByteToRead)
{
	 I2CTimeout = I2CT_LONG_TIMEOUT;
  //判断IIC总线是否忙碌
  while(I2C_GetFlagStatus(BMP180_I2C, I2C_FLAG_BUSY))   
  {
    if((I2CTimeout--) == 0) return 1;
  } 
	
	I2CTimeout = I2CT_FLAG_TIMEOUT;
	//发送起始信号
	I2C_GenerateSTART(BMP180_I2C,ENABLE);
	//检测EV5事件
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_MODE_SELECT )==ERROR)
  {
        if((I2CTimeout--) == 0) return 7;
   } 
	
	I2CTimeout = I2CT_FLAG_TIMEOUT;
	//发送设备写地址
	I2C_Send7bitAddress(BMP180_I2C,BMP180_I2C_Address,I2C_Direction_Transmitter);
	//检测EV6事件等待从机应答
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED )==ERROR)
 {
        if((I2CTimeout--) == 0) return 8;
  }
  
	I2CTimeout = I2CT_FLAG_TIMEOUT;
	//发送要操作设备内部存储器的地址
	I2C_SendData(BMP180_I2C,addr);
	//检测EV8事件
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTING )==ERROR)
 {
        if((I2CTimeout--) == 0) return 9;
  }
	I2CTimeout = I2CT_FLAG_TIMEOUT;
	//发送起始信号
	I2C_GenerateSTART(BMP180_I2C,ENABLE);
	//检测EV5事件
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_MODE_SELECT )==ERROR)
	{
        if((I2CTimeout--) == 0) return 10;
   }
	I2CTimeout = I2CT_FLAG_TIMEOUT;	 
	//发送设备读地址
	I2C_Send7bitAddress(BMP180_I2C,BMP180_I2C_Address+1,I2C_Direction_Receiver);
	//检测EV6事件
	while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED )==ERROR)
	{
       if((I2CTimeout--) == 0) return 11;
   }
	 
	while(Num_ByteToRead--)
	{
		//是否是最后一个字节,若是则发送非应答信号
		if( Num_ByteToRead==0)
	 {
		 //发送非应答信号
		 I2C_AcknowledgeConfig(BMP180_I2C,DISABLE);
		 //发送停止信号
	   I2C_GenerateSTOP(BMP180_I2C,ENABLE);
	 }
	 
	 I2CTimeout = I2CT_FLAG_TIMEOUT;	 
	 //检测EV7事件
   while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED )==ERROR)
   {
       if((I2CTimeout--) == 0) return 12;
   }
	 
    *data=I2C_ReceiveData(BMP180_I2C);
	  data++; 
	 
	}
	
	//重新开启应答信号
	I2C_AcknowledgeConfig(BMP180_I2C,ENABLE);
  return 0;
}


void BMP180_Init(void)
{
	u8 msb, lsb;
  delay_ms(200);     // 1s,这里的延时很重要,上电后延时,没有错误的冗余设计
	
	BMP180_Read(&msb,0xAA,1);  
	BMP180_Read(&lsb,0xAB,1); 
	ac1 = (msb << 8)|lsb;
	BMP180_Read(&msb,0xAC,1);  
	BMP180_Read(&lsb,0xAD,1); 
	ac2 = (msb << 8)|lsb;
	BMP180_Read(&msb,0xAE,1);  
	BMP180_Read(&lsb,0xAF,1); 
	ac3 = (msb << 8)|lsb;
	BMP180_Read(&msb,0xB0,1);  
	BMP180_Read(&lsb,0xB1,1); 
	ac4 = (msb << 8)|lsb;
	BMP180_Read(&msb,0xB2,1);  
	BMP180_Read(&lsb,0xB3,1); 
	ac5 = (msb << 8)|lsb;
	BMP180_Read(&msb,0xB4,1);  
	BMP180_Read(&lsb,0xB5,1); 
	ac6 = (msb << 8)|lsb;
	BMP180_Read(&msb,0xB6,1);  
	BMP180_Read(&lsb,0xB7,1); 
	b1 = (msb << 8)|lsb;
	BMP180_Read(&msb,0xB8,1);  
	BMP180_Read(&lsb,0xB9,1); 
	b2 = (msb << 8)|lsb;
	BMP180_Read(&msb,0xBA,1);  
	BMP180_Read(&lsb,0xBB,1); 
	mb = (msb << 8)|lsb;
	BMP180_Read(&msb,0xBC,1);  
	BMP180_Read(&lsb,0xBD,1); 
	mc = (msb << 8)|lsb;
	BMP180_Read(&msb,0xBE,1);  
	BMP180_Read(&lsb,0xBF,1); 
	md = (msb << 8)|lsb;
}

//********************************************************************

u16 bmp180ReadTemp(void)
{
	u8 msb, lsb;
	//BMP180_Byte_Write(BMP180_SlaveAddress);   //发送设备地址+写信号   BMP180_SlaveAddress  0xee	气压传感器器件地址
	BMP180_Byte_Write(0xF4,0x2E);
	//BMP180_Byte_Write(0x2E);
	
	delay_ms(20);	// max time is 4.5ms
	BMP180_Read(&msb,0xF6,1);  
	BMP180_Read(&lsb,0xF7,1); 
	
	return (msb << 8)|lsb;
}
//*************************************************************

u16 bmp180ReadPressure(void)
{
	u8 msb, lsb;
	//BMP180_Byte_Write(BMP180_SlaveAddress);   //发送设备地址+写信号
	BMP180_Byte_Write(0xF4,0x34);	          // write register address
	//BMP180_Byte_Write(0x34);       	  	// write register data for pressure
	
	delay_ms(20);    	                  // max time is 4.5ms
	BMP180_Read(&msb,0xF6,1);  
	BMP180_Read(&lsb,0xF7,1); 
	return (msb << 8)|lsb;
}

实现效果:

STM32实现气压传感器测量(BMP180)_第12张图片

         3)HAL库模拟IIC实现气压值采集

STM32实现气压传感器测量(BMP180)_第13张图片

STM32实现气压传感器测量(BMP180)_第14张图片

//********************************************************************
uint16_t bmp180ReadTemp(void)
{
	int cnt = 0;
	BMP180_Start();                  //起始信号
	BMP180_SendByte(BMP180_SlaveAddress);   //发送设备地址+写信号   BMP180_SlaveAddress  0xee	气压传感器器件地址
	cnt = 0;
	while(BMP180_RecvACK())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		Delay_mms(1); 
		if(cnt>=100)return 1;
	} 
	BMP180_SendByte(0xF4);	          // write register address
	cnt = 0;
	while(BMP180_RecvACK())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		Delay_mms(1);
		if(cnt>=100)return 1;
	} 
	BMP180_SendByte(0x2E);       	// write register data for temp
	cnt = 0;
	while(BMP180_RecvACK())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		Delay_mms(1);
		if(cnt>=100)return 1;
	} 
	BMP180_Stop();                   //发送停止信号
	Delay_mms(10);	// max time is 4.5ms
	return Multiple_read(0xF6);
}
//*************************************************************
uint16_t bmp180ReadPressure(void)
{
	int cnt = 0;
	BMP180_Start();                   //起始信号
	BMP180_SendByte(BMP180_SlaveAddress);   //发送设备地址+写信号
	cnt = 0;
	while(BMP180_RecvACK())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		Delay_mms(1);
		if(cnt>=100)return 1;
	} 
	BMP180_SendByte(0xF4);	          // write register address
	cnt = 0;
	while(BMP180_RecvACK())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		Delay_mms(1);
		if(cnt>=100)return 1;
	} 
	BMP180_SendByte(0x34);       	  // write register data for pressure
	cnt = 0;
	while(BMP180_RecvACK())								//这边需要增加超时退出,避免卡死
	{
		cnt++;
		Delay_mms(1);
		if(cnt>=100)return 1;
	} 
	BMP180_Stop();                    //发送停止信号
	Delay_mms(20);    	                  // max time is 4.5ms
	return Multiple_read(0xF6);//pressure;	
}


 
//初始化BMP180,根据需要请参考pdf进行修改**************
void Init_BMP180()
{
	char str[100] = "";
	ac1 = Multiple_read(0xAA);
	ac2 = Multiple_read(0xAC);
	ac3 = Multiple_read(0xAE);
	ac4 = Multiple_read(0xB0);
	ac5 = Multiple_read(0xB2);
	ac6 = Multiple_read(0xB4);
	b1 =  Multiple_read(0xB6);
	b2 =  Multiple_read(0xB8);
	mb =  Multiple_read(0xBA);
	mc =  Multiple_read(0xBC);
	md =  Multiple_read(0xBE);
	
	sprintf(str,"ac1=%d ac2=%d ac3=%d ac4=%d ac5=%d ac6=%d b1=%d b2=%d mb=%d mc=%d md=%d\r\n",(int)ac1,(int)ac2,(int)ac3,(int)ac4,(int)ac5,(int)ac6,(int)b1,(int)b2,(int)mb,(int)mc,(int)md);
  HAL_UART_Transmit(&huart1, (uint8_t *)str, (uint8_t)strlen(str), 5000);
}
//***********************************************************************
void bmp180Convert()
{
	unsigned int ut;
	unsigned long up;
	long x1, x2, b5, b6, x3, b3, p;
	unsigned long b4, b7;
 
	ut = bmp180ReadTemp();	   // 读取温度
	up = bmp180ReadPressure();  // 读取压强    return pressure;	
	//*************
	x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
	x2 = ((long) mc << 11) / (x1 + md);
	b5 = x1 + x2;
	result_UT = ((b5 + 8) >> 4);
	//*************		
	b6 = b5 - 4000;
	                         // Calculate B3
	x1 = (b2 * (b6 * b6)>>12)>>11;
	x2 = (ac2 * b6)>>11;
	x3 = x1 + x2;
	b3 = (((((long)ac1)*4 + x3)<>2;	
	                         // Calculate B4
	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<<1)/b4;
	else
	p = (b7/b4)<<1;
	
	x1 = (p>>8) * (p>>8);
	x1 = (x1 * 3038)>>16;
	x2 = (-7357 * p)>>16;
	result_UP = p+((x1 + x2 + 3791)>>4);
}
 
 
/****************************************************************
   函数名称:BMP085_Get_Altitude			    
   函数功能:获取海拔高度值
   入口参数:无
   出口参数:altitude //int型  2字节,当前海拔高度值
   备 注:	 返回的高度值单位为厘米,调用时再换算成带小数的以米为单位的高度值
*****************************************************************/
//double BMP085_Get_Altitude()
//{
//	double altitude;
//	//pressure=(float)pressure;				//获取气压值
//	altitude=44330.0*(1-pow((double)pressure/101325,1/5.255));  	//根据芯片手册提供的公式计算海拔高度
//	//altitude*=100;	 		//转换成厘米单位的高度值,调用时再换算成带小数的高度值,提高精度
//	return altitude;
//}

 实现效果:

STM32实现气压传感器测量(BMP180)_第15张图片

        4)HAL库硬件IIC实现气压值采集

STM32实现气压传感器测量(BMP180)_第16张图片

 STM32实现气压传感器测量(BMP180)_第17张图片

//	在从设备指定内存中写数据
void BMP180_WR_CMD(uint8_t cmd,uint8_t data)
{
	//HAL_I2C_Master_Transmit(&hi2c1, BMP180_I2C_Address, &cmd,1, 0x100);
	HAL_I2C_Mem_Write(&hi2c1, BMP180_I2C_Address, cmd, I2C_MEMADD_SIZE_8BIT, &data, 1, 0x100);
	//HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
	//HAL_I2C_Mem_Write(&hi2c1 ,0x46,0x00,I2C_MEMADD_SIZE_8BIT,&cmd,1,0x100);
}

//读数据
void BMP180_READ_DATA(uint8_t cmd,uint8_t *data,uint16_t size)
{
	HAL_I2C_Master_Transmit(&hi2c1, BMP180_I2C_Address, &cmd,size, 0x100);
	HAL_I2C_Master_Receive(&hi2c1, BMP180_I2C_Address+1, data,size, 0x100);
}


/**
 @brief BH1750初始化函数
 @param 无
 @return 无
*/
void BMP180_Init(void)
{
	char str[100] = "";
	uint8_t msb, lsb;
  HAL_Delay(200);     // 1s,这里的延时很重要,上电后延时,没有错误的冗余设计
	
	BMP180_READ_DATA(0xAA,&msb,1);
	BMP180_READ_DATA(0xAB,&lsb,1);
	ac1 = (msb << 8)|lsb;
	BMP180_READ_DATA(0xAC,&msb,1);
	BMP180_READ_DATA(0xAD,&lsb,1); 
	ac2 = (msb << 8)|lsb;
	BMP180_READ_DATA(0xAE,&msb,1);
	BMP180_READ_DATA(0xAF,&lsb,1); 
	ac3 = (msb << 8)|lsb;
	BMP180_READ_DATA(0xB0,&msb,1);
	BMP180_READ_DATA(0xB1,&lsb,1); 
	ac4 = (msb << 8)|lsb;
	BMP180_READ_DATA(0xB2,&msb,1);
	BMP180_READ_DATA(0xB3,&lsb,1); 
	ac5 = (msb << 8)|lsb;
	BMP180_READ_DATA(0xB4,&msb,1);
	BMP180_READ_DATA(0xB5,&lsb,1); 
	ac6 = (msb << 8)|lsb;
	BMP180_READ_DATA(0xB6,&msb,1);
	BMP180_READ_DATA(0xB7,&lsb,1); 
	b1 = (msb << 8)|lsb;
	BMP180_READ_DATA(0xB8,&msb,1);
	BMP180_READ_DATA(0xB9,&lsb,1); 
	b2 = (msb << 8)|lsb;
	BMP180_READ_DATA(0xBA,&msb,1);
	BMP180_READ_DATA(0xBB,&lsb,1);
	mb = (msb << 8)|lsb;
	BMP180_READ_DATA(0xBC,&msb,1);
	BMP180_READ_DATA(0xBD,&lsb,1);
	mc = (msb << 8)|lsb;
	BMP180_READ_DATA(0xBE,&msb,1);
	BMP180_READ_DATA(0xBF,&lsb,1);
	md = (msb << 8)|lsb;
	
	sprintf(str,"ac1=%d ac2=%d ac3=%d ac4=%d ac5=%d ac6=%d b1=%d b2=%d mb=%d mc=%d md=%d\r\n",(int)ac1,(int)ac2,(int)ac3,(int)ac4,(int)ac5,(int)ac6,(int)b1,(int)b2,(int)mb,(int)mc,(int)md);
  HAL_UART_Transmit(&huart1, (uint8_t *)str, (uint8_t)strlen(str), 5000);
}


uint16_t bmp180ReadTemp(void)
{
	uint8_t msb, lsb;
	//发送设备地址+写信号   BMP180_SlaveAddress  0xee	气压传感器器件地址
	BMP180_WR_CMD(0xF4,0x2E);
	
	HAL_Delay(20);	// max time is 4.5ms
	BMP180_READ_DATA(0xF6,&msb,1);  
	BMP180_READ_DATA(0xF7,&lsb,1); 
	
	return (msb << 8)|lsb;
}
//*************************************************************

uint16_t bmp180ReadPressure(void)
{
	uint8_t msb, lsb;
	//发送设备地址+写信号
	BMP180_WR_CMD(0xF4,0x34);
	
	HAL_Delay(20);    	                  // max time is 4.5ms
	BMP180_READ_DATA(0xF6,&msb,1);  
	BMP180_READ_DATA(0xF7,&lsb,1); 
	return (msb << 8)|lsb;
}


//***********************************************************************

void bmp180Convert()
{
	unsigned int ut;
	unsigned long up;
	long x1, x2, b5, b6, x3, b3, p;
	unsigned long b4, b7;
 
	ut = bmp180ReadTemp();	   // 读取温度
	up = bmp180ReadPressure();  // 读取压强    return pressure;	
	//
	x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
	x2 = ((long) mc << 11) / (x1 + md);
	b5 = x1 + x2;
	result_UT = ((b5 + 8) >> 4);
	//	
	b6 = b5 - 4000;
	                         // Calculate B3
	x1 = (b2 * (b6 * b6)>>12)>>11;
	x2 = (ac2 * b6)>>11;
	x3 = x1 + x2;
	b3 = (((((long)ac1)*4 + x3)<>2;	
	                         // Calculate B4
	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<<1)/b4;
	else
	p = (b7/b4)<<1;
	
	x1 = (p>>8) * (p>>8);
	x1 = (x1 * 3038)>>16;
	x2 = (-7357 * p)>>16;
	result_UP = p+((x1 + x2 + 3791)>>4);
}

实现效果:

STM32实现气压传感器测量(BMP180)_第18张图片

资源下载链接:(20条消息) STM32实现气压传感器测量(BMP180)资源-CSDN文库

吾芯电子工作室

你可能感兴趣的:(单片机c语言,stm32,单片机,BMP180,大气压传感器)