日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 技術(shù)學(xué)院 > 熱搜器件
[導(dǎo)讀]1、頭文件 [cpp] view plaincopy #ifndef _DS18B20_H_ #define _DS18B20_H_ #define uchar unsigned char #define uint unsigned int void DS18B20_Delayus(uint us);

1、頭文件

[cpp] view plaincopy
 
  1. #ifndef _DS18B20_H_  
  2. #define _DS18B20_H_  
  3.   
  4. #define uchar unsigned char   
  5. #define uint  unsigned int  
  6.   
  7.   
  8. void DS18B20_Delayus(uint us);  
  9. void DS18B20_reset();  
  10. void DS18B20_write(uchar dat);  
  11. uchar DS18B20_data();  
  12. uint read_temperature();  
  13.   
  14. #endif  

 

2、C文件

[c-sharp] view plaincopy
 
  1. #include<reg52.h>  
  2. #include<intrins.h>  
  3. #include "DS18B20.h"  
  4.   
  5. uint TT;  //1820溫度變量  
  6. sbit DQ = P2^1;  
  7.   
  8. uchar table_temp[5];  
  9. uchar temp_comp;  
  10. /*******************************************************************/  
  11. /*                                                                 */  
  12. /*us級延時函數(shù)                                                     */  
  13. /*                                                                 */  
  14. /*******************************************************************/  
  15. void DS18B20_Delayus(uint us)  
  16. {  
  17.     while(--us);  
  18. }  
  19. void DS18B20_reset()  
  20. {  
  21.     uchar x = 0;  
  22.     DQ = 1;  
  23.     DS18B20_Delayus(16);  //稍做延時  
  24.     DQ = 0; //將DQ拉低  
  25.     DS18B20_Delayus(160);//延時400us~960us  
  26.     DQ = 1;    //拉高總線  
  27.     DS18B20_Delayus(28);//延時15us~60us  
  28.     x = DQ; //如果=0則初始化成功 =1則初始化失敗  
  29.     DS18B20_Delayus(40);//延時60us~240us  
  30. }  
  31. /*******************************************************************/  
  32. /*                                                                 */  
  33. /* 寫一個字節(jié)                                                      */  
  34. /*                                                                 */  
  35. /*******************************************************************/  
  36. void DS18B20_write(uchar dat)  
  37. {  
  38.      uchar i;  
  39.      for(i = 8; i > 0; i--)  
  40.      {  
  41.          DQ = 0;  
  42.          DQ = dat & 0x01;  
  43.          DS18B20_Delayus(10);  
  44.          DQ = 1;  
  45.          dat >>= 1;  
  46.      }  
  47. }  
  48. /*******************************************************************/  
  49. /*                                                                 */  
  50. /* 讀一個字節(jié)                                                      */  
  51. /*                                                                 */  
  52. /*******************************************************************/  
  53. uchar DS18B20_data()  
  54. {  
  55.     uchar i,dat;  
  56.     for (i = 8; i > 0; i--)  
  57.     {  
  58.       DQ = 0; // 給脈沖信號  
  59.       dat >>= 1;  
  60.       DQ = 1; // 給脈沖信號  
  61.       if(DQ)  
  62.         dat |= 0x80;  
  63.       DS18B20_Delayus(8);  
  64.     }  
  65.     return dat;  
  66. }  
  67. /*******************************************************************/  
  68. /*                                                                 */  
  69. /* 讀取溫度                                                        */  
  70. /*                                                                 */  
  71. /*******************************************************************/  
  72. uint read_temperature()  
  73. {  
  74.     uchar a,b;  
  75.     uint t = 0;  
  76.     float tt = 0;  
  77.   
  78.     DS18B20_reset();//DS18B20復(fù)位  
  79.     DS18B20_write(0xcc); //跳過讀序號列號的操作  
  80.     DS18B20_write(0x44); //啟動溫度轉(zhuǎn)換     
  81.   
  82.     DS18B20_reset();//DS18B20復(fù)位  
  83.     DS18B20_write(0xcc); //跳過讀序號列號的操作  
  84.     DS18B20_write(0xbe); //讀取溫度寄存器  
  85.     a = DS18B20_data(); //讀低8位  
  86.     b = DS18B20_data();  //讀高8位  
  87.     t=b;  
  88.     t<<=8;  
  89.     t=t|a;  
  90.     tt=t*0.0625;  
  91.     t= tt*10+0.5; //放大10倍輸出并四舍五入  
  92.     return(t);  
  93. }  
  94. void main()  
  95. {  
  96.       uchar i_1,tab1,tab2,tab3;  
  97.       LCD_init();  
  98.       DS18B20_reset();  
  99.       LCD_write_com(0x80);  
  100.       for(i_1 = 0; i_1 < 5; i_1++)  
  101.       LCD_write_data(table[i_1]);  
  102.   
  103.       while(1)  
  104.       {  
  105.             TT = read_temperature(); //讀溫度  
  106.   
  107.             tab1 = TT / 100;   
  108.             table_temp[0] = tab1 + 0x30;  // 十位  
  109.             tab2= TT / 10 - tab1*10;   
  110.             table_temp[1] = tab2 + 0x30; //個位  
  111.             table_temp[2] = '.';  
  112.             tab3 = TT - tab1*100 - tab2*10;   
  113.             table_temp[3] = tab3 + 0x30;    //小數(shù)點(diǎn)一位  
  114.             table_temp[4] = 'C';        //顯示溫度符號℃  
  115.             LCD_write_com(0x80+0x06);  
  116.              for(i_1 = 0; i_1 < 5; i_1++)  
  117.               LCD_write_data(table_temp[i_1]);        
  118.       }  
  119. }  

 

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉