|
51單片機實現(xiàn)大氣壓傳感器GY68測量程序
IMG_20200326_091506.jpg (7.34 MB, 下載次數(shù): 50)
下載附件
2020-3-26 09:22 上傳
IMG_20200326_091528.jpg (7.09 MB, 下載次數(shù): 44)
下載附件
2020-3-26 09:22 上傳
單片機源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <math.h> //Keil library
- #define uint unsigned int
- #define uchar unsigned char
- #define BMP085_SlaveAddress 0xee //定義器件在IIC總線中的從地址
- #define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values)
-
- typedef unsigned char BYTE;
- typedef unsigned short WORD;
- sbit duoji = P1^2;
- sbit lcden = P2^1;
- sbit rs = P2^2;
- sbit key_1 = P3^4;
- sbit key_2 = P3^5;
- sbit key_3 = P3^6;
- sbit key_4 = P3^7;
- sbit SCL=P1^0; //IIC時鐘引腳定義
- sbit SDA=P1^1; //IIC數(shù)據(jù)引腳定義
- uchar count3;
- uchar ge,shi,bai,qian,wan,shiwan; //顯示變量
- int dis_data;
- 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;
- uchar code table1[]="Pressure: HPa";
- uchar code table2[]="status: normal ";
- uchar code table3[]="status: abnormal";
- uchar code table4[]="MQ7: T: ";
- uchar code table5[]="Revise success!!";
- uchar code table6[]=" Revise Menu ";
- void LCD_DISPLAY(uchar num); //LCD顯示
- void delay(uint z) //毫秒延時
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- //****************************LCD1602的初始化*******************************
- /**************************************************************************/
- void write_com(uchar com)//LCD寫字節(jié)
- {
- rs=0;
- lcden=0;
- P0=com;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
-
- void write_date(uchar date)//LCD讀字節(jié)
- {
- rs=1;
- lcden=0;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void lcd_sfm(uchar add,int date)//顯示位置和數(shù)字
- {
- uchar qian,bai,shi,ge;
- qian = date/1000;
- bai = date%1000/100;
- shi = date%100/10;
- ge = date%10;
- write_com(0x80+add);
- write_date(0x30+qian);
- write_date(0x30+bai);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void SetCurPosition(uchar X, uchar Y)//設(shè)置光標(biāo)
- {
- Y &= 0x01;
- X &= 0x0F; // 限制X不能大于15,Y不能大于1
- if (Y)
- {
- X |= 0xc0; // 當(dāng)要顯示第二行時地址碼:0xc0
- }
- X |= 0x80; // 第一行的地址碼:0x80
- write_com(X); // 這里不檢測忙信號,發(fā)送地址碼
- //write_com(0x02); //光標(biāo)歸位
- write_com(0x0f); //關(guān)光標(biāo),閃爍
-
- }
- void DisplayOneChar(uchar X,uchar Y,uchar DData)
- {
- Y&=1;
- X&=15;
- if(Y)X|=0x40;
- X|=0x80;
- write_com(X);
- write_date(DData);
-
- }
- void conversion(long temp_data)
- {
-
- shiwan=temp_data/100000+0x30 ;
- temp_data=temp_data%100000; //取余運算
- wan=temp_data/10000+0x30 ;
- temp_data=temp_data%10000; //取余運算
- qian=temp_data/1000+0x30 ;
- temp_data=temp_data%1000; //取余運算
- bai=temp_data/100+0x30 ;
- temp_data=temp_data%100; //取余運算
- shi=temp_data/10+0x30 ;
- temp_data=temp_data%10; //取余運算
- ge=temp_data+0x30;
- }
- /**********************************************************************************************
- ** BMP180初始化及測試函數(shù)
- **********************************************************************************************/
- void Delay5us()
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- /**************************************
- 延時5毫秒(STC90C52RC@12M)
- 不同的工作環(huán)境,需要調(diào)整此函數(shù)
- 當(dāng)改用1T的MCU時,請調(diào)整此延時函數(shù)
- **************************************/
- void Delay5ms()
- {
- WORD n = 560;
- while (n--);
- }
- /**************************************
- 起始信號
- **************************************/
- void BMP085_Start()
- {
- SDA = 1; //拉高數(shù)據(jù)線
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SDA = 0; //產(chǎn)生下降沿
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- }
- /**************************************
- 停止信號
- **************************************/
- void BMP085_Stop()
- {
- SDA = 0; //拉低數(shù)據(jù)線
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SDA = 1; //產(chǎn)生上升沿
- Delay5us(); //延時
- }
- /**************************************
- 發(fā)送應(yīng)答信號
- 入口參數(shù):ack (0:ACK 1:NAK)
- **************************************/
- void BMP085_SendACK(bit ack)
- {
- SDA = ack; //寫應(yīng)答信號
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- }
- /**************************************
- 接收應(yīng)答信號
- **************************************/
- bit BMP085_RecvACK()
- {
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- CY = SDA; //讀應(yīng)答信號
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- return CY;
- }
- /**************************************
- 向IIC總線發(fā)送一個字節(jié)數(shù)據(jù)
- **************************************/
- void BMP085_SendByte(BYTE dat)
- {
- BYTE i;
- for (i=0; i<8; i++) //8位計數(shù)器
- {
- dat <<= 1; //移出數(shù)據(jù)的最高位
- SDA = CY; //送數(shù)據(jù)口
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- }
- BMP085_RecvACK();
- }
- /**************************************
- 從IIC總線接收一個字節(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位計數(shù)器
- {
- dat <<= 1;
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- dat |= SDA; //讀數(shù)據(jù)
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- }
- return dat;
- }
- //*********************************************************
- //讀出BMP085內(nèi)部數(shù)據(jù),連續(xù)兩個
- //*********************************************************
- short Multiple_read(uchar ST_Address)
- {
- uchar msb, lsb;
- short _data;
- BMP085_Start(); //起始信號
- BMP085_SendByte(BMP085_SlaveAddress); //發(fā)送設(shè)備地址+寫信號
- BMP085_SendByte(ST_Address); //發(fā)送存儲單元地址
- BMP085_Start(); //起始信號
- BMP085_SendByte(BMP085_SlaveAddress+1); //發(fā)送設(shè)備地址+讀信號
- msb = BMP085_RecvByte(); //BUF[0]存儲
- BMP085_SendACK(0); //回應(yīng)ACK
- lsb = BMP085_RecvByte();
- BMP085_SendACK(1); //最后一個數(shù)據(jù)需要回NOACK
- BMP085_Stop(); //停止信號
- Delay5ms();
- _data = msb << 8;
- _data |= lsb;
- return _data;
- }
- //*************************************************************
- long bmp085ReadPressure(void)
- {
- long pressure = 0;
- BMP085_Start(); //起始信號
- BMP085_SendByte(BMP085_SlaveAddress); //發(fā)送設(shè)備地址+寫信號
- BMP085_SendByte(0xF4); // write register address
- BMP085_SendByte(0x34); // write register data for pressure
- BMP085_Stop(); //發(fā)送停止信號
- delay(10); // max time is 4.5ms
-
- pressure = Multiple_read(0xF6);
- pressure &= 0x0000FFFF;
-
- return pressure;
- //return (long) bmp085ReadShort(0xF6);
- }
- //**************************************************************
- //初始化BMP085,根據(jù)需要請參考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 bmp085Convert()//bmp180轉(zhuǎn)換
- {
- long up;
- long x1, x2, b5, b6, x3, b3, p;
- unsigned long b4, b7;
- long pressure;
- float temp;
- int pressure_int;
- up = bmp085ReadPressure();
- up = bmp085ReadPressure(); // 讀取壓強
-
- //*************
-
- b6 = b5 - 4000;
- x1 = (b2 * (b6 * b6 >> 12)) >> 11;
- x2 = ac2 * b6 >> 11;
- x3 = x1 + x2;
- b3 = (((long)ac1 * 4 + x3) + 2)/4;
- 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 * 2) / b4 ;
- else
- p = (b7 / b4) * 2;
- x1 = (p >> 8) * (p >> 8);
- x1 = (x1 * 3038) >> 16;
- x2 = (-7357 * p) >> 16;
- pressure = p + ((x1 + x2 + 3791) >> 4);
- temp = (float)pressure / 100.0 + 0.5;//轉(zhuǎn)換為百帕 并四舍五入
- pressure_int = (int)temp; //強制轉(zhuǎn)換
- lcd_sfm(0x09,pressure_int);//顯示
-
- }
- void init()
- {
- lcden=0;
- write_com(0x38); //打開顯示模式設(shè)置
- write_com(0x0c); //打開顯示,光標(biāo)等等設(shè)置未零
- write_com(0x06); //當(dāng)讀或?qū)懸粋字符后地址指針加一,且光標(biāo)加一,當(dāng)寫一個字符后整頻顯示左移,
- write_com(0x01); //清零指令
- write_com(0x80);
-
- TMOD = 0x01;
- TH0 = (65536-100)/256;//100us進入一次中斷
- TL0 = (65536-100)%256;
- TR0 = 1;
- ET0 = 1;
- EA = 0;//開啟總中斷
-
- }
- /**********************************************************************************************
- ** TIMER0中斷服務(wù)子函數(shù)產(chǎn)生PWM
- **********************************************************************************************/
- uchar count = 0;
- uchar pwm = 15;
- void timer0()interrupt 1 using 0
- {
- TH0 = (65536-100)/256;
- TL0 = (65536-100)%256;
- count++;
- if(count >= 200)
- {
- count = 0;
- }
- if(count <= pwm) duoji = 1;
- else duoji = 0;
-
- }
- /**********************************************************************************************
- ** 主函數(shù)
- **********************************************************************************************/
- int max_pressure = 910;//大氣壓閾值
- void main()
- {
- delay(100);//開機延時
- init();//初始化
- Init_BMP085();//BMP初始化
- //lcd_sfm(0x05,666);
- LCD_DISPLAY(1);
- LCD_DISPLAY(2);
- while(1)
- {
- bmp085Convert();
- delay(1000);
- }
- }
- /********************************************************************************************
- * LCD顯示函數(shù)
- ********************************************************************************************/
- void LCD_DISPLAY(uchar num) //LCD顯示
- {
- if(num == 1)
- {
- //write_com(0x01);
- write_com(0x80);
- for(count3=0;count3<16;count3++)
- {
- write_date(table1[count3]); //
- delay(5);
- }
- }
- if(num == 2)
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
test.zip
(62.13 KB, 下載次數(shù): 26)
2020-3-26 09:19 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
|