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

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)ds18b20配合1602屏顯示溫度

          51單片機(jī)ds18b20配合1602屏顯示溫度

          作者: 時(shí)間:2016-12-01 來(lái)源:網(wǎng)絡(luò) 收藏
          1、效果圖

          2、代碼
          3、小結(jié)重點(diǎn)

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


          效果圖:直接在51單片機(jī)開發(fā)板上面實(shí)現(xiàn)的.


          代 碼
          #include
          #include
          sbit RS = P2^4; //定義端口
          sbit RW = P2^5;
          sbit EN = P2^6;
          sbit DQ = P2^0; //定義總線的I/O管腳
          void SendByte(unsigned char dat);
          void zf_1602(unsigned char x,unsigned char y,unsigned dat);
          void Delay4us() //延時(shí)4us
          {
          ;
          }
          void Delay(unsigned char j) //一個(gè)循環(huán)15us
          {
          unsigned char i;
          while(j--)
          {
          i = 5;
          while (--i);
          }
          }
          bit d18b20_qs() //18b20 起始
          {
          bit dat;
          DQ = 1; //DQ復(fù)位
          Delay4us();
          DQ = 0; //拉低總線
          Delay(35); //這里延時(shí)大概 525us
          DQ = 1; //拉高總線
          Delay(2); //這里延時(shí)大概 30us
          dat = DQ; //讀取返回值(0:有18b20存在 1:是沒(méi)有)
          Delay(2);
          return dat; //返回?cái)?shù)值
          }
          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; //拉過(guò)總線準(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; //拉過(guò)總線(準(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; //無(wú)符號(hào)16整形用來(lái)存儲(chǔ)讀回來(lái)的 16位溫度數(shù)據(jù)(j和i組合后的數(shù)據(jù))
          d18b20_qs(); //初始化
          d18b20_x(0xCC); //跳過(guò)序列號(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); //跳過(guò)序列號(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 zh(unsigned int i) //1602顯示緩存寫入函數(shù)
          {
          unsigned char x,z;
          x = i & 0x0f; //取出小數(shù)
          i >>=4;
          switch(x) //小數(shù)位轉(zhuǎn)換
          {
          case 0: z=0;break;
          case 1: z=1;break;
          case 2: z=1;break;
          case 3: z=2;break;
          case 4: z=3;break;
          case 5: z=3;break;
          case 6: z=4;break;
          case 7: z=4;break;
          case 8: z=5;break;
          case 9: z=6;break;
          case 10: z=6;break;
          case 11: z=7;break;
          case 12: z=8;break;
          case 13: z=8;break;
          case 14: z=9;break;
          case 15: z=9;break;
          }
          z = z +48; //轉(zhuǎn)換成ascii碼
          zf_1602(5,1,z); //寫入1602緩存
          z = i & 0xff; //取出整數(shù)
          x = z/10; //取出十位
          x= x+48; //轉(zhuǎn)換成ascii碼
          zf_1602(2,1,x); //寫入1602緩存
          x = z%10; //取出個(gè)位
          x= x+48; //轉(zhuǎn)換成ascii碼
          zf_1602(3,1,x); //寫入1602緩存
          }


          上一頁(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); })();