找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 2579|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

DS18B20應(yīng)用,模數(shù)轉(zhuǎn)換顯示溫度

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主

通過DS18B20的溫度檢測,模數(shù)轉(zhuǎn)化顯示相對應(yīng)的溫度。

仿真圖,如圖所示。





代碼源程序如下:
  1. #include "reg51.h"
  2. #include "intrins.h"
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. #define out P0

  6. sbit smg1=out^4;
  7. sbit smg2=out^5;
  8. sbit DQ=P3^7;
  9. void delay5(uchar);
  10. void init_ds18b20(void);
  11. uchar readbyte(void);
  12. void writebyte(uchar);
  13. uchar retemp(void);

  14. void main(void)                                              
  15. {
  16.         uchar i,temp;
  17.         delay5(1000);
  18.         while(1)
  19.         {
  20.                  temp=retemp();       
  21.                  for(i=0;i<10;i++)                       
  22.                  {
  23.                          out=(temp/10)&0x0f;
  24.                          smg1=0;
  25.                          smg2=1;
  26.                          delay5(1000);                       
  27.                          out=(temp%10)&0x0f;
  28.                          smg1=1;
  29.                          smg2=0;
  30.                          delay5(1000);                       
  31.                  }
  32.         }
  33. }

  34. void delay5(uchar n)                       
  35. {
  36.          do
  37.          {
  38.                  _nop_();
  39.                  _nop_();
  40.                  _nop_();
  41.                  n--;
  42.          }
  43.          while(n);
  44. }

  45. void init_ds18b20(void)               
  46. {
  47.          uchar x=0;
  48.          DQ =0;   
  49.          delay5(120);
  50.          DQ =1;   
  51.          delay5(16);
  52.          delay5(80);
  53. }

  54. uchar readbyte(void)                        
  55. {
  56.         uchar i=0;
  57.         uchar date=0;
  58.         for (i=8;i>0;i--)
  59.         {
  60.                   DQ =0;
  61.                   delay5(3);       
  62.                         DQ =1;
  63.                   date>>=1;
  64.                   if(DQ)
  65.                   date|=0x80;       
  66.                   delay5(11);
  67.          }
  68.         return(date);
  69. }
  70. void writebyte(uchar dat)                        
  71. {
  72. uchar i=0;
  73. for(i=8;i>0;i--)
  74. {
  75.           DQ =0;
  76.           DQ =dat&0x01;                               
  77.           delay5(12);                                  
  78.           DQ = 1;          
  79.           dat>>=1;
  80.           delay5(5);
  81.           }
  82. }
  83. uchar retemp(void)                       
  84. {
  85.         uchar a,b,tt;
  86.         uint t;
  87.         init_ds18b20();
  88.         writebyte(0xCC);
  89.         writebyte(0x44);
  90.         init_ds18b20();
  91.         writebyte(0xCC);
  92.         writebyte(0xBE);
  93.         a=readbyte();
  94.         b=readbyte();
  95.         t=b;
  96.         t<<=8;
  97.         t=t|a;
  98.         tt=t*0.0625;
  99.         return(tt);
  100. }
復(fù)制代碼

所有文件在下方,需要自取。
DS18B20應(yīng)用.zip (83.46 KB, 下載次數(shù): 16)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:557582 發(fā)表于 2019-6-8 10:19 | 只看該作者
謝謝了 這個(gè)正好需要
回復(fù)

使用道具 舉報(bào)

板凳
ID:122829 發(fā)表于 2019-6-8 12:12 | 只看該作者
我做的能顯示小數(shù)點(diǎn)后1位
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表