|
12684液晶屏,bmp180传感器,51单片机
单片机源程序如下:
- #include <reg51.h>
- #include <math.h> //Keil library
- #include <stdlib.h> //Keil library
- #include <stdio.h> //Keil library
- #include <INTRINS.H> //Keil library
- #include<absacc.h>
- #include "lcd.h"
- #include "BMP180.H"
- /*定义传感器内部的EEPROM存储单元里面11个值*/
- #define uchar unsigned char
- #define uint unsigned int
- typedef unsigned char BYTE;
- typedef unsigned short WORD;
- #define BMP085_SlaveAddress 0xee //定义器件在IIC总线中的从地址
- short ac1;
- short ac2;
- short ac3;
- unsigned short ac4;
- unsigned short ac5;
- unsigned short ac6;
- short b1;
- short b2;
- uchar idata flag;
- int idata AltitudeTemp[10]={0,0,0,0,0,0,0,0,0,0}; // 海拔高度的10个临时值,取平均值
- short mb;
- short mc;
- short md;
- #define OSS 0// Oversampling Setting (note: code is not set up to use other OSS values)
- uchar code PressureTitle[16]= {"气压温度测量"};
- uchar idata ShowPressure[16]= {"高度: 米"};
- uchar idata ShowTemperature[16]={"温度: . ℃"};
- uchar idata ShowAltitude[16]= {"电压: . V"};
- uchar code HZW0[10]={"欢迎使用"};
- uchar code HZW1[12]={"海拔高度计"};
- uchar code Blank[16];
- bit ack;
- sbit DA = P2^0;
- sbit CK = P2^1;
- sbit CS = P2^2;
- uchar TaskFlag;
- //*延时us级函数
-
- void Delay5us()
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- /**************************************
- 延时5毫秒(STC90C52RC@12M)
- 不同的工作环境,需要调整此函数
- 当改用1T的MCU时,请调整此延时函数
- **************************************/
- void Delay1ms(uint xms)
- { uint a,b;
- for(a=0;a<110;a++)
- for(b=0;b<xms;b++);
- }
- void Delay5ms()
- {
- WORD n = 560;
- while (n--);
- }
- /**************************************
- 起始信号
- **************************************/
- 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 msb, lsb;
- short _data;
- BMP085_Start(); //起始信号
- BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
- BMP085_SendByte(ST_Address); //发送存储单元地址
- BMP085_Start(); //起始信号
- BMP085_SendByte(BMP085_SlaveAddress+1); //发送设备地址+读信号
- msb = BMP085_RecvByte(); //BUF[0]存储
- BMP085_SendACK(0); //回应ACK
- lsb = BMP085_RecvByte();
- BMP085_SendACK(1); //最后一个数据需要回NOACK
- BMP085_Stop(); //停止信号
- Delay5ms();
- _data = msb << 8;
- _data |= lsb;
- 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(); //发送停止信号
- Delay1ms(10);// max time is 4.5ms
- return (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(); //发送停止信号
- Delay1ms(10); // max time is 4.5ms
- pressure = Multiple_read(0xF6);
- pressure &= 0x0000FFFF;
- return pressure;
- }
- //**************************************************************
-
- //初始化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 da5615(unsigned int da)
- {
- unsigned char i;
- da <<= 6;//10有效数据左对齐
- CS = 0;
- CK = 0;
- for (i=0;i<12;i++)
- {
- DA = (bit)(da & 0x8000);
- CK = 1;
- da <<= 1;
- CK = 0;
- }
- CS = 1;
- CK = 0;
- }
- /* 函数名:BMP085_Get_Param(bit choice) 功能:读出转换后的温度或气压值 当实参为1时,返回气压;当实参为0时,返回温度
- 备注:返回温度值为0.1,调用时转换为1。气压为Pa,调用时气压转换hPa */
- long BMP085_Get_Param(bit choice)
- {
- long ut;
- long up;
- long x1, x2, b5, b6, x3, b3, p;
- unsigned long b4, b7;
- long temperature;
- long pressure;//压力值
- 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);
- if(choice==0)
- return (long)temperature;
- b6 = b5 - 4000;
- // Calculate B3
- 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);
- return (long)pressure;
- }
- /* 函数名:BMP085_Get_Altitude 功能:获取海拔高度值
- 备注:返回高度值为米,调用时再转换成带小数以米为单位的高度值 */
- int Get_Altitude()
- {
- float pressure;
- float altitude;
- pressure=(float)BMP085_Get_Param(1); //获取气压值
- altitude=44330*(1-pow(pressure/101325,1/5.255)); //根据芯片手册提供的公式计算海拔高度 单位:米
- altitude*=100;
- return(int)altitude;
- }
- /* LCD程序 */
- void ConvPressure() // 转换气压,用于LCD的显示
- {
- long Pressure;
- Pressure=BMP085_Get_Param(1);
- if(Pressure>=0)ShowPressure[6]='+';
- else
- {
- ShowPressure[6]='-';
- Pressure=~Pressure;
- }
- // ShowPressure[7]=Pressure%1000000/100000+48;
- // ShowPressure[8]=Pressure%100000/10000+48;
- // ShowPressure[9]=Pressure%10000/1000+48;
- // ShowPressure[10]='.';
- // ShowPressure[11]=Pressure%1000/100+48;
- // ShowPressure[13]='K';
- //
- }
- void ConvTemperature() //转换气温,用于LCD显示
- {
- int Temperature;
- Temperature=(int)BMP085_Get_Param(0);
- if(Temperature>=0)ShowTemperature[6]='+';
- else
- {
- ShowTemperature[6]='-';
- Temperature=~Temperature;
-
- }
- ShowTemperature[10]=Temperature%1000/100+48;
- ShowTemperature[11]=Temperature%100/10+48;
- ShowTemperature[13]=Temperature%10+48;
- }
- void ConvAltitude()
- {
- uint voltage;
- uint ab5;
- uchar a;
- uchar ab1,ab2,ab3,ab4,ab6;
- unsigned char xiaoshu,zhengshu,xiaoshu2;
- long Altitude;
- Altitude=Get_Altitude();
- if(Altitude>=0)ShowAltitude[6]='+';
- else
- {
- ShowAltitude[6]='-';
- Altitude=~Altitude;
- }
- /* 最多10次测量值的平均值为测量结果*/
- flag++;
- if(flag>=10) flag=0;
- AltitudeTemp[flag]=Altitude;
- for(a=0;a<10;a++)
- Altitude+=AltitudeTemp[a];
- Altitude/=11;
- ab1=Altitude/10000;
- ab2=Altitude%10000/1000;
- ab3=Altitude%1000/100;
- ab4=Altitude%100/10;
- ab5=ab1*1000+ab2*100+ab3*10+ab4;
- ab6=(int)(ab5/4);
- da5615(ab6);
- voltage=ab5*2;
- zhengshu=voltage/1000;
- xiaoshu=voltage%1000/100;
- xiaoshu2=voltage%100/10;
- if(xiaoshu2>=5)
- {
- xiaoshu=xiaoshu+1;
- }
- ShowPressure[9]=Altitude/10000+48;
- ShowPressure[10]=Altitude%10000/1000+48;
- ShowPressure[11]=Altitude%1000/100+48;
- ShowPressure[12]=Altitude%100/10+48;
-
- ShowAltitude[12]=zhengshu+0x30;
- ShowAltitude[14]=xiaoshu+0x30;
-
-
- }
- /* 文字显示函数 形式参数格式规定: x轴位置,从左到右,以两个字符或一个汉字作为一格取值范围(0-7) y轴位置,从上到下,以一个汉字为一格,取值范围为(0-3)
- 数组名称,将数组的值以指针形式访问 显示数量,将数组里面的指定数量的内容显示出来,取值范围(0-15) */
- void Show_BMP085_Result() //显示测量结果
- {
- // ConvPressure();
- ConvTemperature();
- ConvAltitude();
- WordDisplay(1,0,PressureTitle,12); //气压温度测量
- WordDisplay(0,1,ShowPressure,16); // 气压 Pa
- …………
- …………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
见7楼
|
评分
-
查看全部评分
|