当前位置:   article > 正文

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

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通信,实物图如下:

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

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

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

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

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

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

3.模块选择

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

        ·BMP180气压传感器模块

        ·USB-TTL串口调试助手

4.编程环境

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

        keil5(编程环境)

5.模块主要参数

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

         测量流程图。

        温度和大气压测量:

        海拔测量:

6.代码实现

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

  1. //********************************************************************
  2. u16 bmp180ReadTemp(void)
  3. {
  4. int cnt = 0;
  5. IIC_Start(); //起始信号
  6. IIC_Send_Byte(BMP180_SlaveAddress); //发送设备地址+写信号 BMP180_SlaveAddress 0xee 气压传感器器件地址
  7. cnt = 0;
  8. while(IIC_Wait_Ack()) //这边需要增加超时退出,避免卡死
  9. {
  10. cnt++;
  11. delay_ms(1);
  12. if(cnt>=100)return 1;
  13. }
  14. IIC_Send_Byte(0xF4); // write register address
  15. cnt = 0;
  16. while(IIC_Wait_Ack()) //这边需要增加超时退出,避免卡死
  17. {
  18. cnt++;
  19. delay_ms(1);
  20. if(cnt>=100)return 1;
  21. }
  22. IIC_Send_Byte(0x2E); // write register data for temp
  23. cnt = 0;
  24. while(IIC_Wait_Ack()) //这边需要增加超时退出,避免卡死
  25. {
  26. cnt++;
  27. delay_ms(1);
  28. if(cnt>=100)return 1;
  29. }
  30. IIC_Stop(); //发送停止信号
  31. delay_ms(10); // max time is 4.5ms
  32. return Multiple_read(0xF6);
  33. }
  34. //*************************************************************
  35. u16 bmp180ReadPressure(void)
  36. {
  37. int cnt = 0;
  38. IIC_Start(); //起始信号
  39. IIC_Send_Byte(BMP180_SlaveAddress); //发送设备地址+写信号
  40. cnt = 0;
  41. while(IIC_Wait_Ack()) //这边需要增加超时退出,避免卡死
  42. {
  43. cnt++;
  44. delay_ms(1);
  45. if(cnt>=100)return 1;
  46. }
  47. IIC_Send_Byte(0xF4); // write register address
  48. cnt = 0;
  49. while(IIC_Wait_Ack()) //这边需要增加超时退出,避免卡死
  50. {
  51. cnt++;
  52. delay_ms(1);
  53. if(cnt>=100)return 1;
  54. }
  55. IIC_Send_Byte(0x34); // write register data for pressure
  56. cnt = 0;
  57. while(IIC_Wait_Ack()) //这边需要增加超时退出,避免卡死
  58. {
  59. cnt++;
  60. delay_ms(1);
  61. if(cnt>=100)return 1;
  62. }
  63. IIC_Stop(); //发送停止信号
  64. delay_ms(20); // max time is 4.5ms
  65. return Multiple_read(0xF6);//pressure;
  66. }
  67. //**************************************************************
  68. //初始化BMP085,根据需要请参考pdf进行修改**************
  69. void Init_BMP180()
  70. {
  71. IIC_Init();
  72. ac1 = Multiple_read(0xAA);
  73. ac2 = Multiple_read(0xAC);
  74. ac3 = Multiple_read(0xAE);
  75. ac4 = Multiple_read(0xB0);
  76. ac5 = Multiple_read(0xB2);
  77. ac6 = Multiple_read(0xB4);
  78. b1 = Multiple_read(0xB6);
  79. b2 = Multiple_read(0xB8);
  80. mb = Multiple_read(0xBA);
  81. mc = Multiple_read(0xBC);
  82. md = Multiple_read(0xBE);
  83. }
  84. //***********************************************************************
  85. void bmp180Convert()
  86. {
  87. unsigned int ut;
  88. unsigned long up;
  89. long x1, x2, b5, b6, x3, b3, p;
  90. unsigned long b4, b7;
  91. ut = bmp180ReadTemp(); // 读取温度
  92. up = bmp180ReadPressure(); // 读取压强 return pressure;
  93. //*************
  94. x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  95. x2 = ((long) mc << 11) / (x1 + md);
  96. b5 = x1 + x2;
  97. result_UT = ((b5 + 8) >> 4);
  98. //*************
  99. b6 = b5 - 4000;
  100. // Calculate B3
  101. x1 = (b2 * (b6 * b6)>>12)>>11;
  102. x2 = (ac2 * b6)>>11;
  103. x3 = x1 + x2;
  104. b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  105. // Calculate B4
  106. x1 = (ac3 * b6)>>13;
  107. x2 = (b1 * ((b6 * b6)>>12))>>16;
  108. x3 = ((x1 + x2) + 2)>>2;
  109. b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  110. b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  111. if (b7 < 0x80000000)
  112. p = (b7<<1)/b4;
  113. else
  114. p = (b7/b4)<<1;
  115. x1 = (p>>8) * (p>>8);
  116. x1 = (x1 * 3038)>>16;
  117. x2 = (-7357 * p)>>16;
  118. result_UP = p+((x1 + x2 + 3791)>>4);
  119. }
  120. /****************************************************************
  121.    函数名称:BMP085_Get_Altitude
  122.    函数功能:获取海拔高度值
  123.    入口参数:无
  124.    出口参数:altitude //int型 2字节,当前海拔高度值
  125.    备 注: 返回的高度值单位为厘米,调用时再换算成带小数的以米为单位的高度值
  126. *****************************************************************/
  127. double BMP085_Get_Altitude()
  128. {
  129. double altitude;
  130. //pressure=(float)pressure; //获取气压值
  131. altitude=44330.0*(1-pow((double)result_UP/101325,1/5.255)); //根据芯片手册提供的公式计算海拔高度
  132. //altitude*=100; //转换成厘米单位的高度值,调用时再换算成带小数的高度值,提高精度
  133. return altitude;
  134. }

         实现效果:

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

  1. //向EEPROM写入一个字节
  2. int BMP180_Byte_Write(uint8_t addr,uint8_t data)
  3. {
  4. I2CTimeout = I2CT_LONG_TIMEOUT;
  5. while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)) //检查I2C总线是否繁忙
  6. {
  7. if((I2CTimeout--) == 0) return 13;
  8. }
  9. //发送起始信号
  10. I2C_GenerateSTART(BMP180_I2C,ENABLE);
  11. //检测EV5事件
  12. I2CTimeout = I2CT_LONG_TIMEOUT;
  13. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_MODE_SELECT)==ERROR)
  14. {
  15. if((I2CTimeout--) == 0) return 14;
  16. }
  17. //发送设备写地址
  18. I2C_Send7bitAddress(BMP180_I2C,BMP180_I2C_Address,I2C_Direction_Transmitter);
  19. //检测EV6事件
  20. I2CTimeout = I2CT_LONG_TIMEOUT;
  21. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)==ERROR)
  22. {
  23. if((I2CTimeout--) == 0) return 15;
  24. }
  25. //发送要操作设备内部的地址
  26. I2C_SendData(BMP180_I2C,addr);
  27. I2CTimeout = I2CT_LONG_TIMEOUT;
  28. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTING )==ERROR)
  29. {
  30. if((I2CTimeout--) == 0) return 16;
  31. }
  32. I2C_SendData(BMP180_I2C,data);
  33. //检测EV8_2事件
  34. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED )==ERROR);
  35. //发送停止信号
  36. I2C_GenerateSTOP(BMP180_I2C,ENABLE);
  37. return 0;
  38. }
  39. //向EEPROM读取多个字节
  40. uint32_t BMP180_Read(uint8_t *data,uint8_t addr,uint16_t Num_ByteToRead)
  41. {
  42. I2CTimeout = I2CT_LONG_TIMEOUT;
  43. //判断IIC总线是否忙碌
  44. while(I2C_GetFlagStatus(BMP180_I2C, I2C_FLAG_BUSY))
  45. {
  46. if((I2CTimeout--) == 0) return 1;
  47. }
  48. I2CTimeout = I2CT_FLAG_TIMEOUT;
  49. //发送起始信号
  50. I2C_GenerateSTART(BMP180_I2C,ENABLE);
  51. //检测EV5事件
  52. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_MODE_SELECT )==ERROR)
  53. {
  54. if((I2CTimeout--) == 0) return 7;
  55. }
  56. I2CTimeout = I2CT_FLAG_TIMEOUT;
  57. //发送设备写地址
  58. I2C_Send7bitAddress(BMP180_I2C,BMP180_I2C_Address,I2C_Direction_Transmitter);
  59. //检测EV6事件等待从机应答
  60. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED )==ERROR)
  61. {
  62. if((I2CTimeout--) == 0) return 8;
  63. }
  64. I2CTimeout = I2CT_FLAG_TIMEOUT;
  65. //发送要操作设备内部存储器的地址
  66. I2C_SendData(BMP180_I2C,addr);
  67. //检测EV8事件
  68. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTING )==ERROR)
  69. {
  70. if((I2CTimeout--) == 0) return 9;
  71. }
  72. I2CTimeout = I2CT_FLAG_TIMEOUT;
  73. //发送起始信号
  74. I2C_GenerateSTART(BMP180_I2C,ENABLE);
  75. //检测EV5事件
  76. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_MODE_SELECT )==ERROR)
  77. {
  78. if((I2CTimeout--) == 0) return 10;
  79. }
  80. I2CTimeout = I2CT_FLAG_TIMEOUT;
  81. //发送设备读地址
  82. I2C_Send7bitAddress(BMP180_I2C,BMP180_I2C_Address+1,I2C_Direction_Receiver);
  83. //检测EV6事件
  84. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED )==ERROR)
  85. {
  86. if((I2CTimeout--) == 0) return 11;
  87. }
  88. while(Num_ByteToRead--)
  89. {
  90. //是否是最后一个字节,若是则发送非应答信号
  91. if( Num_ByteToRead==0)
  92. {
  93. //发送非应答信号
  94. I2C_AcknowledgeConfig(BMP180_I2C,DISABLE);
  95. //发送停止信号
  96. I2C_GenerateSTOP(BMP180_I2C,ENABLE);
  97. }
  98. I2CTimeout = I2CT_FLAG_TIMEOUT;
  99. //检测EV7事件
  100. while( I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED )==ERROR)
  101. {
  102. if((I2CTimeout--) == 0) return 12;
  103. }
  104. *data=I2C_ReceiveData(BMP180_I2C);
  105. data++;
  106. }
  107. //重新开启应答信号
  108. I2C_AcknowledgeConfig(BMP180_I2C,ENABLE);
  109. return 0;
  110. }
  111. void BMP180_Init(void)
  112. {
  113. u8 msb, lsb;
  114. delay_ms(200); // 1s,这里的延时很重要,上电后延时,没有错误的冗余设计
  115. BMP180_Read(&msb,0xAA,1);
  116. BMP180_Read(&lsb,0xAB,1);
  117. ac1 = (msb << 8)|lsb;
  118. BMP180_Read(&msb,0xAC,1);
  119. BMP180_Read(&lsb,0xAD,1);
  120. ac2 = (msb << 8)|lsb;
  121. BMP180_Read(&msb,0xAE,1);
  122. BMP180_Read(&lsb,0xAF,1);
  123. ac3 = (msb << 8)|lsb;
  124. BMP180_Read(&msb,0xB0,1);
  125. BMP180_Read(&lsb,0xB1,1);
  126. ac4 = (msb << 8)|lsb;
  127. BMP180_Read(&msb,0xB2,1);
  128. BMP180_Read(&lsb,0xB3,1);
  129. ac5 = (msb << 8)|lsb;
  130. BMP180_Read(&msb,0xB4,1);
  131. BMP180_Read(&lsb,0xB5,1);
  132. ac6 = (msb << 8)|lsb;
  133. BMP180_Read(&msb,0xB6,1);
  134. BMP180_Read(&lsb,0xB7,1);
  135. b1 = (msb << 8)|lsb;
  136. BMP180_Read(&msb,0xB8,1);
  137. BMP180_Read(&lsb,0xB9,1);
  138. b2 = (msb << 8)|lsb;
  139. BMP180_Read(&msb,0xBA,1);
  140. BMP180_Read(&lsb,0xBB,1);
  141. mb = (msb << 8)|lsb;
  142. BMP180_Read(&msb,0xBC,1);
  143. BMP180_Read(&lsb,0xBD,1);
  144. mc = (msb << 8)|lsb;
  145. BMP180_Read(&msb,0xBE,1);
  146. BMP180_Read(&lsb,0xBF,1);
  147. md = (msb << 8)|lsb;
  148. }
  149. //********************************************************************
  150. u16 bmp180ReadTemp(void)
  151. {
  152. u8 msb, lsb;
  153. //BMP180_Byte_Write(BMP180_SlaveAddress); //发送设备地址+写信号 BMP180_SlaveAddress 0xee 气压传感器器件地址
  154. BMP180_Byte_Write(0xF4,0x2E);
  155. //BMP180_Byte_Write(0x2E);
  156. delay_ms(20); // max time is 4.5ms
  157. BMP180_Read(&msb,0xF6,1);
  158. BMP180_Read(&lsb,0xF7,1);
  159. return (msb << 8)|lsb;
  160. }
  161. //*************************************************************
  162. u16 bmp180ReadPressure(void)
  163. {
  164. u8 msb, lsb;
  165. //BMP180_Byte_Write(BMP180_SlaveAddress); //发送设备地址+写信号
  166. BMP180_Byte_Write(0xF4,0x34); // write register address
  167. //BMP180_Byte_Write(0x34); // write register data for pressure
  168. delay_ms(20); // max time is 4.5ms
  169. BMP180_Read(&msb,0xF6,1);
  170. BMP180_Read(&lsb,0xF7,1);
  171. return (msb << 8)|lsb;
  172. }

实现效果:

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

  1. //********************************************************************
  2. uint16_t bmp180ReadTemp(void)
  3. {
  4. int cnt = 0;
  5. BMP180_Start(); //起始信号
  6. BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+写信号 BMP180_SlaveAddress 0xee 气压传感器器件地址
  7. cnt = 0;
  8. while(BMP180_RecvACK()) //这边需要增加超时退出,避免卡死
  9. {
  10. cnt++;
  11. Delay_mms(1);
  12. if(cnt>=100)return 1;
  13. }
  14. BMP180_SendByte(0xF4); // write register address
  15. cnt = 0;
  16. while(BMP180_RecvACK()) //这边需要增加超时退出,避免卡死
  17. {
  18. cnt++;
  19. Delay_mms(1);
  20. if(cnt>=100)return 1;
  21. }
  22. BMP180_SendByte(0x2E); // write register data for temp
  23. cnt = 0;
  24. while(BMP180_RecvACK()) //这边需要增加超时退出,避免卡死
  25. {
  26. cnt++;
  27. Delay_mms(1);
  28. if(cnt>=100)return 1;
  29. }
  30. BMP180_Stop(); //发送停止信号
  31. Delay_mms(10); // max time is 4.5ms
  32. return Multiple_read(0xF6);
  33. }
  34. //*************************************************************
  35. uint16_t bmp180ReadPressure(void)
  36. {
  37. int cnt = 0;
  38. BMP180_Start(); //起始信号
  39. BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+写信号
  40. cnt = 0;
  41. while(BMP180_RecvACK()) //这边需要增加超时退出,避免卡死
  42. {
  43. cnt++;
  44. Delay_mms(1);
  45. if(cnt>=100)return 1;
  46. }
  47. BMP180_SendByte(0xF4); // write register address
  48. cnt = 0;
  49. while(BMP180_RecvACK()) //这边需要增加超时退出,避免卡死
  50. {
  51. cnt++;
  52. Delay_mms(1);
  53. if(cnt>=100)return 1;
  54. }
  55. BMP180_SendByte(0x34); // write register data for pressure
  56. cnt = 0;
  57. while(BMP180_RecvACK()) //这边需要增加超时退出,避免卡死
  58. {
  59. cnt++;
  60. Delay_mms(1);
  61. if(cnt>=100)return 1;
  62. }
  63. BMP180_Stop(); //发送停止信号
  64. Delay_mms(20); // max time is 4.5ms
  65. return Multiple_read(0xF6);//pressure;
  66. }
  67. //初始化BMP180,根据需要请参考pdf进行修改**************
  68. void Init_BMP180()
  69. {
  70. char str[100] = "";
  71. ac1 = Multiple_read(0xAA);
  72. ac2 = Multiple_read(0xAC);
  73. ac3 = Multiple_read(0xAE);
  74. ac4 = Multiple_read(0xB0);
  75. ac5 = Multiple_read(0xB2);
  76. ac6 = Multiple_read(0xB4);
  77. b1 = Multiple_read(0xB6);
  78. b2 = Multiple_read(0xB8);
  79. mb = Multiple_read(0xBA);
  80. mc = Multiple_read(0xBC);
  81. md = Multiple_read(0xBE);
  82. 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);
  83. HAL_UART_Transmit(&huart1, (uint8_t *)str, (uint8_t)strlen(str), 5000);
  84. }
  85. //***********************************************************************
  86. void bmp180Convert()
  87. {
  88. unsigned int ut;
  89. unsigned long up;
  90. long x1, x2, b5, b6, x3, b3, p;
  91. unsigned long b4, b7;
  92. ut = bmp180ReadTemp(); // 读取温度
  93. up = bmp180ReadPressure(); // 读取压强 return pressure;
  94. //*************
  95. x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  96. x2 = ((long) mc << 11) / (x1 + md);
  97. b5 = x1 + x2;
  98. result_UT = ((b5 + 8) >> 4);
  99. //*************
  100. b6 = b5 - 4000;
  101. // Calculate B3
  102. x1 = (b2 * (b6 * b6)>>12)>>11;
  103. x2 = (ac2 * b6)>>11;
  104. x3 = x1 + x2;
  105. b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  106. // Calculate B4
  107. x1 = (ac3 * b6)>>13;
  108. x2 = (b1 * ((b6 * b6)>>12))>>16;
  109. x3 = ((x1 + x2) + 2)>>2;
  110. b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  111. b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  112. if (b7 < 0x80000000)
  113. p = (b7<<1)/b4;
  114. else
  115. p = (b7/b4)<<1;
  116. x1 = (p>>8) * (p>>8);
  117. x1 = (x1 * 3038)>>16;
  118. x2 = (-7357 * p)>>16;
  119. result_UP = p+((x1 + x2 + 3791)>>4);
  120. }
  121. /****************************************************************
  122.    函数名称:BMP085_Get_Altitude
  123.    函数功能:获取海拔高度值
  124.    入口参数:无
  125.    出口参数:altitude //int型 2字节,当前海拔高度值
  126.    备 注: 返回的高度值单位为厘米,调用时再换算成带小数的以米为单位的高度值
  127. *****************************************************************/
  128. //double BMP085_Get_Altitude()
  129. //{
  130. // double altitude;
  131. // //pressure=(float)pressure; //获取气压值
  132. // altitude=44330.0*(1-pow((double)pressure/101325,1/5.255)); //根据芯片手册提供的公式计算海拔高度
  133. // //altitude*=100; //转换成厘米单位的高度值,调用时再换算成带小数的高度值,提高精度
  134. // return altitude;
  135. //}

 实现效果:

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

 

  1. // 在从设备指定内存中写数据
  2. void BMP180_WR_CMD(uint8_t cmd,uint8_t data)
  3. {
  4. //HAL_I2C_Master_Transmit(&hi2c1, BMP180_I2C_Address, &cmd,1, 0x100);
  5. HAL_I2C_Mem_Write(&hi2c1, BMP180_I2C_Address, cmd, I2C_MEMADD_SIZE_8BIT, &data, 1, 0x100);
  6. //HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  7. //HAL_I2C_Mem_Write(&hi2c1 ,0x46,0x00,I2C_MEMADD_SIZE_8BIT,&cmd,1,0x100);
  8. }
  9. //读数据
  10. void BMP180_READ_DATA(uint8_t cmd,uint8_t *data,uint16_t size)
  11. {
  12. HAL_I2C_Master_Transmit(&hi2c1, BMP180_I2C_Address, &cmd,size, 0x100);
  13. HAL_I2C_Master_Receive(&hi2c1, BMP180_I2C_Address+1, data,size, 0x100);
  14. }
  15. /**
  16. @brief BH1750初始化函数
  17. @param 无
  18. @return
  19. */
  20. void BMP180_Init(void)
  21. {
  22. char str[100] = "";
  23. uint8_t msb, lsb;
  24. HAL_Delay(200); // 1s,这里的延时很重要,上电后延时,没有错误的冗余设计
  25. BMP180_READ_DATA(0xAA,&msb,1);
  26. BMP180_READ_DATA(0xAB,&lsb,1);
  27. ac1 = (msb << 8)|lsb;
  28. BMP180_READ_DATA(0xAC,&msb,1);
  29. BMP180_READ_DATA(0xAD,&lsb,1);
  30. ac2 = (msb << 8)|lsb;
  31. BMP180_READ_DATA(0xAE,&msb,1);
  32. BMP180_READ_DATA(0xAF,&lsb,1);
  33. ac3 = (msb << 8)|lsb;
  34. BMP180_READ_DATA(0xB0,&msb,1);
  35. BMP180_READ_DATA(0xB1,&lsb,1);
  36. ac4 = (msb << 8)|lsb;
  37. BMP180_READ_DATA(0xB2,&msb,1);
  38. BMP180_READ_DATA(0xB3,&lsb,1);
  39. ac5 = (msb << 8)|lsb;
  40. BMP180_READ_DATA(0xB4,&msb,1);
  41. BMP180_READ_DATA(0xB5,&lsb,1);
  42. ac6 = (msb << 8)|lsb;
  43. BMP180_READ_DATA(0xB6,&msb,1);
  44. BMP180_READ_DATA(0xB7,&lsb,1);
  45. b1 = (msb << 8)|lsb;
  46. BMP180_READ_DATA(0xB8,&msb,1);
  47. BMP180_READ_DATA(0xB9,&lsb,1);
  48. b2 = (msb << 8)|lsb;
  49. BMP180_READ_DATA(0xBA,&msb,1);
  50. BMP180_READ_DATA(0xBB,&lsb,1);
  51. mb = (msb << 8)|lsb;
  52. BMP180_READ_DATA(0xBC,&msb,1);
  53. BMP180_READ_DATA(0xBD,&lsb,1);
  54. mc = (msb << 8)|lsb;
  55. BMP180_READ_DATA(0xBE,&msb,1);
  56. BMP180_READ_DATA(0xBF,&lsb,1);
  57. md = (msb << 8)|lsb;
  58. 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);
  59. HAL_UART_Transmit(&huart1, (uint8_t *)str, (uint8_t)strlen(str), 5000);
  60. }
  61. uint16_t bmp180ReadTemp(void)
  62. {
  63. uint8_t msb, lsb;
  64. //发送设备地址+写信号 BMP180_SlaveAddress 0xee 气压传感器器件地址
  65. BMP180_WR_CMD(0xF4,0x2E);
  66. HAL_Delay(20); // max time is 4.5ms
  67. BMP180_READ_DATA(0xF6,&msb,1);
  68. BMP180_READ_DATA(0xF7,&lsb,1);
  69. return (msb << 8)|lsb;
  70. }
  71. //*************************************************************
  72. uint16_t bmp180ReadPressure(void)
  73. {
  74. uint8_t msb, lsb;
  75. //发送设备地址+写信号
  76. BMP180_WR_CMD(0xF4,0x34);
  77. HAL_Delay(20); // max time is 4.5ms
  78. BMP180_READ_DATA(0xF6,&msb,1);
  79. BMP180_READ_DATA(0xF7,&lsb,1);
  80. return (msb << 8)|lsb;
  81. }
  82. //***********************************************************************
  83. void bmp180Convert()
  84. {
  85. unsigned int ut;
  86. unsigned long up;
  87. long x1, x2, b5, b6, x3, b3, p;
  88. unsigned long b4, b7;
  89. ut = bmp180ReadTemp(); // 读取温度
  90. up = bmp180ReadPressure(); // 读取压强 return pressure;
  91. //
  92. x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  93. x2 = ((long) mc << 11) / (x1 + md);
  94. b5 = x1 + x2;
  95. result_UT = ((b5 + 8) >> 4);
  96. //
  97. b6 = b5 - 4000;
  98. // Calculate B3
  99. x1 = (b2 * (b6 * b6)>>12)>>11;
  100. x2 = (ac2 * b6)>>11;
  101. x3 = x1 + x2;
  102. b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  103. // Calculate B4
  104. x1 = (ac3 * b6)>>13;
  105. x2 = (b1 * ((b6 * b6)>>12))>>16;
  106. x3 = ((x1 + x2) + 2)>>2;
  107. b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  108. b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  109. if (b7 < 0x80000000)
  110. p = (b7<<1)/b4;
  111. else
  112. p = (b7/b4)<<1;
  113. x1 = (p>>8) * (p>>8);
  114. x1 = (x1 * 3038)>>16;
  115. x2 = (-7357 * p)>>16;
  116. result_UP = p+((x1 + x2 + 3791)>>4);
  117. }

实现效果:

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

吾芯电子工作室

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/524297
推荐阅读
相关标签
  

闽ICP备14008679号