本人自学 单片机,硬件还行!软件不怎么样! 最近想用OLED屏做一个海拔表,用的BMP180的传感器,网上找了一些程序修改后!能正常显示温度和气压!温度是准确的,但是气压和海拔不行,从1楼26楼只变化了几米的高度! 求助大神帮我看看![C] 纯文本查看 复制代码
#include #include //Keil library #include "OLED.H"#define uchar unsigned char#define uint unsigned int***itSCL=P5^3; //IIC时钟引脚定义***itSDA=P0^5; //IIC数据引脚定义#defineBMP085_SlaveAddress 0xee //定义器件在IIC总线中的从地址 #define OSS 0// Oversampling Setting (note: code is not set up to use other OSS values) typedef unsigned char BYTE;typedef unsigned short WORD; uchar BMP_ge,BMP_shi,BMP_bai,BMP_qian,BMP_wan,BMP_shiwan; //显示变量int dis_data;short ac1;short ac2; short ac3; unsigned short ac4;unsigned short ac5;unsigned short ac6;short b1; short b2;short mb;short mc;short md;void conversion(long temp_data);void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data); //单个写入数据uchar Single_Read(uchar REG_Address); //单个读取内部寄存器数据void Multiple_Read(uchar,uchar); //连续的读取内部寄存器数据void BMP085_Start();void BMP085_Stop();void BMP085_SendACK(bit ack);bit BMP085_RecvACK();void BMP085_SendByte(BYTE dat);BYTE BMP085_RecvByte();void delay5us()// { unsigned char a; for(a=12;a>0;a--);}void delay5ms(void) //{ unsigned char a,b,c; for(c=2;c>0;c--) for(b=238;b>0;b--) for(a=30;a>0;a--);}/**************************************起始信号**************************************/void BMP085_Start(){ SDA = 1; //拉高数据线 SCL = 1; //拉高时钟线 delay5us(); //延时 SDA = 0; //产生下降沿 delay5us(); //延时 SCL = 0; //拉低时钟线}/**************************************停止信号**************************************/void BMP085_Stop(){ SDA = 0; //拉低数据线 SCL = 1; //拉高时钟线 delay5us(); //延时 SDA = 1; //产生上升沿 delay5us(); //延时}/**************************************发送应答信号入口参数:ack (0:ACK 1:NAK)**************************************/void BMP085_SendACK(bit ack){ SDA = ack; //写应答信号 SCL = 1; //拉高时钟线 delay5us(); //延时 SCL = 0; //拉低时钟线 delay5us(); //延时}/**************************************接收应答信号**************************************/bit BMP085_RecvACK(){ SCL = 1; //拉高时钟线 delay5us(); //延时 CY = SDA; //读应答信号 SCL = 0; //拉低时钟线 delay5us(); //延时 return CY;}/**************************************向IIC总线发送一个字节数据**************************************/void BMP085_SendByte(BYTE dat){ BYTE i; for (i=0; i<8; i++) //8位计数器 { dat <<= 1; //移出数据的最高位 SDA = CY; //送数据口 SCL = 1; //拉高时钟线 delay5us(); //延时 SCL = 0; //拉低时钟线 delay5us(); //延时 } BMP085_RecvACK();}/**************************************从IIC总线接收一个字节数据**************************************/BYTE BMP085_RecvByte(){ BYTE i; BYTE dat = 0; SDA = 1; //使能内部上拉,准备读取数据, for (i=0; i<8; i++) //8位计数器 { dat <<= 1; SCL = 1; //拉高时钟线 delay5us(); //延时 dat |= SDA; //读数据 SCL = 0; //拉低时钟线 delay5us(); //延时 } return dat;}//*********************************************************//读出BMP085内部数据,连续两个//*********************************************************short Multiple_read(uchar ST_Address){ uchar m***, l***;short _data; BMP085_Start(); //起始信号 BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号 BMP085_SendByte(ST_Address); //发送存储单元地址 BMP085_Start(); //起始信号 BMP085_SendByte(BMP085_SlaveAddress+1); //发送设备地址+读信号 m*** = BMP085_RecvByte(); //BUF[0]存储 BMP085_SendACK(0); //回应ACK l*** = BMP085_RecvByte(); BMP085_SendACK(1); //最后一个数据需要回NOACK BMP085_Stop(); //停止信号 delay5ms(); //延时5MS _data = m*** << 8;_data |= l***;return _data;}//********************************************************************long bmp085ReadTemp(void) //读取未校准温度数据{ BMP085_Start(); //起始信号 BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号 BMP085_SendByte(0xF4); // write register address BMP085_SendByte(0x2E); // write register data for temp BMP085_Stop(); //发送停止信号delay5ms(); //延时5MS// max time is 4.5msreturn (long) Multiple_read(0xF6);}//*************************************************************long bmp085ReadPressure(void) //读取未校准气压数据{long pressure = 0; BMP085_Start(); //起始信号 BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号 BMP085_SendByte(0xF4); // write register address BMP085_SendByte(0x34); // write register data for pressure BMP085_Stop(); //发送停止信号delay5ms(); // max time is 4.5mspressure = Multiple_read(0xF6);pressure &= 0x0000FFFF;return pressure;//return (long) bmp085ReadShort(0xF6);}//**************************************************************//初始化BMP085,根据需要请参考pdf进行修改**************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);}//*********************************************************void conversion(long temp_data) { BMP_shiwan=temp_data/100000+0x30 ; temp_data=temp_data%100000; //取余运算 BMP_wan=temp_data/10000+0x30 ; temp_data=temp_data%10000; //取余运算BMP_qian=temp_data/1000+0x30 ; temp_data=temp_data%1000; //取余运算 BMP_bai=temp_data/100+0x30 ; temp_data=temp_data%100; //取余运算 BMP_shi=temp_data/10+0x30 ; temp_data=temp_data%10; //取余运算 BMP_ge=temp_data+0x30; }void bmp085Convert(){long ut;long up;long x1, x2, b5, b6, x3, b3, p;unsigned long b4, b7;long temperature; //定义温度long pressure; //定义气压float Altitude; //定义海拔ut = bmp085ReadTemp();ut = bmp085ReadTemp(); // 读取温度up = bmp085ReadPressure();up = bmp085ReadPressure(); // 读取压强x1 = ((long)ut - ac6) * ac5 >> 15; //温度校正x2 = ((long) mc << 11) / (x1 + md);b5 = x1 + x2; temperature = (b5 + 8) >> 4; //************* conversion(temperature); LED_P6x8Str(0,2, (unsigned char*)"Temp:"); //温度 LED_P6x8Char(42,2, BMP_bai); //百位 LED_P6x8Char(48,2,BMP_shi); //十位 LED_P6x8Str(54,2,(unsigned char*)".");//. LED_P6x8Char(60,2,BMP_ge); LED_P6x8Str(67,2,(unsigned char*)"{");//℃ //****************************************************************b6 = b5 - 4000;//气压校正x1 = (b2 * (b6 * b6 >> 12)) >> 11;x2 = ac2 * b6 >> 11;x3 = x1 + x2;b3 = (((long)ac1 * 4 + x3) + 2)/4;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 * 2) / b4 ; else p = (b7 / b4) * 2;x1 = (p >> 8) * (p >> 8);x1 = (x1 * 3038) >> 16;x2 = (-7357 * p) >> 16; pressure = p + ((x1 + x2 + 3791) >> 4);//******************************************************* conversion(pressure);LED_P6x8Str(0,3, (unsigned char*)"pressure:"); //压强LED_P6x8Char(60,3, BMP_shiwan);LED_P6x8Char(66,3, BMP_wan); // 万位//********************************************************************LED_P6x8Char(72,3, BMP_qian); // 千位LED_P6x8Str(78,3,(unsigned char*)".");//.LED_P6x8Char(84,3, BMP_bai); // 百位LED_P6x8Char(90,3, BMP_shi); // 十位LED_P6x8Char(96,3, BMP_ge); // 个位LED_P6x8Str(102,3,(unsigned char*)"kps");//.//单位//********************************************************************//海拔显示//海拔计算 Altitude =(44330.0 * (1.0-pow((float)(pressure) / 101325.0, 1.0/5.255)) ); conversion(Altitude); LED_P6x8Str(0,4, (unsigned char*)"Altitude:"); //海拔 LED_P6x8Char(66,4, BMP_wan); // 万位 LED_P6x8Char(72,4, BMP_qian);// 千位 LED_P6x8Char(78,4, BMP_bai); //百位 LED_P6x8Char(84,4, BMP_shi); // 十位 LED_P6x8Str(90,4,(unsigned char*)".");//. LED_P6x8Char(96,4, BMP_ge); // 个位 LED_P6x8Str(102,4,(unsigned char*)"m"); //单位米 }
0
|
13个回答
|
|
|