找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机+DS18B20读取温度失败

[复制链接]
跳转到指定楼层
楼主
ID:703577 发表于 2020-3-14 15:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
是这样的,在DS18B20上琢磨了几天,但还是没成功
具体就是读取温度失败,总为一个数值,没有变化,通过keil仿真,时序没错,步骤没错,但到proteus上仿真就是只有一个数值,怎么改ds18b20的数值都不变,因此求助各位帮忙看下是哪里出了问题。keil和proteus仿真均是使用89C52单片机以及12MHZ晶振。
具体代码如下:我也会附上文件,方便各位大佬检查

单片机源程序如下:
  1. #include<reg52.h>

  2. #ifndef uchar
  3. #define uchar unsigned char
  4. #endif

  5. #ifndef uint
  6. #define uint unsigned int
  7. #endif

  8. sbit DSPORT =P2^0;
  9. #define LCD1602_DATAPINS P0
  10. sbit LCD1602_E=P1^1;         
  11. sbit LCD1602_RS=P1^0;

  12. uint temp = 0;                                 
  13. uchar yanwu = 0;        
  14. uchar yushe_temp=50;        
  15. uchar yushe_yanwu=50;        

  16. void check_temp();
  17. void Delay1ms();
  18. void Delay_DS18B20(int num);
  19. void Ds18b20Init();
  20. void Ds18b20WriteByte(uchar dat);
  21. uchar Ds18b20ReadByte();
  22. uint Ds18b20ReadTemp();        

  23. void LcdWriteCom(uchar com);
  24. void LcdWriteData(uchar dat)        ;        
  25. void LcdInit();                        
  26. void Display_LCD1602(yushe_temp,yushe_yanwu,temp,yanwu);

  27. uchar code Init1[]="SET T:00   E:000";
  28. uchar code Init2[]="NOW T:00.0 E:000";


  29. void Delay1ms(uint c)  
  30. {
  31.     uchar a,b;
  32.         for (; c>0; c--)
  33.         {
  34.                  for (b=199;b>0;b--)
  35.                  {
  36.                           for(a=1;a>0;a--);
  37.                  }      
  38.         }           
  39. }

  40. void LcdWriteCom(uchar com)        
  41. {
  42.         LCD1602_E = 0;     
  43.         LCD1602_RS = 0;         
  44.         
  45.         LCD1602_DATAPINS = com;     
  46.         Delay1ms(1);               

  47.         LCD1602_E = 1;               
  48.         Delay1ms(5);        
  49.         LCD1602_E = 0;
  50. }                  

  51. void LcdWriteData(uchar dat)               
  52. {
  53.         LCD1602_E = 0;        
  54.         LCD1602_RS = 1;        

  55.         LCD1602_DATAPINS = dat;
  56.         Delay1ms(1);

  57.         LCD1602_E = 1;   
  58.         Delay1ms(5);   
  59.         LCD1602_E = 0;
  60. }
  61.            
  62. void LcdInit()                                                
  63. {
  64.         uchar i = 0;
  65.          LcdWriteCom(0x38);  
  66.         LcdWriteCom(0x0c);  
  67.         LcdWriteCom(0x06);  
  68.         LcdWriteCom(0x01);  
  69.         
  70.         LcdWriteCom(0x80);
  71.           for(i=0;i<16;i++)
  72.   {
  73.                 LcdWriteData(Init1[i]);
  74.   }
  75.   LcdWriteCom(0x80+0x40);
  76.   for(i=0;i<16;i++)
  77.   {
  78.                 LcdWriteData(Init2[i]);
  79.   }
  80. }

  81. void Display_LCD1602(yushe_temp,yushe_yanwu,temp,yanwu)
  82. {
  83.         LcdWriteCom(0x80+6);
  84.         LcdWriteData(0x30+yushe_temp/10);
  85.         LcdWriteData(0x30+yushe_temp%10);

  86.         LcdWriteCom(0x80+13);
  87.         LcdWriteData(0x30+yushe_yanwu/100);
  88.         LcdWriteData(0x30+yushe_yanwu%100/10);
  89.         LcdWriteData(0x30+yushe_yanwu%10);

  90.         LcdWriteCom(0x80+0x40+6);
  91.         LcdWriteData(0x30+temp/100);
  92.         LcdWriteData(0x30+temp%100/10);
  93.         LcdWriteData('.');
  94.         LcdWriteData(0x30+temp%10);

  95.         LcdWriteCom(0x80+0x40+13);
  96.         LcdWriteData(0x30+yanwu/100);
  97.         LcdWriteData(0x30+yanwu%100/10);
  98.         LcdWriteData(0x30+yanwu%10);
  99. }

  100. void Delay_DS18B20(int num)
  101. {
  102. while(num--);
  103. }        

  104. void check_temp()
  105. {
  106.   temp = Ds18b20ReadTemp();
  107.   if(temp<0) temp = 0;
  108.   if(temp>999) temp = 999;
  109. }

  110. void Ds18b20Init()
  111. {
  112.   uchar x = 0;
  113.   DSPORT = 1;
  114.   Delay_DS18B20(8);
  115.   DSPORT = 0;
  116.   Delay_DS18B20(80);
  117.   DSPORT = 1;
  118.   Delay_DS18B20(34);
  119.   x = DSPORT;
  120.   Delay_DS18B20(20);
  121. }

  122. void Ds18b20WriteByte(uchar dat)
  123. {
  124.   uchar i=0;
  125.   for(i=8; i>0; i--)
  126.   {
  127.     DSPORT = 0;
  128.     DSPORT = dat&0X01;
  129.     Delay_DS18B20(5);
  130.     DSPORT = 1;
  131.     dat>>=1;
  132.   }
  133. }

  134. uchar Ds18b20ReadByte()
  135. {
  136.   uchar i=0;
  137.   uchar byte=0;
  138.   for (i=8; i>0; i--)
  139.         {
  140.   DSPORT = 0;
  141.   byte>>=1;
  142.   DSPORT = 1;
  143.   if(DSPORT)
  144.     byte|=0X80;
  145.   Delay_DS18B20(4);
  146.         }
  147.   return (byte);
  148. }

  149. uint Ds18b20ReadTemp()
  150. {
  151.   uchar tml=0;
  152.   uchar tmh=0;
  153.   uint t=0;
  154.   float tt=0;
  155.   Ds18b20Init();
  156.   Delay1ms(1);
  157.   Ds18b20WriteByte(0xcc);                                 
  158.   Ds18b20WriteByte(0x44);                 
  159.   Ds18b20Init();
  160.   Delay1ms(1);
  161.   Ds18b20WriteByte(0xcc);                        
  162.   Ds18b20WriteByte(0x44);                        
  163.   tml = Ds18b20ReadByte();               
  164.   tmh = Ds18b20ReadByte();        
  165.   t = tmh;
  166.   t <<= 8;
  167.   t |= tml;
  168.   tt = t*0.0625;
  169.   t = tt*10+0.5;
  170.   return t;
  171. }

  172. void main()
  173. {
  174.    check_temp();
  175.    LcdInit();
  176.   while(1)
  177.      {
  178.        check_temp();
  179.        Display_LCD1602(yushe_temp,yushe_yanwu,temp,yanwu);
  180.      }
  181. }
复制代码
由于有些是移植别人的代码,yushe_temp,yushe_yanwu,yanwu这几个变量是我毕业设计的其他要求,可以忽略,重点就是这个temp显示不了当前温度值。想破头都想不到是怎么回事,求助各位,帮忙分析下,指出错误点。


实验.rar

34.92 KB, 下载次数: 15

附件为DS18B20+LCD1602显示实验

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

使用道具 举报

沙发
ID:703577 发表于 2020-3-14 16:10 | 只看该作者
在这里说下,keil和proteus中都是使用89C52单片机及晶振频率为12MHZ,心态被搞崩了,有些东西遗留了,不好意思。
回复

使用道具 举报

板凳
ID:213173 发表于 2020-3-14 21:02 | 只看该作者
VENMON 发表于 2020-3-14 16:10
在这里说下,keil和proteus中都是使用89C52单片机及晶振频率为12MHZ,心态被搞崩了,有些东西遗留了,不好 ...

uint Ds18b20ReadTemp()
{
  uchar tml=0;
  uchar tmh=0;
  uint t=0;
  float tt=0;
  Ds18b20Init();
  Delay1ms(1);
  Ds18b20WriteByte(0xcc);                                 
  Ds18b20WriteByte(0x44);                 
  Ds18b20Init();
  Delay1ms(1);
  Ds18b20WriteByte(0xcc);                        
// Ds18b20WriteByte(0x44);// 错误                    
  Ds18b20WriteByte(0xbe);        //发送读取温度命令
tml = Ds18b20ReadByte();               
  tmh = Ds18b20ReadByte();        
  t = tmh;
  t <<= 8;
  t |= tml;
  tt = t*0.0625;
  t = tt*10+0.5;
  return t;
}
回复

使用道具 举报

地板
ID:703577 发表于 2020-3-14 22:17 | 只看该作者
wulin 发表于 2020-3-14 21:02
uint Ds18b20ReadTemp()
{
  uchar tml=0;

我查看我的原程序的确有这一处错误,粗心大意了,想着把那部分独立出来看一下问题所在,对了时序,步骤却粗心地认为与正确步骤一致。还有个提问:初始化DS18B20时,是否存在应答会影响他的初始化,就是我初始化DS18B20没有返回1或0,行吗?
回复

使用道具 举报

5#
ID:213173 发表于 2020-3-15 07:45 | 只看该作者
VENMON 发表于 2020-3-14 22:17
我查看我的原程序的确有这一处错误,粗心大意了,想着把那部分独立出来看一下问题所在,对了时序,步骤却 ...

初始化DS18B20在单器件通讯时判断返回应答信号不是必须的,初始化过程类似于唤醒。
回复

使用道具 举报

6#
ID:703577 发表于 2020-3-16 09:52 | 只看该作者
wulin 发表于 2020-3-15 07:45
初始化DS18B20在单器件通讯时判断返回应答信号不是必须的,初始化过程类似于唤醒。

感谢解答
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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