BMP085是一种用于温度和大气压检测的传感器,并以I2C接口的方式来工作,为了便于程序移植这里是以GPIO口模拟I2C的方式来驱动该传感器。 SCL--- PE.4 SDA--- PA.0 故使这2个引脚输出高低电平的语句定义为: #define SCL_Set() rt_pin_write(pin_clk, PIN_HIGH) #define SCL_Clr() rt_pin_write(pin_clk, PIN_LOW) #define SDA_Set() rt_pin_write(pin_dat, PIN_HIGH) #define SDA_Clr() rt_pin_write(pin_dat, PIN_LOW) 而控制数据引脚输入输出模式的语句为: #define IIC_INPUT_MODE_SET() rt_pin_mode(pin_dat, PIN_MODE_INPUT_PULLUP) #define IIC_OUTPUT_MODE_SET() rt_pin_mode(pin_dat, PIN_MODE_OUTPUT) 控制数据引脚读取电平状态的语句定义为: #define IIC_SDA_IN rt_pin_read(pin_dat) 为了能直观地观察检测数据,这里是有OLED屏来显示检测值,相应的实物连接及检测效果图如下:
实物连接及检测效果图 BMP085的初始化函数为:
- void BMP085_Init(void)
- {
- rt_pin_mode(pin_dat, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_clk, PIN_MODE_OUTPUT);
- 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的起始与停止函数为:
- void BMP085_Start(void)
- {
- IIC_OUTPUT_MODE_SET();
- SDA_Set();
- SCL_Set();
- rt_hw_us_delay(5);
- SDA_Clr();
- rt_hw_us_delay(5);
- SCL_Clr();
- }
- void BMP085_Stop(void)
- {
- IIC_OUTPUT_MODE_SET();
- SDA_Clr(); //拉低数据线
- SCL_Clr();
- rt_hw_us_delay(5); //延时
- SCL_Set(); //拉高时钟线
- rt_hw_us_delay(5);
- SDA_Set(); //产生上升沿
- rt_hw_us_delay(5); //延时
- }
复制代码
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;
- rt_hw_us_delay(2);
- SCL_Set();
- rt_hw_us_delay(2);
- SCL_Clr();
- rt_hw_us_delay(2);
- }
- }
复制代码
读取温度与气压的函数为:
- 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();
- rt_thread_mdelay(10);
- return (long) Multiple_read(0xF6);
- }
- 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();
- rt_thread_mdelay(10);
- 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;
- 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)<>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)
- {
- pin = rt_pin_get("PE.1");
- rt_pin_mode(pin, PIN_MODE_OUTPUT);
- pin_clk = rt_pin_get("PE.4");
- pin_dat = rt_pin_get("PA.0");
- pin_scl = rt_pin_get("PE.3");
- pin_sda = rt_pin_get("PE.2");
- rt_pin_mode(pin_dat, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_clk, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_scl, PIN_MODE_OUTPUT);
- rt_pin_mode(pin_sda, PIN_MODE_OUTPUT);
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"AB32VG1 RISC-V",16);
- OLED_ShowString(0,2,"OLED & BMP085",16);
- BMP085_Init();
- rt_thread_mdelay(2000);
- OLED_Clear();
- OLED_ShowString(0,0,"t= C",16);
- OLED_ShowString(0,2,"p= KPa",16);
- while(1)
- {
- bmp085Convert();
- OLED_ShowNum(24,0,temperature/10,3,16);
- OLED_ShowNum(24,2,pressure/100,5,16);
- rt_pin_write(pin, PIN_LOW);
- rt_thread_mdelay(500);
- rt_pin_write(pin, PIN_HIGH);
- rt_thread_mdelay(500);
- }
- }
复制代码
|