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

          新聞中心

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

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

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

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

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

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

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

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

          //寫一個(gè)字節(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); // 跳過讀序號(hào)列號(hào)的操作
          WriteOneChar(0x44); // 啟動(dòng)溫度轉(zhuǎn)換
          Init_DS18B20();
          WriteOneChar(0xCC); //跳過讀序號(hào)列號(hào)的操作
          WriteOneChar(0xBE); //讀取溫度寄存器等(共可讀9個(gè)寄存器) 前兩個(gè)就是溫度
          a=ReadOneChar();
          b=ReadOneChar();
          t=b;
          t<<=8;
          t=t|a;
          tt=t*0.0625; //將溫度的高位與低位合并
          t= tt*10+0.5; //對(duì)結(jié)果進(jìn)行4舍5入
          return(t);
          }

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

          //QQ單片機(jī)私有函數(shù),在其他的電路板上可能會(huì)不好使
          //在8個(gè)數(shù)碼管上顯示任意字符,字符內(nèi)容由8個(gè)參數(shù)LED1--LED8決定 如果顯示的字符為空,則不對(duì)這一個(gè)數(shù)碼管進(jìn)行掃描以縮短掃描時(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(); //讀取當(dāng)前溫度
          ShowAny(0,0,0,0,0,Num[i/100],Num[i/10%10] | 0x80,Num[i%10]); //顯示當(dāng)前溫度
          }
          }



          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁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); })();