BMP085是一款数字式传感器,可直接获得环境的温度及大气压检测值,故使用起来十分好用,将它与OLED屏相配合即可构成一个小巧的温湿度大气压检测器。
BMP085 和OLED屏与开发板的连接关系如下:
OLED_CLK ---PE3
OLED_DIN ---PE3
BMP085_CLK---PE5
BMP085_DIN ---PE5
为模拟I2C实现高低电平的输出其定义的语句为:
#define SCL_Clr() GPIOE->PBC = GPIO_PIN_3
#define SCL_Set() GPIOE->PBSC = GPIO_PIN_3
#define SDA_Clr() GPIOE->PBC = GPIO_PIN_5
#define SDA_Set() GPIOE->PBSC = GPIO_PIN_5
BMP085进行输入与输出模式设置的函数分别为:
- void IIC_INPUT_MODE_SET()
- {
- GPIO_InitType GPIO_InitStructure;
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
- GPIO_InitStructure.Pin = GPIO_PIN_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
- }
-
-
- void IIC_OUTPUT_MODE_SET()
- {
- GPIO_InitType GPIO_InitStructure;
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
- GPIO_InitStructure.Pin = GPIO_PIN_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
- }
BMP085的初始化函数为:
- void BMP085_Init(void)
- {
- GPIO_InitType GPIO_InitStructure;
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
- GPIO_InitStructure.Pin = GPIO_PIN_3|GPIO_PIN_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
- }
BMP085的收发字节数据的函数分别为:
- void BMP085_Send_Byte(char txd)
- {
- char t;
- IIC_OUTPUT_MODE_SET();
- SCL_Clr();
- for(t=0;t<8;t++)
- {
- if((txd&0x80)>>7)
- SDA_Set();
- else
- SDA_Clr();
- txd<<=1;
- SysTick_Delay_Us (2);
- SCL_Set();
- SysTick_Delay_Us (2);
- SCL_Clr();
- SysTick_Delay_Us (2);
- }
- }
-
- char BMP085_Read_Byte(unsigned char ack)
- {
- unsigned char i,receive=0;
- IIC_INPUT_MODE_SET();
- for(i=0;i<8;i++)
- {
- SCL_Clr();
- SysTick_Delay_Us (2);
- SCL_Set();
- receive<<=1;
- if(IIC_SDA_IN) receive++;
- SysTick_Delay_Us (1);
- }
- if(!ack)
- BMP085_NAck();
- else
- BMP085_Ack();
- return receive;
- }
-
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;
- temperature = ((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);
- }
实现温度、大气压检测及显示的主程序为:
- int main(void)
- {
- LedInit(PORT_GROUP1, LED5_PIN);
- BMP085_Init();
- Init_BMP085();
- OLEDInit();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(20,0,"N32G457 TEST",16);
- OLED_ShowString(20,2,"OLED & BMP085",16);
- OLED_ShowString(20,4,"t= C",16);
- OLED_ShowString(20,6,"p= KPa",16);
- while(1)
- {
- bmp085Convert();
- OLED_ShowNum(44,4,temperature/10,3,16);
- OLED_ShowNum(44,6,pressure/100,5,16);
- LedBlink(PORT_GROUP1, LED5_PIN);
- SysTick_Delay_Ms(1000);
- }
- }
经程序的编译和下载,其运行效果如下图所示。
温度大气压检测效果
|