#申请原创#
BMP085是一种可对环境温度及大气压进行检测的传感器,它体积小易于同0.96寸的LCD一起构成一个便携式的环境状态检测装置。如果再能把STM32WBA52所具有的无线通信添加进来,则可以有效地拓展其应用空间。 该装置的整体电路构成及连接如图1所示:
图1 整体构成
BMP085是一种采用I2C接口工作的器件,故占用的引脚资源极少,在使用时它与开发板的连接关系为: SCL----PB14 SDA----PB0
图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对所用引脚工作模式的配置函数为: void OUTPUT_MODE_SET(void)
{
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_0;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
在使用过程中,由于要进行输入与输出模式的切换,故定义了设置输入与输出模式的函数,其内容为: void INPUT_MODE_SET(void)
{
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_0;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Bmp085_Init(void)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_14;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
在以GPIO口模拟I2C发送字节数据时,其函数内容为: void BMP085_Send_Byte(char txd)
{
char t;
OUTPUT_MODE_SET();
SCL_Clr1();
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
SDA_Set1();
else
SDA_Clr1();
txd<<=1;
Delay_Us(2);
SCL_Set1();
Delay_Us(2);
SCL_Clr1();
Delay_Us(2);
}
}
BMP085的初始化函数为: void Init_BMP085()
{
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);
}
BMP085读取字节数据的函数为: char BMP085_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
INPUT_MODE_SET();
for(i=0;i<8;i++)
{
SCL_Clr1();
Delay_Us(2);
SCL_Set1();
receive<<=1;
if(SDA_IN) receive++;
Delay_Us(1);
}
if(!ack)
BMP085_NAck();
else
BMP085_Ack();
return receive;
}
BMP085读取温度的函数为: long bmp085ReadTemp(void)
{
BMP085_Start();
BMP085_Send_Byte(BMP085_SlaveAddress);
while(BMP085_Wait_Ack());
BMP085_Send_Byte(0xF4);
while(BMP085_Wait_Ack());
BMP085_Send_Byte(0x2E);
while(BMP085_Wait_Ack());
BMP085_Stop();
HAL_Delay(10);
return (long) Multiple_read(0xF6);
}
BMP085读取大气压的函数为: long bmp085ReadPressure(void)
{
long pressure = 0;
BMP085_Start();
BMP085_Send_Byte(BMP085_SlaveAddress);
while(BMP085_Wait_Ack()){}
BMP085_Send_Byte(0xF4);
while(BMP085_Wait_Ack()){}
BMP085_Send_Byte(0x34);
while(BMP085_Wait_Ack()){}
BMP085_Stop();
HAL_Delay(10);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
BMP085数据变换处理的函数为: void bmp085Convert()
{
unsigned int ut;
unsigned long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp085ReadTemp();
up = bmp085ReadPressure();
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature1 = ((b5 + 8) >> 4);
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
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;
pressure = p+((x1 + x2 + 3791)>>4);
}
实现图3所示显示效果的程序为: int main(void)
{
unsigned int i,k,j=0;
HAL_Init();
SystemClock_Config();
LCD_GPIO_Init();
Lcd_Init();
BACK_COLOR=RED;
LCD_Clear(RED);
LCD_ShowString(20,2,"STM32WBA52CG",YELLOW);
LCD_ShowString(20,20,"LCD & BMP085",YELLOW);
Bmp085_Init();
Init_BMP085();
HAL_Delay(2000);
LCD_ShowString(20,40,"t= C",YELLOW);
LCD_ShowString(20,58,"p= KPa",YELLOW);
while(1)
{
bmp085Convert();
LCD_ShowNum(44,40,temperature1/10,3,YELLOW);
LCD_ShowNum(44,58,pressure/100,5,YELLOW);
HAL_GPIO_TogglePin(LD2_GPIO_PORT, LD2_PIN);
HAL_Delay(500);
}
}
图3 检测结果
|