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

          新聞中心

          stc12c5A60s2用ds18b20測溫

          作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏
          //溫度顯示程序==LED顯示,精度0.1攝氏度

          //晶振:12M
          //單片機(jī)stc12c5a60s2 1T
          /******************************************
          跳線設(shè)置:默認(rèn)
          注意事項(xiàng):ds18b20切勿插反,有爆炸燙傷的危險(xiǎn),方向是ds18b20的平面(有字的一面)朝旁邊的三極管Q4
          ***/
          #include
          #include
          #define uchar unsigned char
          #define uint unsigned int
          //數(shù)據(jù)口define interface
          sbit dula = P2^6; //數(shù)碼管段選
          sbit wela = P2^7; //數(shù)碼管位選
          sbit DQ = P1^7; //18b20接口

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

          uint wendu; //溫度值 variable of wenduerature

          //不帶小數(shù)點(diǎn)
          unsigned char code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
          0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
          //帶小數(shù)點(diǎn)
          unsigned char code table1[] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};


          /*************精確延時(shí)函數(shù)**************** */
          // 數(shù)據(jù)表如下
          /*
          ********************************************************************************************************************
          延時(shí)時(shí)間 a的值 b的值 c的值 延時(shí)誤差(us)
          10us 1 1 1 -0.5
          20us 1 1 8 0
          30us 1 1 15 +0.5
          40us 2 1 9 0
          50us 1 1 28 0
          60us 1 1 35 +0.5
          70us 1 1 42 +1
          80us 1 1 48 0
          90us 1 1 55 +0.5
          100us 1 1 61 -0.5
          200us 1 1 128 0
          300us 3 1 63 +1.5
          400us 2 1 129 0
          500us 5 1 63 +0.5
          600us 6 1 63 0
          700us 7 1 63 -0.5
          800us 1 3 175 +0.5
          900us 9 1 63 -1.5
          1ms 1 3 219 -1.5
          2ms 2 3 220 +3
          3ms 3 3 220 +3
          Xms X 3 220 +3
          (X的范圍為2到255)
          */

          void Delay(unsigned char a1,b1,c1)
          {
          unsigned char a,b,c;
          for(a=0;afor(b=0;bfor(c=0;c}

          /*****************DS18B20******************/
          void Init_Ds18b20(void) //DS18B20初始化send reset and initialization command
          {
          DQ = 1; //DQ復(fù)位,不要也可行。
          Delay(1,1,1); //稍做延時(shí) 10us
          DQ = 0; //單片機(jī)拉低總線
          Delay(6,1,63); //600 us //精確延時(shí),維持至少480us
          //Delay(1,1,15); //20us
          DQ = 1; //釋放總線,即拉高了總線
          Delay(5,1,63); //500us //此處延時(shí)有足夠,確保能讓DS18B20發(fā)出存在脈沖。
          }

          uchar Read_One_Byte() //讀取一個(gè)字節(jié)的數(shù)據(jù)read a byte date
          //讀數(shù)據(jù)時(shí),數(shù)據(jù)以字節(jié)的最低有效位先從總線移出
          {
          uchar i = 0;
          uchar dat = 0;
          for(i=8;i>0;i--)
          {
          DQ = 0; //將總線拉低,要在1us之后釋放總線
          //單片機(jī)要在此下降沿后的15us內(nèi)讀數(shù)據(jù)才會有效。
          _nop_(); //至少維持了1us,表示讀時(shí)序開始
          dat >>= 1; //讓從總線上讀到的位數(shù)據(jù),依次從高位移動到低位。
          DQ = 1; //釋放總線,此后DS18B20會控制總線,把數(shù)據(jù)傳輸?shù)娇偩€上
          Delay(1,1,1); //延時(shí)10us,此處參照推薦的讀時(shí)序圖,盡量把控制器采樣時(shí)間放到讀時(shí)序后的15us內(nèi)的最后部分
          if(DQ) //控制器進(jìn)行采樣
          {
          dat |= 0x80; //若總線為1,即DQ為1,那就把dat的最高位置1;若為0,則不進(jìn)行處理,保持為0
          }
          Delay(1,1,8); //20us //此延時(shí)不能少,確保讀時(shí)序的長度60us。
          }
          return (dat);
          }

          void Write_One_Byte(uchar dat)
          {
          uchar i = 0;
          for(i=8;i>0;i--)
          {
          DQ = 0; //拉低總線
          _nop_(); //至少維持了1us,表示寫時(shí)序(包括寫0時(shí)序或?qū)?時(shí)序)開始
          DQ = dat&0x01; //從字節(jié)的最低位開始傳輸
          //指令dat的最低位賦予給總線,必須在拉低總線后的15us內(nèi),
          //因?yàn)?5us后DS18B20會對總線采樣。
          Delay(1,1,15); //必須讓寫時(shí)序持續(xù)至少60us
          DQ = 1; //寫完后,必須釋放總線,
          dat >>= 1;
          Delay(1,1,1);
          }
          }


          uint Get_Tmp() //獲取溫度get the wenduerature
          {
          float tt;
          uchar a,b;
          Init_Ds18b20(); //初始化
          Write_One_Byte(0xcc); //忽略ROM指令
          Write_One_Byte(0x44); //溫度轉(zhuǎn)換指令
          Init_Ds18b20(); //初始化
          Write_One_Byte(0xcc); //忽略ROM指令
          Write_One_Byte(0xbe); //讀暫存器指令
          a = Read_One_Byte(); //讀取到的第一個(gè)字節(jié)為溫度LSB
          b = Read_One_Byte(); //讀取到的第一個(gè)字節(jié)為溫度MSB
          wendu = b; //先把高八位有效數(shù)據(jù)賦于wendu
          wendu <<= 8; //把以上8位數(shù)據(jù)從wendu低八位移到高八位
          wendu = wendu|a; //兩字節(jié)合成一個(gè)整型變量
          tt = wendu*0.0625; //得到真實(shí)十進(jìn)制溫度值
          //因?yàn)镈S18B20可以精確到0.0625度
          //所以讀回?cái)?shù)據(jù)的最低位代表的是0.0625度
          wendu = tt*10+0.5; //放大十倍
          //這樣做的目的將小數(shù)點(diǎn)后第一位也轉(zhuǎn)換為可顯示數(shù)字
          //同時(shí)進(jìn)行一個(gè)四舍五入操作。
          return wendu;
          }


          /****************數(shù)碼碼動態(tài)顯示函數(shù)**************/

          void Display(uint wendu) //顯示程序
          {
          uchar A1,A2,A3;
          A1 = wendu/100; //百位
          A2 = wendu%100/10; //十位
          A3 = wendu%10; //個(gè)位

          P0 = table[A1]; //顯示百位
          dula = 1; //打開段選,對應(yīng)74573的鎖存位,高電平不鎖存
          dula = 0;
          P0 = 0xFe;
          wela = 1; //打開位選
          wela = 0;
          Delay(5,3,220);


          P0 = table1[A2]; //顯示十位,使用的是有小數(shù)點(diǎn)的數(shù)組(因?yàn)閣endu值擴(kuò)大了10倍,雖然是十位,實(shí)際為個(gè)位)
          dula = 1;
          dula = 0;
          P0 = 0xFd;
          wela = 1;
          wela = 0;
          Delay(5,3,220);

          P0 = table[A3]; //顯示個(gè)位
          dula = 1;
          dula = 0;
          P0 = 0xFb;
          wela = 1;
          wela = 0;
          Delay(5,3,220);
          }


          void main()
          {
          while(1)
          {
          Display(Get_Tmp());
          }
          }



          關(guān)鍵詞: stc12c5A60s2ds18b20測

          評論


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