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

          新聞中心

          基于C8051F的18B20程序

          作者: 時(shí)間:2016-12-01 來(lái)源:網(wǎng)絡(luò) 收藏
          #include "C8051F410.h"

          #include
          #define uint unsigned int
          #define uchar unsigned char

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

          sbit DQ = P2^4 ;

          void delay(unsigned int i)
          {
          while(i--);
          }
          Init_DS18B20(void)
          {
          unsigned char x=0;
          DQ = 1; //DQ復(fù)位
          delay(176); //稍做延時(shí)
          DQ = 0; //單片機(jī)將DQ拉低
          delay(1760); //精確延時(shí) 大于 480us
          DQ = 1; //拉高總線
          delay(308);
          x=DQ; //稍做延時(shí)后 如果x=0則初始化成功 x=1則初始化失敗
          delay(440);
          }

          uchar 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(88);
          }
          return(dat);
          }

          WriteOneChar(unsigned char dat)
          {
          unsigned char i=0;
          for (i=8; i>0; i--)
          {
          DQ = 0;
          DQ = dat&0x01;
          delay(110);
          DQ = 1;
          dat>>=1;
          }
          }

          uint ReadTemperature(void) //溫度*10
          {
          uchar a=0;
          uchar b=0;
          uint temp=0;
          uint t=0;
          int temple; /*存放讀取的溫度值 將其除以16即為得到的值*/
          Init_DS18B20();
          WriteOneChar(0xCC); // 跳過(guò)讀序號(hào)列號(hào)的操作
          WriteOneChar(0x44); // 啟動(dòng)溫度轉(zhuǎn)換
          Init_DS18B20();
          WriteOneChar(0xCC); //跳過(guò)讀序號(hào)列號(hào)的操作
          WriteOneChar(0xBE); //讀取溫度寄存器等(共可讀9個(gè)寄存器) 前兩個(gè)就是溫度
          a=ReadOneChar(); //低8位
          b=ReadOneChar(); //高8位
          temp=b;
          temp<<=8;
          temp=temp|a;
          temp&=0x07ff;
          if( (b&0x08))
          {
          temple=~temp+1; //如果為負(fù)溫則去除其補(bǔ)碼
          FWD=0; /*表示溫度為負(fù)數(shù)*/
          }
          else
          {
          temple=temp;
          FWD=1; /*表示溫度為正數(shù)*/
          }
          t=temple*10/16;
          //t = temp;
          //t= t*625;
          //t = t/1000;
          return(t);
          }



          關(guān)鍵詞: C8051F18B20程

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