標題: ds18b20溫度顯示 仿真及源碼 [打印本頁]

作者: R0514    時間: 2016-10-31 19:04
標題: ds18b20溫度顯示 仿真及源碼
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復位,初始化函數
  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é)函數
  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);   //讀出的數據最低位在最前面,這樣剛好一個字節(jié)在DAT里
  43.   }
  44.   return(dat);
  45. }

  46. void tmp_writebyte(uchar dat)   //write a byte to ds18b20  向1820寫一個字節(jié)數據函數
  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     讀取寄存器中存儲的溫度數據
  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表示小數點后只取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. }
復制代碼

溫度顯示.rar

77.91 KB, 下載次數: 32, 下載積分: 黑幣 -5


作者: 下從文    時間: 2016-11-14 21:35
很不錯,很有用。




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1