|
運(yùn)行后氣壓數(shù)值不對,溫度也沒有顯示
- #include "bmp085.h"
- #include "lcd12864.h"
- #define BMP085_SlaveAddress 0xee // 定義器件在IIC總線中的從地址
- #define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values)
- sbit SCL = P3^5; // I2C時鐘引腳定義
- sbit SDA = P3^6; // I2C數(shù)據(jù)引腳定義
- short ac1, ac2, ac3;
- unsigned short ac4, ac5, ac6;
- short b1, b2;
- short mb, mc, md;
- // 11.0592MHz 1T ----------------------------------
- void Delay5us(void)
- {
- unsigned char a;
- for(a=26; a>0; a--);
- }
- // 11.0592MHz 1T ----------------------------------
- void Delay5ms(void)
- {
- unsigned char a,b,c;
- for(c=7; c>0; c--)
- for(b=168; b>0; b--)
- for(a=22; a>0; a--);
- }
- // 起始信號 --------------------------------------
- void I2C_Start()
- {
- SDA = 1; // 拉高數(shù)據(jù)線
- SCL = 1; // 拉高時鐘線
- Delay5us(); // 延時
- SDA = 0; // 產(chǎn)生下降沿
- Delay5us(); // 延時
- SCL = 0; // 拉低時鐘線
- }
- // 停止信號 --------------------------------------
- void I2C_Stop()
- {
- SDA = 0; // 拉低數(shù)據(jù)線
- SCL = 1; // 拉高時鐘線
- Delay5us(); // 延時
- SDA = 1; // 產(chǎn)生上升沿
- Delay5us(); // 延時
- }
- // 發(fā)送應(yīng)答信號 ----------------------------------
- // 輸入:0-ACK 1-NACK -----------------------------
- void I2C_SendACK(bit ack)
- {
- SDA = ack; // 寫應(yīng)答信號
- SCL = 1; // 拉高時鐘線
- Delay5us(); // 延時
- SCL = 0; // 拉低時鐘線
- Delay5us(); // 延時
- }
- // 接收應(yīng)答信號 -----------------------------------
- bit I2C_RecvACK()
- {
- SCL = 1; // 拉高時鐘線
- Delay5us(); // 延時
- CY = SDA; // 讀應(yīng)答信號
- SCL = 0; // 拉低時鐘線
- Delay5us(); // 延時
- return CY;
- }
- /**
- * 向I2C總線發(fā)送一個字節(jié)數(shù)據(jù)
- */
- void I2C_SendByte(unsigned char dat)
- {
- unsigned char i;
- for (i=0; i<8; i++) { // 8位計數(shù)器
- dat <<= 1; // 移出數(shù)據(jù)的最高位
- SDA = CY; // 送數(shù)據(jù)口
- SCL = 1; // 拉高時鐘線
- Delay5us(); // 延時
- SCL = 0; // 拉低時鐘線
- Delay5us(); // 延時
- }
- I2C_RecvACK();
- }
- /**
- * 從I2C總線接收一個字節(jié)數(shù)據(jù)
- */
- unsigned char I2C_RecvByte()
- {
- unsigned char i;
- unsigned char dat = 0;
-
- SDA = 1; // 使能內(nèi)部上拉,準(zhǔn)備讀取數(shù)據(jù),
- for (i=0; i<8; i++) { // 8位計數(shù)器
- dat <<= 1; // 移出數(shù)據(jù)的最高位
- SCL = 1; // 拉高時鐘線
- Delay5us(); // 延時
- dat |= SDA; // 讀數(shù)據(jù)
- SCL = 0; // 拉低時鐘線
- Delay5us(); // 延時
- }
- return dat;
- }
- /*
- * 讀出BMP085內(nèi)部數(shù)據(jù),連續(xù)兩個
- * 輸入:adr-寄存器地址
- */
- short BMP085_MultipleRead(unsigned char adr)
- {
- unsigned char msb, lsb;
- short dat;
-
- I2C_Start(); // 起始信號
- I2C_SendByte(BMP085_SlaveAddress); // 發(fā)送設(shè)備地址 + 寫信號
- I2C_SendByte(adr); // 發(fā)送存儲單元地址
-
- I2C_Start(); // 起始信號
- I2C_SendByte(BMP085_SlaveAddress+1); // 發(fā)送設(shè)備地址+讀信號
- msb = I2C_RecvByte(); // 讀取數(shù)據(jù)
- I2C_SendACK(0); // ACK
- lsb = I2C_RecvByte();
- I2C_SendACK(1); // 最后一個數(shù)據(jù)不應(yīng)答
- I2C_Stop(); // 停止信號
-
- Delay5ms();
- dat = msb << 8;
- dat |= lsb;
-
- return dat;
- }
- /*
- * 讀取溫度數(shù)據(jù)
- */
- long BMP085_ReadTemp(void)
- {
- I2C_Start(); // 起始信號
- I2C_SendByte(BMP085_SlaveAddress); // 發(fā)送設(shè)備地址+寫信號
- I2C_SendByte(0xF4); // 0xF4--進(jìn)行測量
- I2C_SendByte(0x2E); // write register data for temp
- I2C_Stop(); // 發(fā)送停止信號
- Delay5ms(); // max conversion time is 4.5ms
- return (long)BMP085_MultipleRead(0xF6); // 讀取返回值
- }
- /**
- * 讀取壓強(qiáng)數(shù)據(jù)
- */
- long BMP085_ReadPressure(void)
- {
- long pressure = 0;
- I2C_Start(); // 起始信號
- I2C_SendByte(BMP085_SlaveAddress); // 發(fā)送設(shè)備地址+寫信號
- I2C_SendByte(0xF4); // 0xF4--進(jìn)行測量
- I2C_SendByte(0x34); // write register data for pressure
- I2C_Stop(); // 發(fā)送停止信號
- Delay5ms(); // max conversion time is 4.5ms
- pressure = BMP085_MultipleRead(0xF6); // 讀取返回值
- pressure &= 0x0000FFFF;
-
- return pressure;
- }
- /**
- * 每個器件都有自己單獨(dú)的校準(zhǔn)系數(shù),讀取校準(zhǔn)數(shù)據(jù)。
- */
- void BMP085_Init(void)
- {
- ac1 = BMP085_MultipleRead(0xAA);
- ac2 = BMP085_MultipleRead(0xAC);
- ac3 = BMP085_MultipleRead(0xAE);
- ac4 = BMP085_MultipleRead(0xB0);
- ac5 = BMP085_MultipleRead(0xB2);
- ac6 = BMP085_MultipleRead(0xB4);
- b1 = BMP085_MultipleRead(0xB6);
- b2 = BMP085_MultipleRead(0xB8);
- mb = BMP085_MultipleRead(0xBA);
- mc = BMP085_MultipleRead(0xBC);
- md = BMP085_MultipleRead(0xBE);
- }
- /**
- * 數(shù)據(jù)讀取顯示
- */
- long ut, up;
- long x1, x2, b5, b6, x3, b3, p;
- unsigned long b4, b7;
- long temperature, pressure;
- void BMP085_Convert(void)
- {
- unsigned char i, dis_temp[9] = {0};
- /* 加載溫度顯示數(shù)據(jù) */
- ut = BMP085_ReadTemp();
- x1 = ((long)ut - ac6) * ac5 >> 15;
- x2 = ((long) mc << 11) / (x1 + md);
- b5 = x1 + x2;
- temperature = (b5 + 8) >> 4;
- dis_temp[0] = temperature%1000/100 + 0x30;
- dis_temp[1] = temperature%100/10 + 0x30;
- dis_temp[2] = '.';
- dis_temp[3] = temperature%10 + 0x30;
- for (i=0; i<4; i++) {
- SMS_Content[5+i] = dis_temp[i]; //加載數(shù)據(jù)到待發(fā)送短信
- // SMS_Content[50] = "Temp:22.2, Pres:101.3KPa, Humi:50%, PM2.5:30ug/m^3";
- }
- LCD12864_Dis(0, 3, dis_temp);
- /* 加載壓強(qiáng)顯示數(shù)據(jù) */
- up = BMP085_ReadPressure();
-
- 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);
- if (pressure > 100000) {
- dis_temp[0] = pressure/100000 + 0x30;
- dis_temp[1] = pressure%100000/10000 + 0x30;
- dis_temp[2] = pressure%10000/1000 + 0x30;
- dis_temp[3] = '.';
- dis_temp[4] = pressure%1000/100 + 0x30;
- dis_temp[5] = pressure%100/10 + 0x30;
- dis_temp[6] = 'K';
- dis_temp[7] = 'P';
- dis_temp[8] = 'a';
- } else {
- dis_temp[0] = 0x20; // 空格
- dis_temp[1] = pressure%100000/10000 + 0x30;
- dis_temp[2] = pressure%10000/1000 + 0x30;
- dis_temp[3] = '.';
- dis_temp[4] = pressure%1000/100 + 0x30;
- dis_temp[5] = pressure%100/10 + 0x30;
- dis_temp[6] = 'K';
- dis_temp[7] = 'P';
- dis_temp[8] = 'a';
- }
- for (i=0; i<6; i++) {
- SMS_Content[16+i] = dis_temp[i];
- }
- LCD12864_WriteCmd(0x8b);
- for(i=0; i<9; i++)
- LCD12864_WriteDat(dis_temp[i]);
- }
復(fù)制代碼
|
|