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樓
|