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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于ds18b20溫度傳感器的51單片C語言程序

          基于ds18b20溫度傳感器的51單片C語言程序

          作者: 時(shí)間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
          基于ds18b20溫度傳感器51單片C語言程序

          #include

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

          #define uchar unsigned char

          #define uint unsigned int

          sbit DQ=P1^1;

          uchar code SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

          uchar tplsb,tpmsb;

          /*延時(shí)tms*/

          void delay(uint t)

          {

          uint i;

          while(t--)

          {

          for (i=0;i<125;i++)

          {;}

          }

          }

          /* 產(chǎn)生復(fù)位脈沖初始化DS18B20 */

          void TxReset(void)

          {

          uint i;

          DQ=0;

          /* 拉低約900us */

          i=100;

          while (i>0) i--;

          DQ=1;

          i=4;

          while (i>0) i--;

          }

          /* 等待應(yīng)答脈沖 */

          void RxWait (void)

          {

          uint i;

          while(DQ)

          while(~DQ)

          i=4;

          while (i>0) i--;

          }

          /* 讀取數(shù)據(jù)的一位,滿足讀時(shí)隙要求 */

          bit RdBit(void)

          {

          uint i;

          bit b;

          DQ=0;

          i++;

          DQ=1;

          i++;

          i++;

          b=DQ;

          i=8;

          while(i>0) i--;

          return(b);

          }

          /* 讀取數(shù)據(jù)的一個(gè)字節(jié) */

          uchar RdByte(void)

          {

          uchar i,j,b;

          b = 0;

          for (i=1;i<=8;i++)

          {

          j = RdBit();

          b = (j<<7)|(b>>1);

          }

          return(b);

          }

          /* 寫數(shù)據(jù)的一個(gè)字節(jié),滿足寫1和寫0的時(shí)隙要求 */

          void WrByte(uchar b)

          {

          uint i;

          uchar j;

          bit btmp;

          for(j=1;j<=8;j++)

          {

          btmp = b&0x01;

          b = b>>1;

          if (btmp)

          {

          /* 寫1 */

          DQ = 0;

          i++;i++;// 延時(shí),使得15us以內(nèi)拉高

          DQ = 1;

          i = 8;

          while(i>0) i--;// 整個(gè)寫1時(shí)隙不低于60us

          }

          else

          {

          /* 寫0 */

          DQ = 0;

          i = 8;

          while(i>0) i--;// 保持低在60us到120us之間

          DQ = 1;

          i++;

          i++;

          }

          }

          }

          /* 啟動(dòng)溫度轉(zhuǎn)換 */

          void convert(void)

          {

          TxReset();// 產(chǎn)生復(fù)位脈沖,初始化DS18B20

          RxWait();// 等待DS18B20給出應(yīng)答脈沖

          delay(1);// 延時(shí)

          WrByte(0xcc);// skip rom 命令

          WrByte(0x44);// convert T 命令

          }

          /* 讀取溫度值 */

          void RdTemp(void)

          {

          TxReset();// 產(chǎn)生復(fù)位脈沖,初始化DS18B20

          RxWait();// 等待DS18B20給出應(yīng)答脈沖

          delay(1);// 延時(shí)

          WrByte(0xcc);// skip rom 命令

          WrByte(0xbe);// read scratchpad 命令

          tplsb = RdByte();// 溫度值低位字節(jié)(其中低4位為二進(jìn)制的“小數(shù)”部分)

          tpmsb = RdByte();// 高位值高位字節(jié)(其中高5位為符號(hào)位)

          }

          void send (void)

          { uchar b,c;

          uint i,j;

          b=(tpmsb<<4)|(tplsb>>4);

          for(j=800;j>=0;j--)

          {

          if(j!=0)

          {

          p1=0x80;

          delay(2);

          p1=0x00;

          delay(2);

          p1=0x80;

          delay(2);

          for(i=8;i>>=1;i--)

          { c=b;

          c<

          P1=c;// P1.7輸出

          delay(4);

          }

          }

          else return;}

          }

          /* 主程序,讀取的溫度值最終存放在tplsb和tpmsb變量中。

          tplsb其中低4位為二進(jìn)制的“小數(shù)”部分;tpmsb其中高

          5位為符號(hào)位。真正通過數(shù)碼管輸出時(shí),需要進(jìn)行到十進(jìn)

          制有符號(hào)實(shí)數(shù)(包括小數(shù)部分)的轉(zhuǎn)換。 */

          void main(void)

          {

          do

          {

          delay(1);// 延時(shí)1ms

          convert();// 啟動(dòng)溫度轉(zhuǎn)換,需要750ms

          delay(1000);// 延時(shí)1s

          RdTemp();// 讀取溫度

          delay(1);

          send();

          }

          while(1);

          }



          評(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); })();