本帖最后由 jinglixixi 于 2020-6-11 08:27 编辑
BMP085是一款基于I2C接口的数字式气温、气压传感器,具有体积少易于使用的特点。 BMP085与MCU的连接关系为: SCLK——PB7 SDIN——PB8 其余的两个引脚则接3.3V工作电源
BMP085与开发板连接图
定义GPIO口输入及输出高低电平的语句为: #defineSCL_Set() GPIO_SetPinLevel(GPIOB,GPIO_PIN7, GPIO_LEVEL_HIGH); // PB7 #define SCL_Clr() GPIO_SetPinLevel(GPIOB, GPIO_PIN7,GPIO_LEVEL_LOW);
#defineSDA_Set() GPIO_SetPinLevel(GPIOB,GPIO_PIN8, GPIO_LEVEL_HIGH); // PB8 #defineSDA_Clr() GPIO_SetPinLevel(GPIOB,GPIO_PIN8, GPIO_LEVEL_LOW);
#defineIIC_SDA_IN GPIO_GetPinLevel(GPIOB,GPIO_PIN8)
#defineIIC_INPUT_MODE_SET() GPIO_SetDir(GPIOB,GPIO_PIN8, GPIO_IN); #defineIIC_OUTPUT_MODE_SET() GPIO_SetDir(GPIOB,GPIO_PIN8, GPIO_OUT);
配置相关引脚为输出功能的函数为: void IIC_Init(void)
{
GPIO_SetDir(GPIOB, GPIO_PIN7, GPIO_OUT);
GPIO_SetDir(GPIOB, GPIO_PIN8, GPIO_OUT);
}
辅助BMP085读写的基本函数有: void IIC_Start(void)
{
IIC_OUTPUT_MODE_SET();
SDA_Set();
SCL_Set();
udelay(5);
SDA_Clr();
udelay(5);
SCL_Clr();
}
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);
}
气温的读取函数为: long bmp085ReadTemp(void)
{
IIC_Start();
IIC_Send_Byte(BMP085_SlaveAddress);
while(IIC_Wait_Ack()){}
IIC_Send_Byte(0xF4);
while(IIC_Wait_Ack()){}
IIC_Send_Byte(0x2E);
while(IIC_Wait_Ack()){}
IIC_Stop();
mdelay(10);
return (long) Multiple_read(0xF6);
}
气压的读取函数为: long bmp085ReadPressure(void)
{
long pressure = 0;
IIC_Start();
IIC_Send_Byte(BMP085_SlaveAddress);
while(IIC_Wait_Ack()){}
IIC_Send_Byte(0xF4);
while(IIC_Wait_Ack()){}
IIC_Send_Byte(0x34);
while(IIC_Wait_Ack()){}
IIC_Stop();
mdelay(10);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
实现图示输出效果的主程序为: int main(void)
{
InitDelay();
InitDebug();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AC8710 TEST",16);
OLED_ShowString(0,2,"OLED Display",16);
OLED_ShowString(0,4,"jinglixixi",16);
OLED_ShowString(0,6,"2020.6.6",16);
IIC_Init(); //BMP085
Init_BMP085();
while(1)
{
bmp085Convert();
printf("temperature: %d C\r\n", temperature/10);
printf("pressure: %d KPa\r\n", pressure/100);
ConvAltitude();
mdelay(1000);
}
}
测量效果图
|