当前位置:首页 > 技术学院 > 热搜器件
[导读][cpp] view plaincopy /*温度传感器18B20--串口显示温度*/ #include<reg52.h> #include <intrins.h> typedef unsigned char uint8; typedef unsigned int uint16; typedef

  1. /*温度传感器18B20--串口显示温度*/  
  2. #include<reg52.h>  
  3. #include <intrins.h>   
  4.   
  5. typedef unsigned char uint8;  
  6. typedef unsigned int  uint16;  
  7. typedef          char int8;  
  8. typedef          int int16;  
  9. sbit DQ=P3^2;    //温度输入口  
  10.   
  11. void nops()  
  12. {  
  13.     _nop_();  
  14.     _nop_();  
  15.     _nop_();  
  16.     _nop_();  
  17. }  
  18.   
  19. void delay(uint16 n)  
  20. {  
  21.     while(n--);  
  22. }  
  23.   
  24. void delay_ms(uint16 n)  
  25. {  
  26.     uint8 m=120;  
  27.   
  28.     while (n--)  
  29.         while (m--);  
  30. }  
  31.   
  32. void DS18b20_reset(void)  
  33. {  
  34.     bit flag=1;  
  35.         while (flag)  
  36.         {  
  37.             DQ = 1;  
  38.             delay(1);   //7.5us  
  39.             DQ = 0;  
  40.             delay(50); //139.8us  
  41.             DQ = 1;   
  42.             delay(6);  // 21us  
  43.             flag = DQ;  
  44.         }  
  45.     delay(45);    //延时500us  
  46.     DQ=1;  
  47. }  
  48.   
  49.   
  50. /* 
  51.  * 18B20写1个字节函数 
  52.  * 向1-WIRE总线上写一个字节 
  53. */  
  54. void write_byte(uint8 val)  
  55. {  
  56.     uint8 i;  
  57.   
  58.     for (i=0; i<8; i++)  
  59.     {  
  60.         DQ = 1;  
  61.         _nop_();     //两次传送间隔大于1us  
  62.         DQ = 0;  
  63.         nops(); //4us  
  64.         DQ = val & 0x01;      //最低位移出  
  65.         delay(6);           //66us  (30US)  
  66.         val >>= 1;          //右移一位  
  67.     }  
  68.     DQ = 1;  
  69.     delay(1);    
  70. }  
  71.   
  72.   
  73. /* 
  74.  * 18B20读1个字节函数 
  75.  * 从1-WIRE总线上读取一个字节 
  76. */  
  77. uint8 read_byte(void)  
  78. {  
  79.     uint8 i, value=0;  
  80.   
  81.     for (i=0; i<8; i++)  
  82.     {  
  83.         value >>= 1;  
  84.         DQ=1;  
  85.         _nop_();  
  86.         DQ = 0;  
  87.         nops();   //4us  
  88.         DQ = 1;  
  89.         nops();   //4us   
  90.         if (DQ)  
  91.             value|=0x80;  
  92.         delay(6);           //66us  
  93.     }  
  94.     DQ=1;  
  95.     return value;  
  96. }  
  97.   
  98.   
  99. /* 
  100.  * 启动温度转换 
  101. */  
  102. void start_temp_sensor(void)  
  103. {  
  104.    DS18b20_reset();  
  105.    write_byte(0xCC); // 发Skip ROM命令  
  106.    write_byte(0x44);// 发转换命令  
  107. }  
  108.   
  109.   
  110. /* 
  111.  * 读出温度 
  112. */  
  113. int16 read_temp(void)  
  114. {  
  115.     uint8 temp_data[2]; // 读出温度暂放  
  116.     int16 temp;  
  117.   
  118.     DS18b20_reset();  // 复位  
  119.     write_byte(0xCC); // 发Skip ROM命令  
  120.     write_byte(0xBE); // 发读命令  
  121.     temp_data[0]=read_byte();  //温度低8位  
  122.     temp_data[1]=read_byte();  //温度高8位  
  123.   
  124.     temp = temp_data[1];  
  125.     temp <<= 8;  
  126.     temp |= temp_data[0];  
  127.     temp >>= 4;     //注意是移动四位  
  128.   
  129.     return temp;  
  130. }  
  131.   
  132. /** 
  133.  * UART初始化 
  134.  * 波特率:9600 
  135. */  
  136. void uart_init(void)  
  137. {  
  138.     TMOD = 0x21;        // 定时器1工作在方式2(自动重装)  
  139.     SCON = 0x50;        // 10位uart,允许串行接受  
  140.   
  141.     TH1 = 0xFD;  
  142.     TL1 = 0xFD;  
  143.   
  144.     TR1 = 1;  
  145. }  
  146.   
  147. /** 
  148.  * UART发送一字节 
  149. */  
  150. void UART_Send_Byte(uint8 dat)  
  151. {  
  152.     SBUF = dat;  
  153.     while (TI == 0);  
  154.     TI = 0;  
  155. }  
  156.   
  157. /** 
  158.  * 将数据转换成ASC码并通过UART发送出去 
  159. */  
  160. void UART_Send_Dat(uint8 dat)    //100度以下温度可用  
  161. {  
  162.     UART_Send_Byte(dat/10%10 + '0');  
  163.     UART_Send_Byte(dat%10 + '0');  
  164. }  
  165.   
  166.    
  167.   
  168. main()  
  169. {  
  170.     int16 ans;  
  171.   
  172.     uart_init();  
  173.     start_temp_sensor();  
  174.     while (1)  
  175.     {  
  176.         delay_ms (1000); // 延时1秒  
  177.   
  178.         ans=read_temp();  
  179.       
  180.         if (ans < 0)  
  181.         {  
  182.             UART_Send_Byte('-');  
  183.             ans = -ans;  
  184.         }  
  185.       
  186.         UART_Send_Dat(ans);  
  187.         UART_Send_Byte('r');  
  188.         UART_Send_Byte('n');  
  189.     }  
  190.   
  191. }  

 
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭