网上查看有卡尔曼滤波,怎么跟程序结合起来,keil中为什么写入pow函数就会出错
- #include <REG51.H>
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <INTRINS.H> //包含_nop_()函数定义的头文件
- #define uchar unsigned char
- #define uint unsigned int
- #define BMP180_SlaveAddress 0xee //定义器件在IIC总线中的从地址
- #define OSS 0 // 过采样设置(注意:代码没有设置为使用其他oss值)
- #define DataPort P0 //LCD1602数据端口
- sbit LCM_RS=P3^5; //LCD1602命令端口寄存器选择位,输入
- sbit LCM_RW=P3^6; //LCD1602命令端口读写
- sbit LCM_EN=P3^4; //LCD1602命令端口使能
- sbit SCL=P2^1; //IIC时钟引脚定义
- sbit SDA=P2^0; //IIC数据引脚定义
- int dis_data; //变量
- typedef unsigned char BYTE;
- typedef unsigned short WORD;
- long temperature;//温度值
- long pressure;//压力值
- long height;//相对海拔高度值
- 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 delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /**************写命令到lcm子函数*****************/
- void write_com(uchar com)
- {
- LCM_RS=0;
- LCM_RW=0;
- LCM_EN=0;
- P0=com;
- delay(5);
- LCM_EN=1;
- delay(5);
- LCM_EN=0;
- }
- /***************写数据到lcm子函数****************/
- void write_data(uchar date)
- {LCM_RS=1;
- P0=date;
- delay(5);
- LCM_EN=1;
- delay(5);
- LCM_EN=0;
- }
- /**************初始化LCD1602*********************/
- void InitLcd()
- {
- write_com(0x38); //设置16X2显示,5X7点阵,8位数据接口
- write_com(0x0c);//设置开显示,不显示光标
- write_com(0x06);//写一个字符后地址指针加1
- write_com(0x01);//显示清0,数据指针清0
- }
- /****************显示指定坐标的一个字符*******************/
- void DisplayOneChar(uchar X,uchar Y,uchar WData)
- {
- Y&=1;
- X&=15;
- if(Y)X|=0x40;
- X|=0x80;
- write_com(X);
- write_data(WData);
- }
- /*************************************延时5微秒函数**************************************/
- void Delay5us()
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- /************************************延时5毫秒函数*************************************/
- void Delay5ms()
- {
- WORD n = 560;
- while (n--);
- }
- //获取BMP180设备ID默认值为0x55
- /*uchar GetBMP180DevicelD()
- {
- uchar DevicelD=0;
- BMP180_Start(); //起始信号
- BMP180_SendByte(BMP180_SlaveAddress);; //发送设备地址+写信号
- BMP180_SendByte(0xD0); //发送存储单元地址OD为ID存储寄存器
- BMP180_Start(); //起始信号
- BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+读信号
- DevicelD =BMP180_SendByte(0xee);
- BMP180_Stop();
- return DevicelD;
- }*/
- /**************************************开始信号(bmp180启动)**************************************/
- void BMP180_Start()
- {
- SDA = 1; //拉高数据线
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- SDA = 0; //产生下降沿
- Delay5us(); //延时
- SCL = 0; //拉低时钟线
- }
- /**************************************停止信号(停止)**************************************/
- void BMP180_Stop()
- {
- SDA = 0; //拉低数据线
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- SDA = 1; //产生上升沿
- Delay5us(); //延时
- }
- /**************************************发送应答信号**************************************/
- void BMP180_SendACK(bit ack)
- {
- SDA = ack; //写应答信号
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- SCL = 0; //拉低时钟线
- Delay5us(); //延时
- }
- /**************************************接收应答信号**************************************/
- bit BMP180_RecvACK()
- {
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- CY = SDA; //读应答信号
- SCL = 0; //拉低时钟线
- Delay5us(); //延时
- return CY;
- }
- /**************************************向IIC总线发送一个字节数据**************************************/
- void BMP180_SendByte(BYTE dat)
- {
- BYTE i;
- for (i=0; i<8; i++) //8位计数器
- {
- dat <<= 1; //移出数据的最高位
- SDA = CY; //送数据口
- SCL = 1; //拉高时钟线
- Delay5us(); //延时
- SCL = 0; //拉低时钟线
- Delay5us(); //延时
- }
- BMP180_RecvACK();
- }
- /**************************************从IIC总线接收一个字节数据**************************************/
- BYTE BMP180_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;
- }
- /*******************从BMP180中读取两字节数据******************************/
- short Multiple_read(uchar ST_Address)
- {
- uchar msb, lsb;
- short _data;
- BMP180_Start(); //起始信号
- BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+写信号
- BMP180_SendByte(ST_Address); //发送存储单元地址
- BMP180_Start(); //起始信号
- BMP180_SendByte(BMP180_SlaveAddress+1); //发送设备地址+读信号
- msb = BMP180_RecvByte(); //BUF[0]存储
- BMP180_SendACK(0); //主机发送应答信号,回应ACK
- lsb = BMP180_RecvByte();
- BMP180_SendACK(1); //最后一个数据需要回非ACK
- BMP180_Stop(); //停止信号
- Delay5ms();
- _data = msb << 8;
- _data |= lsb;
- return _data;
- }
- /******************************温度读取**************************************/
- long bmp180ReadTemp(void)
- {
- BMP180_Start(); //起始信号
- BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+写信号
- BMP180_SendByte(0xF4); // write register address
- BMP180_SendByte(0x2E); // write register data for temp
- BMP180_Stop(); //发送停止信号
- delay(10);// max time is 4.5ms
- return (long) Multiple_read(0xF6); //返回温度,0.1℃
- }
- /************************压强读取*************************************/
- long bmp180ReadPressure(void)
- {
- long pressure = 0;
- BMP180_Start(); //起始信号
- BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+写信号
- BMP180_SendByte(0xF4); // write register address
- BMP180_SendByte(0x34); // write register data for pressure
- BMP180_Stop(); //发送停止信号
- delay(10); // max time is 4.5ms
- pressure = Multiple_read(0xF6);
- pressure &= 0x0000FFFF;
- return pressure; //返回气压值,pa
- }
- /****************初始化BMP180,读出内部标定参数**************/
- void Init_BMP180()
- {
- 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);
- }
- double pow(double a,double b)
- {
- double result=1;
- for(;b>0;b--)
- {
- result*=a;
- }
- return result;
- }
- /*************************获取气压、温度并显示******************************/
- void bmp180Convert()
- {
- unsigned int ut;
- unsigned long up;
-
- long x1, x2, b5, b6, x3, b3, p;
- unsigned long b4, b7;
-
- long temp,y;
-
- ut = bmp180ReadTemp(); // 读取温度
- up = bmp180ReadPressure(); // 读取压强
-
- //根据芯片手册提供的公式计算补偿后的温度
- x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;//x1=((ut-ac6)*ac5)/(2^15)
- x2 = ((long) mc << 11) / (x1 + md); //x2=(mc*2^11)/(x1+md)
- b5 = x1 + x2; //b5=x1+x2
- temperature = (b5 + 8) >> 4; //t=(b5+8)/(2^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; // Calculate B4
- 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);
- delay(50);
-
- temp=1.0563;
- y=0.1987;
- pow(temp,y);
-
- /*temp=pressure/101325;
- y=1/5.255;
- temp=pow(temp,y);
- //height=(44330*(1-temp));*/
- //height= 44330*(1-pow((pressure/101325.0),(1.0/5.255)));
- height=(101325-pressure)*843/1000;
- if(height<0)
- {
- height=(-height);
- //height=(-123);
- DisplayOneChar(0,0,'T');//温度部分
- DisplayOneChar(1,0,':');
- DisplayOneChar(2,0,temperature%1000/100+0x30);
- DisplayOneChar(3,0,temperature%100/10+0x30);
- DisplayOneChar(4,0,'.');
- DisplayOneChar(5,0,temperature%10+0x30);
- DisplayOneChar(6,0,0XDF);
- DisplayOneChar(7,0,'C');
- DisplayOneChar(8,0,'H');//海拔部分
- DisplayOneChar(9,0,'-');
- DisplayOneChar(10,0,height%10000/1000+0x30);
- DisplayOneChar(11,0,height%1000/100+0x30);
- DisplayOneChar(12,0,height%100/10+0x30);
- DisplayOneChar(13,0,height%10+0x30);
- DisplayOneChar(14,0,'.');
- DisplayOneChar(15,0,height*10%10+0x30);
- //DisplayOneChar(15,0,'m');
- DisplayOneChar(0,1,'P');//气压部分
- DisplayOneChar(1,1,':');
- DisplayOneChar(2,1,pressure%1000000/100000+48);
- DisplayOneChar(3,1,pressure%100000/10000+48);
- DisplayOneChar(4,1,pressure%10000/1000+48);
- DisplayOneChar(5,1,'.');
- DisplayOneChar(6,1,pressure%1000/100+48);
- DisplayOneChar(7,1,pressure%100/10+48);
- //DisplayOneChar(8,1,pressure%10/1+48);
- DisplayOneChar(8,1,'K');
- DisplayOneChar(9,1,'p');
- DisplayOneChar(10,1,'a');
-
- }
- else
- {
- DisplayOneChar(0,0,'T');//温度部分
- DisplayOneChar(1,0,':');
- DisplayOneChar(2,0,temperature%1000/100+0x30);
- DisplayOneChar(3,0,temperature%100/10+0x30);
- DisplayOneChar(4,0,'.');
- DisplayOneChar(5,0,temperature%10+0x30);
- DisplayOneChar(6,0,0XDF);
- DisplayOneChar(7,0,'C');
- DisplayOneChar(8,0,' ');
- DisplayOneChar(9,0,'H');//海拔部分
- DisplayOneChar(10,0,height%10000/1000+0x30);
- DisplayOneChar(11,0,height%1000/100+0x30);
- DisplayOneChar(12,0,height%100/10+0x30);
- DisplayOneChar(13,0,height%10+0x30);
- DisplayOneChar(14,0,'.');
- DisplayOneChar(15,0,height*10%10+0x30);
- //DisplayOneChar(15,0,'m');
- DisplayOneChar(0,1,'P');//气压部分
- DisplayOneChar(1,1,':');
- DisplayOneChar(2,1,pressure%1000000/100000+48);
- DisplayOneChar(3,1,pressure%100000/10000+48);
- DisplayOneChar(4,1,pressure%10000/1000+48);
- DisplayOneChar(5,1,'.');
- DisplayOneChar(6,1,pressure%1000/100+48);
- DisplayOneChar(7,1,pressure%100/10+48);
- //DisplayOneChar(8,1,pressure%10/1+48);
- DisplayOneChar(8,1,'K');
- DisplayOneChar(9,1,'p');
- DisplayOneChar(10,1,'a');
- }
- }
- /**********************主程序***************************/
- void main()
- { long icid;
- delay(50); //上电延时
- InitLcd(); //液晶初始化
- Init_BMP180(); //初始化BMP180
- /*icid = Multiple_read(0xD0);
- if (icid==0xee)
- DisplayOneChar(8,1,'y');
- else DisplayOneChar(9,1,'n');
- while(1);*/
- while(1) //循环
- {
- bmp180Convert();
- delay(100);
- }
- }
复制代码
|