找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

ds18b20溫度顯示 仿真及源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:143558 發(fā)表于 2016-10-31 19:04 | 只看該作者 |只看大圖 回帖獎勵 |正序瀏覽 |閱讀模式
lcd1602顯示,只能顯示正溫度


單片機程序源碼:
  1. #include<reg52.h>
  2. #include  "Cry1602.h"
  3. #include  "Cry1602.c"
  4. #define out  P1
  5. #define uint unsigned int
  6. #define uchar unsigned char
  7. #define set_DQ P3|=0x80;
  8. #define clr_DQ P3&=~0x80;
  9. uchar i=0,t=0,j;
  10. uchar count[2]=0;
  11. void delay(uint z)
  12. {
  13.         uint x,y;
  14.         for(x=z;x>0;x--)
  15.                 for(y=114;y>0;y--);
  16. }
  17. void tmp_reset(void)       //send tmp_reset and initialization command 18B20復(fù)位,初始化函數(shù)
  18. {
  19.   uint i;
  20.   clr_DQ;
  21.   for(i=103;i>0;i--);
  22.   set_DQ;
  23. for(i=4;i>0;i--);
  24. }

  25. bit tmp_read1bit(void)      
  26. {
  27.    uint i;
  28.    bit dat;
  29.    clr_DQ;i++;         
  30.    set_DQ;i++;i++;
  31.    dat=P3&0x80;
  32.    for(i=8;i>0;i--);
  33.    return (dat);
  34. }

  35. uchar tmp_readbyte(void)   //read a byte date   讀1字節(jié)函數(shù)
  36. {
  37.   uchar i,j,dat;
  38.   dat=0;
  39.   for(i=1;i<=8;i++)
  40.   {
  41.     j=tmp_read1bit();
  42.     dat=(j<<7)|(dat>>1);   //讀出的數(shù)據(jù)最低位在最前面,這樣剛好一個字節(jié)在DAT里
  43.   }
  44.   return(dat);
  45. }

  46. void tmp_writebyte(uchar dat)   //write a byte to ds18b20  向1820寫一個字節(jié)數(shù)據(jù)函數(shù)
  47. {
  48.   uint i;
  49.   uchar j;
  50.   bit testb;
  51.   for(j=1;j<=8;j++)
  52.   {
  53.     testb=dat&0x01;
  54.     dat=dat>>1;
  55.     if(testb)     //write 1
  56.     {
  57.       clr_DQ;
  58.       i++;i++;
  59.       set_DQ;
  60.       for(i=8;i>0;i--);
  61.     }
  62.     else
  63.     {
  64.       clr_DQ;       //write 0
  65.       for(i=8;i>0;i--);
  66.       set_DQ;
  67.       i++;i++;
  68.     }

  69.   }
  70. }
  71. uint get_tmp()               //get the temperature     讀取寄存器中存儲的溫度數(shù)據(jù)
  72. {
  73.   float tt;
  74.   uchar a,b;
  75.   uint temp;

  76.   tmp_reset();
  77.   delay(1);
  78.   tmp_writebyte(0xcc);  // address all drivers on bus  寫跳過讀ROM指令
  79.   tmp_writebyte(0x44);

  80.   tmp_reset();
  81.   delay(1);
  82.   tmp_writebyte(0xcc);
  83.   tmp_writebyte(0xbe);
  84.   a=tmp_readbyte();   //讀低8位
  85.   b=tmp_readbyte();   //讀高8位
  86.   temp=b;
  87.   temp<<=8;             //two byte  compose a int variable   兩個字節(jié)組合為1個字
  88.   temp=temp|a;
  89.   tt=temp*0.0625;   //溫度在寄存器中是12位,分辨率是0.0625
  90.   temp=(tt*10+0.5)/10;  //乘10表示小數(shù)點后只取1位,加0.5是四折五入
  91.   return temp;
  92. }



  93. void timer_init();

  94. void main()
  95. {
  96.         timer_init();
  97.         LcdReset();
  98.         DispStr(0x80,0,"welcom!");
  99.         while(1)
  100.         {
  101.                 temp=get_tmp();
  102.                 for(j=0;j<2;j++)
  103.                 {
  104.                         count[1-j]=temp%10+0x30;
  105.                         temp/=10;
  106.                 }
  107.         DispStr(0x85,1,count);
  108.         }
  109. }
  110. void timer_init()
  111. {
  112. EA=1;
  113.         ET1=1;
  114.         TR1=1;
  115.         TMOD=0x10;
  116.         TH1=(65536-10000)/256;
  117.         TL1=(65536-10000)%256;
  118. }
復(fù)制代碼

溫度顯示.rar

77.91 KB, 下載次數(shù): 32, 下載積分: 黑幣 -5

評分

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

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:147898 發(fā)表于 2016-11-14 21:35 | 只看該作者
很不錯,很有用。
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

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