<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 單片機中使用DS18B20溫度傳感器C語言程序(參考3)

          單片機中使用DS18B20溫度傳感器C語言程序(參考3)

          作者: 時間:2016-11-18 來源:網(wǎng)絡 收藏
          #include"reg51.h"

          sbit DQ =P3^7; //定義通信端口

          本文引用地址:http://www.ex-cimer.com/article/201611/315676.htm

          //延時函數(shù)
          void delay(unsigned int i)
          {
          while(i--);
          }

          //初始化函數(shù)
          Init_DS18B20(void)
          {
          unsigned char x=0;
          DQ = 1; //DQ復位
          delay(8); //稍做延時
          DQ = 0; //單片機將DQ拉低
          delay(80); //精確延時 大于 480us
          DQ = 1; //拉高總線
          delay(14);
          x=DQ; //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗
          delay(20);
          }

          //讀一個字節(jié)
          ReadOneChar(void)
          {
          unsigned char i=0;
          unsigned char dat = 0;
          for (i=8;i>0;i--)
          {
          DQ = 0; // 給脈沖信號
          dat>>=1;
          DQ = 1; // 給脈沖信號
          if(DQ)
          dat|=0x80;
          delay(4);
          }
          return(dat);
          }

          //寫一個字節(jié)
          WriteOneChar(unsigned char dat)
          {
          unsigned char i=0;
          for (i=8; i>0; i--)
          {
          DQ = 0;
          DQ = dat&0x01;
          delay(5);
          DQ = 1;
          dat>>=1;
          }
          }

          //讀取溫度
          ReadTemperature(void)
          {
          unsigned char a=0;
          unsigned char b=0;
          unsigned int t=0;
          float tt=0;
          Init_DS18B20();
          WriteOneChar(0xCC); // 跳過讀序號列號的操作
          WriteOneChar(0x44); // 啟動溫度轉(zhuǎn)換
          Init_DS18B20();
          WriteOneChar(0xCC); //跳過讀序號列號的操作
          WriteOneChar(0xBE); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
          a=ReadOneChar();
          b=ReadOneChar();
          t=b;
          t<<=8;
          t=t|a;
          tt=t*0.0625; //將溫度的高位與低位合并
          t= tt*10+0.5; //對結(jié)果進行4舍5入
          return(t);
          }

          //分別從0-9的數(shù)碼編碼,注意此編碼只適合QQ單片機實驗板,其他電路板上的編碼請參考上面的算法自行計算。
          unsigned char code Num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

          //QQ單片機私有函數(shù),在其他的電路板上可能會不好使
          //在8個數(shù)碼管上顯示任意字符,字符內(nèi)容由8個參數(shù)LED1--LED8決定 如果顯示的字符為空,則不對這一個數(shù)碼管進行掃描以縮短掃描時間
          ShowAny(unsigned char LED1,unsigned char LED2,unsigned char LED3,unsigned char LED4,unsigned char LED5,unsigned char LED6,unsigned char LED7,unsigned char LED8)
          {
          if(LED1) { P2=0xEF; P0=LED1; delay(100); }
          if(LED2) { P2=0xDF; P0=LED2; delay(100); }
          if(LED3) { P2=0xBF; P0=LED3; delay(100); }
          if(LED4) { P2=0x7F; P0=LED4; delay(100); }
          if(LED5) { P2=0xFE; P0=LED5; delay(100); }
          if(LED6) { P2=0xFD; P0=LED6; delay(500); }
          if(LED7) { P2=0xFB; P0=LED7; delay(500); }
          if(LED8) { P2=0xF7; P0=LED8; delay(100); }
          }

          main()
          {
          unsigned int i=0;
          while(1)
          {
          i=ReadTemperature(); //讀取當前溫度
          ShowAny(0,0,0,0,0,Num[i/100],Num[i/10%10] | 0x80,Num[i%10]); //顯示當前溫度
          }
          }



          評論


          技術專區(qū)

          關閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();