|
12684液晶屏,bmp180傳感器,51單片機(jī)
單片機(jī)源程序如下:
- #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"
- /*定義傳感器內(nèi)部的EEPROM存儲(chǔ)單元里面11個(gè)值*/
- #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個(gè)臨時(shí)值,取平均值
- 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]= {"氣壓溫度測(cè)量"};
- uchar idata ShowPressure[16]= {"高度: 米"};
- uchar idata ShowTemperature[16]={"溫度: . ℃"};
- uchar idata ShowAltitude[16]= {"電壓: . V"};
- uchar code HZW0[10]={"歡迎使用"};
- uchar code HZW1[12]={"海拔高度計(jì)"};
- uchar code Blank[16];
- bit ack;
- sbit DA = P2^0;
- sbit CK = P2^1;
- sbit CS = P2^2;
- uchar TaskFlag;
- //*延時(shí)us級(jí)函數(shù)
-
- void Delay5us()
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- /**************************************
- 延時(shí)5毫秒(STC90C52RC@12M)
- 不同的工作環(huán)境,需要調(diào)整此函數(shù)
- 當(dāng)改用1T的MCU時(shí),請(qǐng)調(diào)整此延時(shí)函數(shù)
- **************************************/
- 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--);
- }
- /**************************************
- 起始信號(hào)
- **************************************/
- void BMP085_Start()
- {
- SDA = 1; //拉高數(shù)據(jù)線
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- SDA = 0; //產(chǎn)生下降沿
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線
- }
- /**************************************
- 停止信號(hào)
- **************************************/
- void BMP085_Stop()
- {
- SDA = 0; //拉低數(shù)據(jù)線
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- SDA = 1; //產(chǎn)生上升沿
- Delay5us(); //延時(shí)
- }
-
- /**************************************
- 發(fā)送應(yīng)答信號(hào)
- 入口參數(shù):ack (0:ACK 1:NAK)
- **************************************/
- void BMP085_SendACK(bit ack)
- {
- SDA = ack; //寫(xiě)應(yīng)答信號(hào)
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線
- Delay5us(); //延時(shí)
- }
- /**************************************
- 接收應(yīng)答信號(hào)
- **************************************/
- bit BMP085_RecvACK()
- {
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- CY = SDA; //讀應(yīng)答信號(hào)
- SCL = 0; //拉低時(shí)鐘線
- Delay5us(); //延時(shí)
- return CY;
- }
- /**************************************
- 向IIC總線發(fā)送一個(gè)字節(jié)數(shù)據(jù)
- **************************************/
- void BMP085_SendByte(BYTE dat)
- {
- BYTE i;
- for (i=0; i<8; i++) //8位計(jì)數(shù)器
- {
- dat <<= 1; //移出數(shù)據(jù)的最高位
- SDA = CY; //送數(shù)據(jù)口
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線
- Delay5us(); //延時(shí)
- }
- BMP085_RecvACK();
- }
- /**************************************
- 從IIC總線接收一個(gè)字節(jié)數(shù)據(jù)
- **************************************/
- BYTE BMP085_RecvByte()
- {
- BYTE i;
- BYTE dat = 0;
- SDA = 1; //使能內(nèi)部上拉,準(zhǔn)備讀取數(shù)據(jù),
- for (i=0; i<8; i++) //8位計(jì)數(shù)器
- {
- dat <<= 1;
- SCL = 1; //拉高時(shí)鐘線
- Delay5us(); //延時(shí)
- dat |= SDA; //讀數(shù)據(jù)
- SCL = 0; //拉低時(shí)鐘線
- Delay5us(); //延時(shí)
- }
- return dat;
- }
- //*********************************************************
- //讀出BMP085內(nèi)部數(shù)據(jù),連續(xù)兩個(gè)
- //*********************************************************
- short Multiple_read(uchar ST_Address)
- {
- uchar msb, lsb;
- short _data;
- BMP085_Start(); //起始信號(hào)
- BMP085_SendByte(BMP085_SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(hào)
- BMP085_SendByte(ST_Address); //發(fā)送存儲(chǔ)單元地址
- BMP085_Start(); //起始信號(hào)
- BMP085_SendByte(BMP085_SlaveAddress+1); //發(fā)送設(shè)備地址+讀信號(hào)
- msb = BMP085_RecvByte(); //BUF[0]存儲(chǔ)
- BMP085_SendACK(0); //回應(yīng)ACK
- lsb = BMP085_RecvByte();
- BMP085_SendACK(1); //最后一個(gè)數(shù)據(jù)需要回NOACK
- BMP085_Stop(); //停止信號(hào)
- Delay5ms();
- _data = msb << 8;
- _data |= lsb;
- return _data;
- }
- //********************************************************************
- long bmp085ReadTemp(void)
- {
- BMP085_Start(); //起始信號(hào)
- BMP085_SendByte(BMP085_SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(hào)
- BMP085_SendByte(0xF4); // write register address
- BMP085_SendByte(0x2E); // write register data for temp
- BMP085_Stop(); //發(fā)送停止信號(hào)
- Delay1ms(10);// max time is 4.5ms
- return (long) Multiple_read(0xF6);
- }
- //*************************************************************
- long bmp085ReadPressure(void)
- {
- long pressure = 0;
- BMP085_Start(); //起始信號(hào)
- BMP085_SendByte(BMP085_SlaveAddress); //發(fā)送設(shè)備地址+寫(xiě)信號(hào)
- BMP085_SendByte(0xF4); // write register address
- BMP085_SendByte(0x34); // write register data for pressure
- BMP085_Stop(); //發(fā)送停止信號(hào)
- Delay1ms(10); // max time is 4.5ms
- pressure = Multiple_read(0xF6);
- pressure &= 0x0000FFFF;
- return pressure;
- }
- //**************************************************************
-
- //初始化BMP085,根據(jù)需要請(qǐng)參考pdf進(jìn)行修改**************
- 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有效數(shù)據(jù)左對(duì)齊
- CS = 0;
- CK = 0;
- for (i=0;i<12;i++)
- {
- DA = (bit)(da & 0x8000);
- CK = 1;
- da <<= 1;
- CK = 0;
- }
- CS = 1;
- CK = 0;
- }
- /* 函數(shù)名:BMP085_Get_Param(bit choice) 功能:讀出轉(zhuǎn)換后的溫度或氣壓值 當(dāng)實(shí)參為1時(shí),返回氣壓;當(dāng)實(shí)參為0時(shí),返回溫度
- 備注:返回溫度值為0.1,調(diào)用時(shí)轉(zhuǎn)換為1。氣壓為Pa,調(diào)用時(shí)氣壓轉(zhuǎn)換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(); // 讀取壓強(qiáng)
- 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;
- }
- /* 函數(shù)名:BMP085_Get_Altitude 功能:獲取海拔高度值
- 備注:返回高度值為米,調(diào)用時(shí)再轉(zhuǎn)換成帶小數(shù)以米為單位的高度值 */
- int Get_Altitude()
- {
- float pressure;
- float altitude;
- pressure=(float)BMP085_Get_Param(1); //獲取氣壓值
- altitude=44330*(1-pow(pressure/101325,1/5.255)); //根據(jù)芯片手冊(cè)提供的公式計(jì)算海拔高度 單位:米
- altitude*=100;
- return(int)altitude;
- }
- /* LCD程序 */
- void ConvPressure() // 轉(zhuǎn)換氣壓,用于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() //轉(zhuǎn)換氣溫,用于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次測(cè)量值的平均值為測(cè)量結(jié)果*/
- 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;
-
-
- }
- /* 文字顯示函數(shù) 形式參數(shù)格式規(guī)定: x軸位置,從左到右,以兩個(gè)字符或一個(gè)漢字作為一格取值范圍(0-7) y軸位置,從上到下,以一個(gè)漢字為一格,取值范圍為(0-3)
- 數(shù)組名稱,將數(shù)組的值以指針形式訪問(wèn) 顯示數(shù)量,將數(shù)組里面的指定數(shù)量的內(nèi)容顯示出來(lái),取值范圍(0-15) */
- void Show_BMP085_Result() //顯示測(cè)量結(jié)果
- {
- // ConvPressure();
- ConvTemperature();
- ConvAltitude();
- WordDisplay(1,0,PressureTitle,12); //氣壓溫度測(cè)量
- WordDisplay(0,1,ShowPressure,16); // 氣壓 Pa
- …………
- …………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
見(jiàn)7樓
|
評(píng)分
-
查看全部評(píng)分
|