找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1770|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

單片機(jī)程序不知道哪里出問題了?運(yùn)行后氣壓數(shù)值不對(duì),溫度也沒有顯示

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:303971 發(fā)表于 2018-11-23 19:31 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
運(yùn)行后氣壓數(shù)值不對(duì),溫度也沒有顯示
  1. #include "bmp085.h"
  2. #include "lcd12864.h"

  3. #define        BMP085_SlaveAddress   0xee          // 定義器件在IIC總線中的從地址     
  4. #define OSS 0            // Oversampling Setting (note: code is not set up to use other OSS values)

  5. sbit          SCL = P3^5;      // I2C時(shí)鐘引腳定義
  6. sbit           SDA = P3^6;      // I2C數(shù)據(jù)引腳定義

  7. short ac1, ac2, ac3;
  8. unsigned short ac4, ac5, ac6;
  9. short b1, b2;
  10. short mb, mc, md;


  11. // 11.0592MHz  1T ----------------------------------
  12. void Delay5us(void)   
  13. {
  14.     unsigned char a;
  15.     for(a=26; a>0; a--);
  16. }

  17. // 11.0592MHz  1T ----------------------------------
  18. void Delay5ms(void)   
  19. {
  20.     unsigned char a,b,c;
  21.     for(c=7; c>0; c--)
  22.         for(b=168; b>0; b--)
  23.             for(a=22; a>0; a--);
  24. }

  25. // 起始信號(hào) --------------------------------------
  26. void I2C_Start()
  27. {
  28.     SDA = 1;                    // 拉高數(shù)據(jù)線
  29.     SCL = 1;                    // 拉高時(shí)鐘線
  30.     Delay5us();                 // 延時(shí)
  31.     SDA = 0;                    // 產(chǎn)生下降沿
  32.     Delay5us();                 // 延時(shí)
  33.     SCL = 0;                    // 拉低時(shí)鐘線
  34. }

  35. // 停止信號(hào) --------------------------------------
  36. void I2C_Stop()
  37. {
  38.     SDA = 0;                    // 拉低數(shù)據(jù)線
  39.     SCL = 1;                    // 拉高時(shí)鐘線
  40.     Delay5us();                 // 延時(shí)
  41.     SDA = 1;                    // 產(chǎn)生上升沿
  42.     Delay5us();                 // 延時(shí)
  43. }

  44. // 發(fā)送應(yīng)答信號(hào) ----------------------------------
  45. // 輸入:0-ACK  1-NACK -----------------------------
  46. void I2C_SendACK(bit ack)
  47. {
  48.     SDA = ack;                  // 寫應(yīng)答信號(hào)
  49.     SCL = 1;                    // 拉高時(shí)鐘線
  50.     Delay5us();                 // 延時(shí)
  51.     SCL = 0;                    // 拉低時(shí)鐘線
  52.     Delay5us();                 // 延時(shí)
  53. }

  54. // 接收應(yīng)答信號(hào) -----------------------------------
  55. bit I2C_RecvACK()
  56. {
  57.     SCL = 1;                    // 拉高時(shí)鐘線
  58.     Delay5us();                 // 延時(shí)
  59.     CY = SDA;                   // 讀應(yīng)答信號(hào)
  60.     SCL = 0;                    // 拉低時(shí)鐘線
  61.     Delay5us();                 // 延時(shí)

  62.     return CY;
  63. }

  64. /**
  65. * 向I2C總線發(fā)送一個(gè)字節(jié)數(shù)據(jù)
  66. */
  67. void I2C_SendByte(unsigned char dat)
  68. {
  69.     unsigned char i;

  70.     for (i=0; i<8; i++) {       // 8位計(jì)數(shù)器
  71.         dat <<= 1;              // 移出數(shù)據(jù)的最高位
  72.         SDA = CY;               // 送數(shù)據(jù)口
  73.         SCL = 1;                // 拉高時(shí)鐘線
  74.         Delay5us();             // 延時(shí)
  75.         SCL = 0;                // 拉低時(shí)鐘線
  76.         Delay5us();             // 延時(shí)
  77.     }
  78.     I2C_RecvACK();
  79. }

  80. /**
  81. * 從I2C總線接收一個(gè)字節(jié)數(shù)據(jù)
  82. */
  83. unsigned char I2C_RecvByte()
  84. {
  85.     unsigned char i;
  86.     unsigned char dat = 0;

  87.     SDA = 1;                    // 使能內(nèi)部上拉,準(zhǔn)備讀取數(shù)據(jù),
  88.     for (i=0; i<8; i++) {       // 8位計(jì)數(shù)器
  89.         dat <<= 1;              // 移出數(shù)據(jù)的最高位
  90.         SCL = 1;                // 拉高時(shí)鐘線
  91.         Delay5us();             // 延時(shí)
  92.         dat |= SDA;             // 讀數(shù)據(jù)               
  93.         SCL = 0;                // 拉低時(shí)鐘線
  94.         Delay5us();             // 延時(shí)
  95.     }
  96.     return dat;
  97. }

  98. /*
  99. * 讀出BMP085內(nèi)部數(shù)據(jù),連續(xù)兩個(gè)
  100. * 輸入:adr-寄存器地址
  101. */
  102. short BMP085_MultipleRead(unsigned char adr)
  103. {   
  104.                 unsigned char msb, lsb;
  105.                 short dat;
  106.                
  107.                 I2C_Start();                             // 起始信號(hào)
  108.                 I2C_SendByte(BMP085_SlaveAddress);       // 發(fā)送設(shè)備地址 + 寫信號(hào)
  109.                 I2C_SendByte(adr);                       // 發(fā)送存儲(chǔ)單元地址
  110.                
  111.                 I2C_Start();                             // 起始信號(hào)
  112.                 I2C_SendByte(BMP085_SlaveAddress+1);     // 發(fā)送設(shè)備地址+讀信號(hào)

  113.                 msb = I2C_RecvByte();                    // 讀取數(shù)據(jù)            
  114.                 I2C_SendACK(0);                          // ACK
  115.                 lsb = I2C_RecvByte();     
  116.                 I2C_SendACK(1);                          // 最后一個(gè)數(shù)據(jù)不應(yīng)答
  117.                 I2C_Stop();                              // 停止信號(hào)
  118.                
  119.                 Delay5ms();
  120.                 dat = msb << 8;
  121.                 dat |= lsb;        
  122.                
  123.                 return dat;
  124. }

  125. /*
  126. * 讀取溫度數(shù)據(jù)
  127. */
  128. long BMP085_ReadTemp(void)
  129. {
  130.                 I2C_Start();                         // 起始信號(hào)
  131.                 I2C_SendByte(BMP085_SlaveAddress);   // 發(fā)送設(shè)備地址+寫信號(hào)
  132.                 I2C_SendByte(0xF4);                         // 0xF4--進(jìn)行測量
  133.                 I2C_SendByte(0x2E);                        // write register data for temp
  134.                 I2C_Stop();                          // 發(fā)送停止信號(hào)
  135.                 Delay5ms();                                 // max conversion time is 4.5ms

  136.                 return (long)BMP085_MultipleRead(0xF6);  // 讀取返回值
  137. }

  138. /**
  139. * 讀取壓強(qiáng)數(shù)據(jù)
  140. */
  141. long BMP085_ReadPressure(void)
  142. {
  143.         long pressure = 0;

  144.     I2C_Start();                           // 起始信號(hào)
  145.     I2C_SendByte(BMP085_SlaveAddress);     // 發(fā)送設(shè)備地址+寫信號(hào)
  146.     I2C_SendByte(0xF4);                           // 0xF4--進(jìn)行測量
  147.     I2C_SendByte(0x34);                          // write register data for pressure
  148.     I2C_Stop();                            // 發(fā)送停止信號(hào)
  149.         Delay5ms();                                   // max conversion time is 4.5ms
  150.         pressure = BMP085_MultipleRead(0xF6);  // 讀取返回值
  151.         pressure &= 0x0000FFFF;
  152.         
  153.     return pressure;        
  154. }

  155. /**
  156. * 每個(gè)器件都有自己單獨(dú)的校準(zhǔn)系數(shù),讀取校準(zhǔn)數(shù)據(jù)。
  157. */
  158. void BMP085_Init(void)
  159. {
  160.         ac1 = BMP085_MultipleRead(0xAA);
  161.         ac2 = BMP085_MultipleRead(0xAC);
  162.         ac3 = BMP085_MultipleRead(0xAE);
  163.         ac4 = BMP085_MultipleRead(0xB0);
  164.         ac5 = BMP085_MultipleRead(0xB2);
  165.         ac6 = BMP085_MultipleRead(0xB4);
  166.         b1 =  BMP085_MultipleRead(0xB6);
  167.         b2 =  BMP085_MultipleRead(0xB8);
  168.         mb =  BMP085_MultipleRead(0xBA);
  169.         mc =  BMP085_MultipleRead(0xBC);
  170.         md =  BMP085_MultipleRead(0xBE);
  171. }

  172. /**
  173. * 數(shù)據(jù)讀取顯示
  174. */
  175. long ut, up;
  176. long x1, x2, b5, b6, x3, b3, p;
  177. unsigned long b4, b7;
  178. long  temperature, pressure;
  179. void BMP085_Convert(void)
  180. {
  181.         unsigned char i, dis_temp[9] = {0};

  182.         /* 加載溫度顯示數(shù)據(jù) */
  183.         ut = BMP085_ReadTemp();           

  184.         x1 = ((long)ut - ac6) * ac5 >> 15;
  185.         x2 = ((long) mc << 11) / (x1 + md);
  186.         b5 = x1 + x2;
  187.         temperature = (b5 + 8) >> 4;

  188.         dis_temp[0] = temperature%1000/100 + 0x30;
  189.         dis_temp[1] = temperature%100/10 + 0x30;
  190.         dis_temp[2] = '.';
  191.         dis_temp[3] = temperature%10 + 0x30;
  192.     for (i=0; i<4; i++) {
  193.                 SMS_Content[5+i] = dis_temp[i];  //加載數(shù)據(jù)到待發(fā)送短信
  194.                 // SMS_Content[50] = "Temp:22.2, Pres:101.3KPa, Humi:50%, PM2.5:30ug/m^3";   
  195.         }
  196.         LCD12864_Dis(0, 3, dis_temp);

  197.     /* 加載壓強(qiáng)顯示數(shù)據(jù) */
  198.         up = BMP085_ReadPressure();   
  199.                
  200.         b6 = b5 - 4000;
  201.         x1 = (b2 * (b6 * b6 >> 12)) >> 11;
  202.         x2 = ac2 * b6 >> 11;
  203.         x3 = x1 + x2;
  204.         b3 = (((long)ac1 * 4 + x3) + 2)/4;
  205.         x1 = ac3 * b6 >> 13;
  206.         x2 = (b1 * (b6 * b6 >> 12)) >> 16;
  207.         x3 = ((x1 + x2) + 2) >> 2;
  208.         b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
  209.         b7 = ((unsigned long) up - b3) * (50000 >> OSS);
  210.         if( b7 < 0x80000000)
  211.                 p = (b7 * 2) / b4 ;
  212.         else  
  213.                 p = (b7 / b4) * 2;
  214.         x1 = (p >> 8) * (p >> 8);
  215.         x1 = (x1 * 3038) >> 16;
  216.         x2 = (-7357 * p) >> 16;
  217.         pressure = p + ((x1 + x2 + 3791) >> 4);

  218.         if (pressure > 100000) {
  219.                 dis_temp[0] = pressure/100000 + 0x30;
  220.                 dis_temp[1] = pressure%100000/10000 + 0x30;
  221.                 dis_temp[2] = pressure%10000/1000 + 0x30;
  222.                 dis_temp[3] = '.';
  223.                 dis_temp[4] = pressure%1000/100 + 0x30;
  224.                 dis_temp[5] = pressure%100/10 + 0x30;
  225.                 dis_temp[6] = 'K';
  226.                 dis_temp[7] = 'P';
  227.                 dis_temp[8] = 'a';

  228.         } else {
  229.                 dis_temp[0] = 0x20;  // 空格
  230.                 dis_temp[1] = pressure%100000/10000 + 0x30;
  231.                 dis_temp[2] = pressure%10000/1000 + 0x30;
  232.                 dis_temp[3] = '.';
  233.                 dis_temp[4] = pressure%1000/100 + 0x30;
  234.                 dis_temp[5] = pressure%100/10 + 0x30;
  235.                 dis_temp[6] = 'K';
  236.                 dis_temp[7] = 'P';
  237.                 dis_temp[8] = 'a';
  238.         }
  239.         for (i=0; i<6; i++) {
  240.                 SMS_Content[16+i] = dis_temp[i];   
  241.         }

  242.         LCD12864_WriteCmd(0x8b);
  243.         for(i=0; i<9; i++)
  244.           LCD12864_WriteDat(dis_temp[i]);   
  245. }
復(fù)制代碼

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:434779 發(fā)表于 2018-12-16 05:57 | 只看該作者
我也是,明明 (&0f)  與  運(yùn)算后高4位該是全0零  可是  7279 的數(shù)碼管顯示還是高位有1存在,出鬼啦?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表