|
經(jīng)過(guò)多次努力,用STC90C52+BMP180+LCD1602做的溫度氣壓高度計(jì)終于調(diào)試成功,效果如圖所示,程序見(jiàn)附件。
制作出來(lái)的實(shí)物圖:
氣壓計(jì)實(shí)物圖.jpg (210.65 KB, 下載次數(shù): 101)
下載附件
2018-6-22 10:16 上傳
氣壓計(jì)顯示.jpg (69.46 KB, 下載次數(shù): 107)
下載附件
2018-6-22 10:16 上傳
0.png (9.35 KB, 下載次數(shù): 110)
下載附件
2018-6-22 18:44 上傳
單片機(jī)源程序如下:
- #include <REG52.H>
- #include <math.h> //Keil library
- #include <stdlib.h> //Keil library
- #include <stdio.h> //Keil library
- #include <INTRINS.H> //Keil library
- #define uchar unsigned char
- #define uint unsigned int
- #define BMP085_SlaveAddress 0xee //定義器件在IIC總線(xiàn)中的從地址
- #define OSS 0// Oversampling Setting (note: code is not set up to use other OSS values)
- #define DataPort P0 //LCD1602數(shù)據(jù)端口
- sbit LCM_RS=P2^4; //LCD1602命令端口
- sbit LCM_RW=P2^5; //LCD1602命令端口
- sbit LCM_EN=P2^6; //LCD1602命令端口
- sbit SCL=P1^0; //IIC時(shí)鐘引腳定義
- sbit SDA=P1^1; //IIC數(shù)據(jù)引腳定義
- int dis_data; //變量
- typedef unsigned char BYTE;
- typedef unsigned short WORD;
- long temperature;//溫度值
- long pressure;//壓力值
- long height;//相對(duì)海拔高度值
- 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(unsigned int k)
- {
- unsigned int i,j;
- for(i=0;i<k;i++)
- {
- for(j=0;j<121;j++)
- {;}}
- }
- /*******************************/
- void WaitForEnable(void)
- {
- DataPort=0xff;
- LCM_RS=0;LCM_RW=1;_nop_();
- LCM_EN=1;_nop_();_nop_();
- while(DataPort&0x80);
- LCM_EN=0;
- }
- /*******************************/
- void WriteCommandLCM(uchar CMD,uchar Attribc)
- {
- if(Attribc)WaitForEnable();
- LCM_RS=0;LCM_RW=0;_nop_();
- DataPort=CMD;_nop_();
- LCM_EN=1;_nop_();_nop_();LCM_EN=0;
- }
- /*******************************/
- void WriteDataLCM(uchar dataW)
- {
- WaitForEnable();
- LCM_RS=1;LCM_RW=0;_nop_();
- DataPort=dataW;_nop_();
- LCM_EN=1;_nop_();_nop_();LCM_EN=0;
- }
- /***********************************/
- void InitLcd()
- {
- WriteCommandLCM(0x38,1);
- WriteCommandLCM(0x08,1);
- WriteCommandLCM(0x01,1);
- WriteCommandLCM(0x06,1);
- WriteCommandLCM(0x0c,1);
- }
- /***********************************/
- void DisplayOneChar(uchar X,uchar Y,uchar DData)
- {
- Y&=1;
- X&=15;
- if(Y)X|=0x40;
- X|=0x80;
- WriteCommandLCM(X,0);
- WriteDataLCM(DData);
- }
- ///**************************************
- //延時(shí)5微秒(STC90C52RC@12M)
- //不同的工作環(huán)境,需要調(diào)整此函數(shù),注意時(shí)鐘過(guò)快時(shí)需要修改
- //當(dāng)改用1T的MCU時(shí),請(qǐng)調(diào)整此延時(shí)函數(shù)
- //**************************************/
- //void Delay5us();
- 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 Delay5ms()
- {
- WORD n = 560;
- while (n--);
- }
- /**************************************
- 起始信號(hào)
- **************************************/
- void BMP085_Start()
- {
- SDA = 1; //拉高數(shù)據(jù)線(xiàn)
- SCL = 1; //拉高時(shí)鐘線(xiàn)
- Delay5us(); //延時(shí)
- SDA = 0; //產(chǎn)生下降沿
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線(xiàn)
- }
- /**************************************
- 停止信號(hào)
- **************************************/
- void BMP085_Stop()
- {
- SDA = 0; //拉低數(shù)據(jù)線(xiàn)
- SCL = 1; //拉高時(shí)鐘線(xiàn)
- 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í)鐘線(xiàn)
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線(xiàn)
- Delay5us(); //延時(shí)
- }
- /**************************************
- 接收應(yīng)答信號(hào)
- **************************************/
- bit BMP085_RecvACK()
- {
- SCL = 1; //拉高時(shí)鐘線(xiàn)
- Delay5us(); //延時(shí)
- CY = SDA; //讀應(yīng)答信號(hào)
- SCL = 0; //拉低時(shí)鐘線(xiàn)
- Delay5us(); //延時(shí)
- return CY;
- }
- /**************************************
- 向IIC總線(xiàn)發(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í)鐘線(xiàn)
- Delay5us(); //延時(shí)
- SCL = 0; //拉低時(shí)鐘線(xiàn)
- Delay5us(); //延時(shí)
- }
- BMP085_RecvACK();
- }
- /**************************************
- 從IIC總線(xiàn)接收一個(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í)鐘線(xiàn)
- Delay5us(); //延時(shí)
- dat |= SDA; //讀數(shù)據(jù)
- SCL = 0; //拉低時(shí)鐘線(xiàn)
- 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)
- delay(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)
- delay(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 bmp085Convert()//換算
- {
- unsigned int ut;
- unsigned long up;
- long x1, x2, b5, b6, x3, b3, p;
- unsigned long b4, b7;
- 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);
- 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);
- height=(101325-pressure)*843/100;
- DisplayOneChar(0,0,'T');//溫度部分
- DisplayOneChar(1,0,':');
- DisplayOneChar(4,0,'.');
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
STC90C52+BMP180+LCD1602溫度氣壓高度計(jì)單片機(jī)程序.rar
(42.17 KB, 下載次數(shù): 397)
2018-6-22 10:42 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|