標(biāo)題: STC12 1T單片機(jī)DS18B20溫度檢測(cè)仿真+代碼+原理圖 [打印本頁(yè)]

作者: yuchenggong2016    時(shí)間: 2018-11-21 22:42
標(biāo)題: STC12 1T單片機(jī)DS18B20溫度檢測(cè)仿真+代碼+原理圖
新人報(bào)道。了解學(xué)習(xí)下
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機(jī)源程序如下:
  1. /*******************************************************************************************/
  2. //   單片機(jī)原理與應(yīng)用課程設(shè)計(jì)
  3. //   題目:溫度檢測(cè)系統(tǒng)設(shè)計(jì)
  4. //   要點(diǎn):1、用溫度傳感器18B20測(cè)環(huán)境溫度,用LCD1602顯示測(cè)量結(jié)果
  5. //         2、用PROTEUS仿真
  6. //         3、焊接電路板并調(diào)試運(yùn)行
  7. //   出品:濰坊學(xué)院信息與控制工程學(xué)院
  8. //   編程:戴曰章
  9. //   日期:2014年12月
  10. //   注意:該程序僅供參考。改程序用于STC12C5A60S2單片機(jī)。它是1T的指令周期,與傳統(tǒng)的12T的MCU不同
  11. /*******************************************************************************************/

  12. #include<reg52.h>
  13. #include<intrins.h>
  14. #define uchar unsigned char
  15. #define uint unsigned int

  16. #define     LCDIO      P1  //1602數(shù)據(jù)口
  17. sbit DQ=P3^2;//ds18b20與單片機(jī)連接口

  18. sbit rs=P2^0;  //1602數(shù)據(jù)命令選擇引腳
  19. sbit rd=P2^1;        //讀寫(xiě)選擇
  20. sbit lcden=P2^2;        //1602選通引腳




  21. void DelayXus(uchar n);
  22. void DS18B20_Reset();
  23. void DS18B20_WriteByte(uchar dat);
  24. uchar DS18B20_ReadByte();


  25. float f_temp; //浮點(diǎn)型溫度值   
  26. uint tvalue;//溫度值
  27. uchar tflag;

  28. uchar code table[]=        {"The temperature "};    //每行顯示16個(gè)字符
  29. uchar code table1[]=        {"is:  000.0C wfu"};

  30. uchar data disdata[5];


  31. void delay(uint z)        //短延時(shí)
  32. {
  33.         uint x,y;
  34.         for(x=z;x>0;x--)
  35.                 for(y=110;y>0;y--);
  36. }

  37. void write_com(uchar com)        //1602寫(xiě)命令子程序
  38. {
  39.         rs=0;                //RS是數(shù)據(jù)命令選擇短,高電平寫(xiě)數(shù)據(jù),低電平寫(xiě)命令
  40.         rd=0;                //RD是讀寫(xiě)選擇短,高電平讀,低電平寫(xiě)
  41.         lcden=0;        //1602選通端,高電平選通,低電平禁止
  42.         LCDIO =com;
  43.         delay(5);
  44.         lcden=1;
  45.         delay(5);
  46.         lcden=0;        
  47. }

  48. void write_date(uchar date)        //1602寫(xiě)數(shù)據(jù)子程序
  49. {
  50.         rs=1;        //RS是數(shù)據(jù)命令選擇短,高電平寫(xiě)數(shù)據(jù),低電平寫(xiě)命令
  51.         rd=0;        //RD是讀寫(xiě)選擇短,高電平讀,低電平寫(xiě)
  52.         lcden=0;        //1602選通端,高電平選通,低電平禁止
  53.         LCDIO =date;
  54.         delay(5);
  55.         lcden=1;
  56.         delay(5);
  57.         lcden=0;        
  58. }



  59. void init()                                //1602初始化程序
  60. {
  61.         uchar num;
  62.         lcden=0;

  63.         write_com(0x38);        //0011 1000B,功能模式設(shè)置,設(shè)置為8為數(shù)據(jù)口,兩行顯示,5*7點(diǎn)陣
  64.         write_com(0x0c);        //0000 1011B,顯示開(kāi)及光標(biāo)設(shè)置,關(guān)顯示,顯示光標(biāo),光標(biāo)閃爍
  65.         write_com(0x06);        //0000 0110B,顯示光標(biāo)移動(dòng)設(shè)置,讀或?qū)懸粋(gè)字符,地址指針減一且光標(biāo)減一,寫(xiě)一個(gè)字符屏幕顯示不移動(dòng)
  66.         write_com(0x01);        //0000 0001B,顯示清屏,數(shù)據(jù)指針和所有顯示清屏
  67.         write_com(0x80);        //1000 000B,關(guān)閉顯示
  68.         delay(5);
  69.         write_com(0x80);        //1000 000B,設(shè)置為2行顯示,寫(xiě)入第一行字符的地址,第一行地址是00-2F
  70.         for(num=0;num<16;num++)
  71.                 {
  72.                         write_date(table[num]);        //寫(xiě)入第一行數(shù)據(jù)
  73.                         delay(5);
  74.                 }
  75.         write_com(0x80+0x40);        //1100 0000B,設(shè)置為2行顯示,寫(xiě)入第二行字符的地址,第而行地址是40-67
  76.         for(num=0;num<16;num++)        //寫(xiě)入第二行數(shù)據(jù)
  77.                 {
  78.                         write_date(table1[num]);//寫(xiě)入第二行數(shù)據(jù)
  79.                         delay(5);
  80.                 }      
  81.         

  82. }










  83. /**************************************
  84. 延時(shí)X微秒(STC12C5A60S2@12M)
  85. 不同的工作環(huán)境,需要調(diào)整此函數(shù)
  86. 此延時(shí)函數(shù)是使用1T的指令周期進(jìn)行計(jì)算,與傳統(tǒng)的12T的MCU不同
  87. **************************************/
  88. void DelayXus(uchar n)
  89. {
  90.     while (n--)
  91.     {
  92.         _nop_();
  93.         _nop_();
  94.     }
  95. }

  96. /**************************************
  97. 復(fù)位DS18B20,并檢測(cè)設(shè)備是否存在
  98. **************************************/
  99. void DS18B20_Reset()
  100. {
  101.     CY = 1;
  102.     while (CY)
  103.     {
  104.         DQ = 0;                     //送出低電平復(fù)位信號(hào)
  105.         DelayXus(240);              //延時(shí)至少480us
  106.         DelayXus(240);
  107.         DQ = 1;                     //釋放數(shù)據(jù)線
  108.         DelayXus(60);               //等待60us
  109.         CY = DQ;                    //檢測(cè)存在脈沖
  110.         DelayXus(240);              //等待設(shè)備釋放數(shù)據(jù)線
  111.         DelayXus(180);
  112.     }
  113. }

  114. /**************************************
  115. 從DS18B20讀1字節(jié)數(shù)據(jù)
  116. **************************************/
  117. uchar DS18B20_ReadByte()
  118. {
  119.     uchar i;
  120.     uchar dat = 0;

  121.     for (i=0; i<8; i++)             //8位計(jì)數(shù)器
  122.     {
  123.         dat >>= 1;
  124.         DQ = 0;                     //開(kāi)始時(shí)間片
  125.         DelayXus(1);                //延時(shí)等待
  126.         DQ = 1;                     //準(zhǔn)備接收
  127.         DelayXus(1);                //接收延時(shí)
  128.         if (DQ) dat |= 0x80;        //讀取數(shù)據(jù)
  129.         DelayXus(60);               //等待時(shí)間片結(jié)束
  130.     }

  131.     return dat;
  132. }

  133. /**************************************
  134. 向DS18B20寫(xiě)1字節(jié)數(shù)據(jù)
  135. **************************************/
  136. void DS18B20_WriteByte(uchar dat)
  137. {
  138.     char i;

  139.     for (i=0; i<8; i++)             //8位計(jì)數(shù)器
  140.     {
  141.         DQ = 0;                     //開(kāi)始時(shí)間片
  142.         DelayXus(1);                //延時(shí)等待
  143.         dat >>= 1;                  //送出數(shù)據(jù)
  144.         DQ = CY;
  145.         DelayXus(60);               //等待時(shí)間片結(jié)束
  146.         DQ = 1;                     //恢復(fù)數(shù)據(jù)線
  147.         DelayXus(1);                //恢復(fù)延時(shí)
  148.     }
  149. }







  150. unsigned int ReadTemp(void)
  151. {
  152. unsigned char a=0;
  153. unsigned char b=0;


  154.     DS18B20_Reset();                //設(shè)備復(fù)位
  155.     DS18B20_WriteByte(0xCC);        //跳過(guò)ROM命令
  156.     DS18B20_WriteByte(0x44);        //開(kāi)始轉(zhuǎn)換命令
  157.     while (!DQ);                    //等待轉(zhuǎn)換完成

  158.     DS18B20_Reset();                //設(shè)備復(fù)位
  159.     DS18B20_WriteByte(0xCC);        //跳過(guò)ROM命令
  160.     DS18B20_WriteByte(0xBE);        //讀暫存存儲(chǔ)器命令
  161.     a= DS18B20_ReadByte();       //讀溫度低字節(jié)
  162.     b= DS18B20_ReadByte();       //讀溫度高字節(jié)


  163.   tvalue=b;
  164.   tvalue<<=8;
  165.   tvalue=tvalue|a;
  166.     if(tvalue<0x0fff)
  167.    tflag=0;
  168.     else
  169.    {tvalue=~tvalue+1;
  170.          tflag=1;
  171.    }
  172. //tvalue=tvalue*(0.425);//溫度值擴(kuò)大10倍,精確到1位小數(shù)
  173. f_temp=tvalue*(0.0625);    // 溫度在寄存器中為12位,分辨率為0.0625
  174. tvalue=f_temp*10+0.5;    //乘以10表示小數(shù)點(diǎn)后面只取1位,加0.5時(shí)四舍五入
  175. //f_temp=f_temp+0.05;
  176. //f_temp=tvalue*(0.0625);
  177. //tvalue=f_temp*100+(tvalue>0?0.5:-0.5);


  178.         return(tvalue);
  179.   }


  180. void ds1820disp()//溫度值顯示
  181.         { uchar flagdat;
  182.           disdata[0]=tvalue/1000+0x30;//百位數(shù)
  183.      disdata[1]=tvalue%1000/100+0x30;//十位數(shù)
  184.      disdata[2]=tvalue%100/10+0x30;//個(gè)位數(shù)
  185.      disdata[3]=tvalue%10+0x30;//小數(shù)位
  186.    
  187.      if(tflag==0)
  188.             flagdat=0x20;//正溫度不顯示符號(hào)
  189.      else
  190.        flagdat=0x2d;//負(fù)溫度顯示負(fù)號(hào):-

  191.      if(disdata[0]==0x30)
  192.            {disdata[0]=0x20;//如果百位為0,不顯示
  193.                  if(disdata[1]==0x30)
  194.                   {disdata[1]=0x20;//如果百位為0,十位為0也不顯示
  195.                   }
  196.                 }


  197.            write_com(0x80+0x44);            //1100 0000B,設(shè)置為2行顯示,寫(xiě)入第二行字符的地址,第而行地址是40-67
  198.            write_date(flagdat);//顯示符號(hào)位
  199.            write_com(0x80+0x45);
  200.            write_date(disdata[0]);//顯示百位
  201.            write_com(0x80+0x46);
  202.            write_date(disdata[1]);//顯示十位         
  203.            write_com(0x80+0x47);
  204.            write_date(disdata[2]);//顯示個(gè)位         
  205.            write_com(0x80+0x48);
  206.            write_date(0x2e);//顯示小數(shù)點(diǎn)         
  207.            write_com(0x80+0x49);
  208.            write_date(disdata[3]);//顯示小數(shù)位
  209.            write_com(0x80+0x4a);
  210.            write_date('C');
  211.    }



  212. void main()
  213. {        
  214.         init();

  215.         ReadTemp();//讀取溫度
  216.         ds1820disp();//顯示
  217.         while(1)
  218.         {

  219.          ReadTemp();//讀取溫度
  220.          ds1820disp();//顯示

  221.         }

  222. }
復(fù)制代碼

所有資料51hei提供下載:
http://www.torrancerestoration.com/bbs/dpj-142615-1.html







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