[STM32WBA] 【STM32WBA52CG测评】+环境温度及大气压的状态检测与显示

[复制链接]
1565|1
 楼主| jinglixixi 发表于 2023-7-21 10:47 | 显示全部楼层 |阅读模式
#申请原创#

BMP085是一种可对环境温度及大气压进行检测的传感器,它体积小易于同0.96寸的LCD一起构成一个便携式的环境状态检测装置。如果再能把STM32WBA52所具有的无线通信添加进来,则可以有效地拓展其应用空间。
该装置的整体电路构成及连接如图1所示:

1.jpg
1  整体构成

BMP085是一种采用I2C接口工作的器件,故占用的引脚资源极少,在使用时它与开发板的连接关系为:
SCL----PB14
SDA----PB0

2.jpg
2  Arduino接口
所用引脚输出高低电平的定义为:
#define SCL_Set1()        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14,GPIO_PIN_SET)
#define SCL_Clr1()        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14,GPIO_PIN_RESET)
#define SDA_Set1()        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0,GPIO_PIN_SET)
#define SDA_Clr1()        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0,GPIO_PIN_RESET)
为读取数据所作的相应定义为:
#define SDA_IN             HAL_GPIO_ReadPin(GPIOB,  GPIO_PIN_0)
BMP085对所用引脚工作模式的配置函数为:
  1. void OUTPUT_MODE_SET(void)
  2. {
  3.         GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  4.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  5.         GPIO_InitStruct.Pin = GPIO_PIN_0;
  6.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  7. }

在使用过程中,由于要进行输入与输出模式的切换,故定义了设置输入与输出模式的函数,其内容为:
  1. void INPUT_MODE_SET(void)
  2. {
  3.         GPIO_InitStruct.Mode  = GPIO_MODE_INPUT;
  4.     GPIO_InitStruct.Pull  = GPIO_PULLUP;
  5.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  6.         GPIO_InitStruct.Pin = GPIO_PIN_0;
  7.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  8. }

  9. void Bmp085_Init(void)
  10. {
  11.         __HAL_RCC_GPIOB_CLK_ENABLE();
  12.         GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  13.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  14.         GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_14;
  15.     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  16. }

在以GPIO口模拟I2C发送字节数据时,其函数内容为:
  1. void BMP085_Send_Byte(char txd)
  2. {
  3.     char t;
  4.     OUTPUT_MODE_SET();
  5.     SCL_Clr1();
  6.     for(t=0;t<8;t++)
  7.     {
  8.         if((txd&0x80)>>7)
  9.             SDA_Set1();
  10.         else
  11.             SDA_Clr1();
  12.         txd<<=1;
  13.         Delay_Us(2);
  14.         SCL_Set1();
  15.         Delay_Us(2);
  16.         SCL_Clr1();
  17.         Delay_Us(2);
  18.     }
  19. }

BMP085的初始化函数为:
  1. void Init_BMP085()
  2. {
  3.          ac1 = Multiple_read(0xAA);
  4.          ac2 = Multiple_read(0xAC);
  5.          ac3 = Multiple_read(0xAE);
  6.          ac4 = Multiple_read(0xB0);
  7.          ac5 = Multiple_read(0xB2);
  8.          ac6 = Multiple_read(0xB4);
  9.          b1 =  Multiple_read(0xB6);
  10.          b2 =  Multiple_read(0xB8);
  11.          mb =  Multiple_read(0xBA);
  12.          mc =  Multiple_read(0xBC);
  13.          md =  Multiple_read(0xBE);
  14. }

BMP085读取字节数据的函数为:
  1. char BMP085_Read_Byte(unsigned char ack)
  2. {
  3.       unsigned char i,receive=0;
  4.       INPUT_MODE_SET();
  5.       for(i=0;i<8;i++)
  6.       {
  7.         SCL_Clr1();
  8.         Delay_Us(2);
  9.         SCL_Set1();
  10.         receive<<=1;
  11.         if(SDA_IN) receive++;
  12.         Delay_Us(1);
  13.     }
  14.     if(!ack)
  15.         BMP085_NAck();
  16.     else
  17.         BMP085_Ack();
  18.     return receive;
  19. }

BMP085读取温度的函数为:
  1. long bmp085ReadTemp(void)
  2. {
  3.     BMP085_Start();
  4.     BMP085_Send_Byte(BMP085_SlaveAddress);
  5.     while(BMP085_Wait_Ack());
  6.     BMP085_Send_Byte(0xF4);
  7.     while(BMP085_Wait_Ack());
  8.     BMP085_Send_Byte(0x2E);
  9.     while(BMP085_Wait_Ack());
  10.     BMP085_Stop();
  11.     HAL_Delay(10);
  12.     return (long) Multiple_read(0xF6);
  13. }

BMP085读取大气压的函数为:
  1. long bmp085ReadPressure(void)
  2. {
  3.     long pressure = 0;
  4.     BMP085_Start();
  5.     BMP085_Send_Byte(BMP085_SlaveAddress);
  6.     while(BMP085_Wait_Ack()){}
  7.     BMP085_Send_Byte(0xF4);
  8.     while(BMP085_Wait_Ack()){}
  9.     BMP085_Send_Byte(0x34);
  10.     while(BMP085_Wait_Ack()){}
  11.     BMP085_Stop();
  12.     HAL_Delay(10);
  13.     pressure = Multiple_read(0xF6);
  14.     pressure &= 0x0000FFFF;
  15.     return pressure;
  16. }

BMP085数据变换处理的函数为:
  1. void bmp085Convert()
  2. {
  3.       unsigned int ut;
  4.       unsigned long up;
  5.       long x1, x2, b5, b6, x3, b3, p;
  6.       unsigned long b4, b7;
  7.       ut = bmp085ReadTemp();
  8.       up = bmp085ReadPressure();
  9.       x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  10.       x2 = ((long) mc << 11) / (x1 + md);
  11.       b5 = x1 + x2;
  12.       temperature1 = ((b5 + 8) >> 4);
  13.       b6 = b5 - 4000;
  14.       x1 = (b2 * (b6 * b6)>>12)>>11;
  15.       x2 = (ac2 * b6)>>11;
  16.       x3 = x1 + x2;
  17.       b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  18.       x1 = (ac3 * b6)>>13;
  19.       x2 = (b1 * ((b6 * b6)>>12))>>16;
  20.       x3 = ((x1 + x2) + 2)>>2;
  21.       b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  22.       b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  23.       if (b7 < 0x80000000)
  24.         p = (b7<<1)/b4;
  25.       else
  26.         p = (b7/b4)<<1;
  27.       x1 = (p>>8) * (p>>8);
  28.       x1 = (x1 * 3038)>>16;
  29.       x2 = (-7357 * p)>>16;
  30.       pressure = p+((x1 + x2 + 3791)>>4);
  31. }

实现图3所示显示效果的程序为:
  1. int main(void)
  2. {
  3.     unsigned int i,k,j=0;
  4.     HAL_Init();
  5.     SystemClock_Config();
  6.     LCD_GPIO_Init();
  7.     Lcd_Init();
  8.         BACK_COLOR=RED;
  9.         LCD_Clear(RED);
  10.         LCD_ShowString(20,2,"STM32WBA52CG",YELLOW);
  11.         LCD_ShowString(20,20,"LCD & BMP085",YELLOW);
  12.         Bmp085_Init();
  13.         Init_BMP085();
  14.         HAL_Delay(2000);
  15.         LCD_ShowString(20,40,"t=     C",YELLOW);
  16.         LCD_ShowString(20,58,"p=       KPa",YELLOW);
  17.     while(1)
  18.     {
  19.                           bmp085Convert();
  20.                           LCD_ShowNum(44,40,temperature1/10,3,YELLOW);
  21.                           LCD_ShowNum(44,58,pressure/100,5,YELLOW);
  22.                           HAL_GPIO_TogglePin(LD2_GPIO_PORT, LD2_PIN);
  23.                           HAL_Delay(500);
  24.     }
  25. }

3.jpg
3  检测结果

gejigeji521 发表于 2024-1-12 16:59 | 显示全部楼层
通过外设接口读取传感器,显示。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

525

主题

2963

帖子

39

粉丝
快速回复 在线客服 返回列表 返回顶部