單片機源程序如下:
- #include "reg52.h"
- #include "BMP180.h"
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include "intrins.h"
- #include "oled.h"
- long temperature; //定義溫度
- long pressure; //定義氣壓
- short ac1,ac2,ac3,b1,b2,mb,mc,md;
- unsigned short ac4,ac5,ac6;
- /**************************************
- 延時5微秒(STC90C52RC@12M)
- 不同的工作環境,需要調整此函數,注意時鐘過快時需要修改
- 當改用1T的MCU時,請調整此延時函數
- **************************************/
- void Delay5us()
- {
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- /**************************************
- 延時5毫秒(STC90C52RC@12M)
- 不同的工作環境,需要調整此函數
- 當改用1T的MCU時,請調整此延時函數
- **************************************/
- void Delay5ms()
- {
- WORD n = 560;
- while (n--);
- }
- /**************************************
- 起始信號
- **************************************/
- void BMP180_Start()
- {
- SDA = 1; //拉高數據線
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SDA = 0; //產生下降沿
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- }
- /**************************************
- 停止信號
- **************************************/
- void BMP180_Stop()
- {
- SDA = 0; //拉低數據線
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SDA = 1; //產生上升沿
- Delay5us(); //延時
- }
- /**************************************
- 發送應答信號
- 入口參數:ack (0:ACK 1:NAK)
- **************************************/
- void BMP180_SendACK(bit ack)
- {
- SDA = ack; //寫應答信號
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- }
- /**************************************
- 接收應答信號
- **************************************/
- bit BMP180_RecvACK()
- {
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- CY = SDA; //讀應答信號
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- return CY;
- }
- /**************************************
- 向IIC總線發送一個字節數據
- **************************************/
- void BMP180_SendByte(BYTE dat)
- {
- BYTE i;
- for (i=0; i<8; i++) //8位計數器
- {
- dat <<= 1; //移出數據的最高位
- SDA = CY; //送數據口
- SCL = 1; //拉高時鐘線
- Delay5us(); //延時
- SCL = 0; //拉低時鐘線
- Delay5us(); //延時
- }
- BMP180_RecvACK();
- }
- /**************************************
- 從IIC總線接收一個字節數據
- **************************************/
- BYTE BMP180_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;
- BMP180_Start(); //起始信號
- BMP180_SendByte(BMP180_SlaveAddress); //發送設備地址+寫信號
- BMP180_SendByte(ST_Address); //發送存儲單元地址
- BMP180_Start(); //起始信號
- BMP180_SendByte(BMP180_SlaveAddress+1); //發送設備地址+讀信號
-
- msb = BMP180_RecvByte(); //BUF[0]存儲
- BMP180_SendACK(0); //回應ACK
- lsb = BMP180_RecvByte();
- BMP180_SendACK(1); //最后一個數據需要回NOACK
-
- BMP180_Stop(); //停止信號
- Delay5ms(); //延時5MS
- _data = msb << 8;
- _data |= lsb;
- return _data;
- }
- //********************************************************************
- long bmp180ReadTemp(void) //讀取未校準溫度數據
- {
-
- BMP180_Start(); //起始信號
- BMP180_SendByte(BMP180_SlaveAddress); //發送設備地址+寫信號
- BMP180_SendByte(0xF4); // write register address
- BMP180_SendByte(0x2E); // write register data for temp
- BMP180_Stop(); //發送停止信號
- Delay5ms(); //延時5MS
- // max time is 4.5ms
- return (long) Multiple_read(0xF6);
- }
- //*************************************************************
- long bmp180ReadPressure(void) //讀取未校準氣壓數據
- {
- long pressure = 0;
- BMP180_Start(); //起始信號
- BMP180_SendByte(BMP180_SlaveAddress); //發送設備地址+寫信號
- BMP180_SendByte(0xF4); // write register address
- BMP180_SendByte(0x34); // write register data for pressure
- BMP180_Stop(); //發送停止信號
- Delay5ms(); // max time is 4.5ms
-
- pressure = Multiple_read(0xF6);
- pressure &= 0x0000FFFF;
-
- return pressure;
- //return (long) bmp085ReadShort(0xF6);
- }
- //初始化BMP085,根據需要請參考pdf進行修改**************
- void Init_BMP180()
- {
- 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 conversion(long temp_data)
- {
- BMP_shiwan=temp_data/100000+0x30 ;
- temp_data=temp_data%100000; //取余運算
- BMP_wan=temp_data/10000+0x30 ;
- temp_data=temp_data%10000; //取余運算
- BMP_qian=temp_data/1000+0x30 ;
- temp_data=temp_data%1000; //取余運算
- BMP_bai=temp_data/100+0x30 ;
- temp_data=temp_data%100; //取余運算
- BMP_shi=temp_data/10+0x30 ;
- temp_data=temp_data%10; //取余運算
- BMP_ge=temp_data+0x30;
- }
- void bmp180Convert()
- {
- long ut;
- long up;
- long x1, x2, b5, b6, x3, b3, p;
- unsigned long b4, b7;
-
-
- ut = bmp180ReadTemp();
- ut = bmp180ReadTemp(); // 讀取溫度
- up = bmp180ReadPressure();
- up = bmp180ReadPressure(); // 讀取壓強
- //*************************************************************************
- x1 = ((long)ut - ac6) * ac5 >> 15; //溫度校正
- x2 = ((long) mc << 11) / (x1 + md);
- b5 = x1 + x2;
- temperature = (b5 + 8) >> 4;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
oled顯示.rar
(81.32 KB, 下載次數: 207)
2018-12-7 16:13 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|