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

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單總線協(xié)議(ds18b20)讀寫詳解

          單總線協(xié)議(ds18b20)讀寫詳解

          作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
          void d18b20_x(unsigned char dat) //寫 8 位 數(shù) 據(jù)
          {
          unsigned char i;
          for(i=0;i<8;i++) //8位計(jì)數(shù)器
          {
          DQ = 0; //拉低總線
          DQ = dat & 0x01; //取最低位賦值給總線
          Delay(3); //延時(shí)45us
          DQ = 1; //拉過總線準(zhǔn)備寫下一個(gè)數(shù)據(jù)(或者總線復(fù)位)
          dat >>= 1; //數(shù)據(jù)右移一位
          }
          }
          unsigned char d18b20_d() //讀 8 位 數(shù) 據(jù)
          {
          unsigned char i,dat=0;
          for(i=0;i<8;i++) //8位計(jì)數(shù)器
          {
          DQ = 0; //拉低總線
          dat >>= 1; //數(shù)據(jù)右移一位
          DQ = 1; //拉過總線(準(zhǔn)備讀取數(shù)據(jù))
          if(DQ) //判斷是否是 1 如果是就把數(shù)據(jù)賦值給變量的高位
          dat |= 0x80;
          Delay(4);
          }
          return dat; //返回讀取到數(shù)據(jù)數(shù)據(jù)
          }
          unsigned int wd() //讀取溫度函數(shù)
          {
          unsigned char i = 0; //低8位數(shù)據(jù)
          unsigned char j = 0; //高8位數(shù)據(jù)
          unsigned int k = 0; //無符號(hào)16整形用來存儲(chǔ)讀回來的 16位溫度數(shù)據(jù)(j和i組合后的數(shù)據(jù))
          d18b20_qs(); //初始化
          d18b20_x(0xCC); //跳過序列號(hào)的操作(因?yàn)?8b20在總線上可以掛很多個(gè),這個(gè)序列號(hào)和網(wǎng)卡MAC地址類似)
          d18b20_x(0x44); //開啟溫度轉(zhuǎn)換
          Delay(200); //開啟溫度轉(zhuǎn)換需要時(shí)間這里延時(shí)一下
          d18b20_qs(); //初始化
          d18b20_x(0xCC); //跳過序列號(hào)的操作(因?yàn)?8b20在總線上可以掛很多個(gè),這個(gè)序列號(hào)和網(wǎng)卡MAC地址類似)
          d18b20_x(0xBE); //讀取溫度寄存器等(共可讀9個(gè)寄存器) 前兩個(gè)就是溫度
          i = d18b20_d(); //讀取低8位
          j = d18b20_d(); //讀取高8位
          k = j;
          k <<= 8;
          k = k + i;
          return k; //返回讀取到的16位數(shù)據(jù)
          }
          void CSH (void) //初始化串口
          {
          SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
          TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重裝
          TH1 = 0xFD; // TH1: 重裝值 9600 波特率晶振11.0592MHz
          TR1 = 1; // TR1: timer 1 打開
          EA = 1; //打開總中斷
          //ES = 1; //打開串口中斷
          }
          void SendByte(unsigned char dat) //發(fā)送一個(gè)字符
          {
          SBUF = dat; //SBUF 串行數(shù)據(jù)緩沖器
          while(!TI); //TI發(fā)送中斷標(biāo)志位 (當(dāng)數(shù)據(jù)發(fā)送完畢后由硬件置 1 否則等待硬件置 1)
          TI = 0;
          }
          void main()
          {
          unsigned char i,j;
          unsigned int w;
          CSH();
          while(1)
          {
          w = wd();
          i= w & 0xff; //取低8位
          j= (w >> 8)&0xff; //取高8位
          SendByte(j); //通過串口把高8位數(shù)據(jù)返回給上位機(jī)
          SendByte(i); //通過串口把低8位數(shù)據(jù)返回給上位機(jī)
          P1 = j; //使用8個(gè)LED 輸出高8位數(shù)據(jù)
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          P1 = i; //使用8個(gè)LED輸出低8位數(shù)據(jù)
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          Delay(200); //延時(shí)3毫秒
          }
          }
          3、總結(jié)

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

          1)使用的是11.0592的晶振
          2)使用下面的公式可以計(jì)算出攝氏度的溫度
          wd :讀取到的16位數(shù)據(jù)
          攝氏度 = wd x 0.0625



          上一頁(yè) 1 2 下一頁(yè)

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