BMP085是一种用于温度、大气压检测的传感器,它采用I2C接口的方式来工作,可借助板载的I2C接口来直接进行插接,所使用的接口为J9,见图2所示。
图1传感器
图2 传感器接口
BMP085与开发板的连接关系为:
SCL---PB6
SDA---PB7
为便于引脚来输出高低电平及读取引脚的状态,为其引脚所作的相应定义为:
#define SCL_Set1() GPIO_WritePin(CW_GPIOB,
GPIO_PIN_6,GPIO_Pin_SET)
#define SCL_Clr1() GPIO_WritePin(CW_GPIOB,
GPIO_PIN_6,GPIO_Pin_RESET)
#define SDA_Set1() GPIO_WritePin(CW_GPIOB,
GPIO_PIN_7,GPIO_Pin_SET)
#define SDA_Clr1() GPIO_WritePin(CW_GPIOB, GPIO_PIN_7,GPIO_Pin_RESET)
#define SDA_IN GPIO_ReadPin(CW_GPIOB, GPIO_PIN_7)
配置所用引脚工作模式的函数为:
void BMP085_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.IT = GPIO_IT_NONE;
GPIO_InitStruct.Mode= GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pins = GPIO_PIN_6| GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_Init(CW_GPIOB,&GPIO_InitStruct);
}
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);
}
以GPIO口实现字节数据发送的函数为:
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;
SCL_Set1();
SCL_Clr1();
}
}
以GPIO口实现字节数据接收的函数为:
char BMP085_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
INPUT_MODE_SET();
for(i=0;i<8;i++)
{
SCL_Clr1();
SCL_Set1();
receive<<=1;
if(SDA_IN) receive++;
}
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();
Delay(0xFFF);
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();
Delay(0xFFF);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
将检测值转化为实际物理单位的函数为:
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);
}
在TFT显示屏的配合下,实现温度与大气压检测的主程序为:
int32_t main(void)
{
RCC_HSI_Enable(RCC_HSIOSC_DIV6);
Lcd_Init();
Lcd_Clear(RED);
Gui_DrawFont_GBK16(20,20,GRAY0,RED,"BMP085
Test");
Gui_DrawFont_GBK16(20,60,GRAY0,RED,"Temp:(C)");
Gui_DrawFont_GBK16(20,100,GRAY0,RED,"Pres:(KP)");
Gui_DrawFont_GBK16(20,140,GRAY0,RED,"jinglixixi");
Gui_DrawLine(0,136,127,136,GRAY0);
BMP085_Init();
Init_BMP085();
Delay(0xFFFF);
Delay(0xFFFF);
while(1)
{
bmp085Convert();
ShowNum(20,80,GRAY0,RED,temperature1/10,3);
ShowNum(20,120,GRAY0,RED,pressure/100,4);
Delay(0xFFFF);
Delay(0xFFFF);
}
}
经程序的编译和下载,其运行效果如图3所示。至此,就实现了对所处环境下的温度与大气压的检测功能。
图3 检测效果