找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2269|回复: 0
打印 上一主题 下一主题
收起左侧

单总线挂接4路ds18B20温度监测系统 仿真+程序

[复制链接]
跳转到指定楼层
楼主
单总线挂接4路ds18B20温度监测系统



4路ds18B20温度监测51单片机程序:
  1. /*******************************************************************
  2. **函数功能:ds18b20的驱动函数                                                                          **
  3. **创建人:xingyuegu                                                                                                  **
  4. **创建日期:7-12                                                                                          **
  5. **版本:1.0                                                                                                                  **
  6. **修改日期:10-30                                                                                            **
  7. **版本:2.0                                                                                                                  **
  8. *******************************************************************/
  9. #include <reg51.h>
  10. #include <intrins.h>
  11. #define uchar unsigned char
  12. #define uint  unsigned int
  13. void serial_set(void);
  14. void process(unsigned char,unsigned char);
  15. char volatile xiaoshu_temp[5],zhen_temp[4];                 //
  16. bit flag;
  17. bit dot_dis=1;
  18. sbit DQ=P1^0;
  19. //sbit DQ=P2^3;
  20. extern unsigned char temp[5];
  21. /*************************************************************
  22. **功能:延时600us                                                                                        **
  23. **参数:无                                                                                               **
  24. *************************************************************/
  25. void delay600us(void)
  26. {
  27.         uchar i;
  28.         for(i=0;i<255;i++);

  29. }
  30. /*************************************************************
  31. **功能:延时60us                                                                                        **
  32. **参数:无                                                                                               **
  33. *************************************************************/
  34. void delay60us(void)
  35. {
  36.         uchar i;
  37.         for(i=0;i<20;i++);
  38. }
  39. /*************************************************************
  40. **功能:延时18us                                                                                        **
  41. **参数:无                                                                                               **
  42. *************************************************************/
  43. /*void delay15us(void)
  44. {
  45.         uchar i;
  46.         for(i=0;i<2;i++);
  47. }*/
  48. /*************************************************************
  49. **功能:复位脉冲                                                                                        **
  50. **参数:bool                                                                                               **
  51. *************************************************************/
  52. bit resetpulse(void)
  53. {
  54.        
  55.         DQ=0;
  56.         delay600us();                         //延时500us
  57.         DQ=1;
  58.         delay60us();                        // 延时60us
  59.         return(DQ);                                 //读取P1.0的状态
  60. }
  61. /*************************************************************
  62. **功能:ds18b20的初始化                                                                                **
  63. **参数:无                                                                                               **
  64. *************************************************************/
  65. void ds18b20_init(void)
  66. {
  67.         while(1)
  68.         {
  69.                 if(!resetpulse())          //收到ds18b20的应答信号
  70.                 {       
  71.                         //printf("reset successful!");

  72.                         //delay240us();        //延时240us
  73.                         DQ=1;
  74.                         delay600us();        //延时240us
  75.                         break;               
  76.                 }
  77.                 else
  78.                         resetpulse();         //否则再发复位信号
  79.         }
  80. }

  81. /*************************************************************
  82. **功能:向ds18b20写命令                                                                                **
  83. **参数:无                                                                                               **
  84. *************************************************************/
  85. void ds18b20_writecommand(uchar command)
  86. {       

  87.         uchar   i;
  88.         for(i=0;i<8;i++)
  89.         {
  90.                 if((command & 0x01)==0)
  91.                 {
  92.                         DQ=0;                                        //写0
  93.                         delay60us();                        //延时60us
  94.                         _nop_();
  95.                         _nop_();
  96.                         _nop_();
  97.                         _nop_();
  98.                         _nop_();
  99.                         _nop_();
  100.                         DQ=1;                                  
  101.                         _nop_();
  102.                         _nop_();

  103.                 }
  104.        
  105.                 else                                                //写1
  106.                 {
  107.                          DQ=0;
  108.                         _nop_();
  109.                         _nop_();                                //延时2us
  110.                         DQ=1;
  111.                         delay60us();                        //延时60us
  112.                         _nop_();
  113.                         _nop_();
  114.                         _nop_();
  115.                         _nop_();
  116.                 }
  117.                 command=_cror_(command,1); // 右移1位
  118.          }
  119.   
  120.   
  121. }

  122. /*************************************************************
  123. **功能:读ds18b20数据                                                                                **
  124. **参数:返回读到的数据                                                                                **
  125. *************************************************************/
  126. uchar ds18b20_readdata(void)
  127. {
  128.         uchar readdata;
  129.         uchar i;
  130.         for(i=0;i<8;i++)
  131.         {
  132.                 DQ=0;
  133.                 _nop_();
  134.                 _nop_();
  135.                 _nop_();
  136.                 DQ=1;                  //释放总线
  137.                 //delay15us();        注意不需要
  138.                 if(DQ==0)           //如果读到的是0
  139.                 {
  140.                         readdata=readdata&0x7f;
  141.                         delay60us();
  142.                 }
  143.                 else                        //读到的是1
  144.                 {
  145.                         readdata=readdata|0x80;
  146.                         delay60us();
  147.                 }
  148.                 if(i<7)
  149.                
  150.                 readdata=_cror_(readdata,1);
  151.         }
  152.         return readdata;
  153. }

  154. /*************************************************************
  155. **功能:温度处理函数                                                                                        **
  156. **参数:无返回                                                                                                **
  157. *************************************************************/
  158. void temperature_process(uchar low,uchar high)
  159. {
  160.         uint temp1,temp2,temp3;
  161.         if(high&0x80)//判断正负
  162.         {
  163.         flag=1;

  164.         temp3=temp3|high;
  165.         temp3=temp3&0x00ff;
  166.         temp3=temp3<<8;
  167.         temp1=temp3;

  168.         temp1=temp1|low;
  169.        
  170.         temp1=(temp1^0xffff);
  171.         temp1=temp1+1;         //取反加1
  172.         low=temp1&0x000f;
  173.         high=temp1>>4;
  174.                 process(high,low);
  175.         }
  176.         else
  177.         {

  178.                 flag=0;                 //zhen
  179.                 temp1=high;
  180.                 temp2=low;
  181.                 temp1=temp1<<4;
  182.                 temp2=temp2>>4;
  183.                 temp3=temp1|temp2;
  184.                 high=temp3;
  185.                 low=low&0x0f;
  186.                 process(high,low);
  187.        
  188.         }
  189. }
  190. /*************************************************************
  191. **功能:数值处理函数                                                                                        **
  192. **参数:无返回                                                                                                **
  193. *************************************************************/
  194. void process(unsigned char high,unsigned char low)
  195. {
  196.          uint temp1;
  197.          uchar i;
  198.          temp1=low*625;
  199.          xiaoshu_temp[0]=temp1/1000+'0';
  200.          //xiaoshu_temp[1]=temp1/100%10+'0';
  201.          //xiaoshu_temp[2]=temp1%100/10+'0';
  202.          //xiaoshu_temp[3]=temp1%10+'0';
  203.          xiaoshu_temp[1]='\0';
  204.          dot_dis=1;
  205.                 //if(xiaoshu_temp[3]=='0')
  206.                 //{
  207.                 //        xiaoshu_temp[3]='\0';
  208.                 //        if(xiaoshu_temp[2]=='0')
  209.                 //                {
  210.                 //                        xiaoshu_temp[2]='\0';
  211.                 //                        if(xiaoshu_temp[1]=='0')
  212.                 //                        {
  213.                 //                                xiaoshu_temp[1]='\0';
  214.                                                 if(xiaoshu_temp[0]=='0')
  215.                                                 {
  216.                                                         xiaoshu_temp[0]='\0';
  217.                                                         dot_dis=0;
  218.                                                 }
  219.                                                 else
  220.                                                         dot_dis=1;
  221.                                                
  222.                                                        
  223.                         //                }
  224.                 //                }
  225.                                        
  226.         //        }
  227.        
  228.          
  229.          zhen_temp[0]=high/100+'0';
  230.          zhen_temp[1]=high%100/10+'0';
  231.          zhen_temp[2]=high%10+'0';
  232.          zhen_temp[3]='\0';
  233.          for(i=0;i<2;i++)
  234.                 if(zhen_temp[0]=='0')
  235.                         {
  236.                         zhen_temp[0]=zhen_temp[1];
  237.                         zhen_temp[1]=zhen_temp[2];
  238.                         zhen_temp[2]='\0';
  239.                         }
  240.                 else
  241.                         break;
  242. }
  243. /****************************************/
  244. //serial_set func
  245. void serial_set(void)
  246. {
  247.        
  248.         SCON=0x50;
  249.         TMOD=TMOD&0x0f;
  250.         TMOD=TMOD|0x20;
  251.         TH1=0xfd;
  252.         TL1=0xfd;

  253.         TR1=1;
  254.         TI=1;  //用PRINTF时,TI要置1
  255. }


  256. /*************************************************************
  257. **功能:序列号匹配子程序                                                                                **
  258. **参数:无返回                                                                                                **
  259. *************************************************************/
  260. bit match_rom(uchar *rom)
  261. {
  262.         uchar i;
  263.         ds18b20_init();
  264.         ds18b20_writecommand(0x55);
  265.         for(i=8;i>0;i--)
  266.         {
  267.                 ds18b20_writecommand(*(rom+i-1));
  268.         //        rom++;
  269.         }
  270.         return 1;
  271. }       
  272.                
复制代码


所有资料下载:
单总线挂接4路ds18B20温度监测系统.zip (271.41 KB, 下载次数: 23)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表