找回密码
 立即注册

QQ登录

只需一步,快速开始

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

请教,为什么MAX6675读取温度不能实时显示!

[复制链接]
跳转到指定楼层
楼主
ID:218368 发表于 2018-3-29 16:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用51通过MAX6675读取温度,并在1602上显示。
现在的程序只能显示开机读取的温度,温度变化后,不能显示在1602上,请大侠看看是程序什么地方出了问题。

单片机源码:
  1. #include <reg51.h>
  2. #include <Intrins.h>
  3. #include <LCD1602.H>
  4. #include <math.H>


  5. #define uchar unsigned char
  6. #define uint unsigned int


  7. sbit MAX6675_SO=P1^0;
  8. sbit MAX6675_SCK=P1^1;
  9. sbit MAX6675_CS=P1^2;   



  10. bit ack,flag=0,flag1=0;

  11. uint  tz;
  12. int t_zhi=0;


  13. char code keytab[]={0xEE,0xDE,0xBE,0x7E,0xED,0xDD,0xBD,0x7D,0xEB,0xDB,0xBB,0x7B,0xE7,0xD7,0xB7,0x77};
  14. unsigned char code str3[]={"              "};
  15. unsigned char data disdata[5];





  16. unsigned int ReadMAX6675()
  17. {
  18. unsigned char count;
  19. unsigned int Value;
  20. MAX6675_CS=0;   
  21. MAX6675_SCK=0;
  22. Value=0;
  23. _nop_() ;_nop_();_nop_();_nop_();
  24. for(count=0;count<16;count++)
  25. {
  26.   Value=Value<<1;
  27.   MAX6675_SCK=1;
  28.   _nop_() ;_nop_();_nop_();_nop_();
  29.   if(MAX6675_SO==1)
  30.      Value|=0x01;
  31.      MAX6675_SCK=0;
  32. }
  33. MAX6675_CS=1;
  34. return Value;
  35. }

  36. unsigned int GetCurrentTemp(unsigned int CurrentValue)
  37. {
  38.   unsigned int TempValue;
  39.   TempValue=CurrentValue;
  40.   if(TempValue&0x8000)
  41.   return 0;
  42.   if(TempValue&0x0004)
  43.   return 1;
  44. }

  45. void tempdisp()         //ζèÖμÏÔê¾
  46. { unsigned char i;
  47.   unsigned int TempValue;
  48.   unsigned int testD2;
  49.   int xiaoshu;
  50.   TempValue=ReadMAX6675();
  51.   testD2=GetCurrentTemp(TempValue);
  52.   TempValue=ReadMAX6675();
  53.   TempValue&=0x7ff8;
  54.   TempValue>>=3;
  55.   xiaoshu= (TempValue*1023.75/4095)*10-262;
  56.   t_zhi=xiaoshu/10;
  57.   disdata[0]=xiaoshu/10000+0x30;
  58.   xiaoshu=xiaoshu%10000;
  59.   disdata[1]=xiaoshu/1000+0x30;
  60.   xiaoshu=xiaoshu%1000;
  61.   disdata[2]=xiaoshu/100+0x30;
  62.   xiaoshu=xiaoshu%100;
  63.   disdata[3]=xiaoshu/10+0x30;
  64.   xiaoshu=xiaoshu%10;
  65.   disdata[4]=xiaoshu/1+0x30;
  66.   LCD_set_position(68);
  67.   for(i=0;i<4;i++)
  68.   LCD_write_data(disdata[i]);
  69.   LCD_write_data('.');
  70.   LCD_write_data(disdata[4]);
  71.   LCD_write_data(0xDF);
  72.   LCD_write_data(0x43);

  73. }






  74. void main()
  75. {


  76.   LCD_initial();
  77.   LCD_cls();
  78.   LCD_set_position(0);
  79.   //LCD_prints(str1);
  80.   LCD_set_position(3);

  81.   MAX6675_CS=1;
  82.   while(1)
  83.   {
  84.     ReadMAX6675();
  85.      GetCurrentTemp();
  86.            tempdisp();


  87. }
  88. }
复制代码


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

使用道具 举报

沙发
ID:485731 发表于 2019-4-21 15:45 | 只看该作者
楼主你的问题解决了么?
回复

使用道具 举报

板凳
ID:417092 发表于 2019-5-3 12:35 | 只看该作者
求同样的问题。。。
回复

使用道具 举报

地板
ID:417092 发表于 2019-5-3 12:41 | 只看该作者
求解,同样的问题。我在网上找了几个程序,都无法读出温度
回复

使用道具 举报

5#
ID:471427 发表于 2020-11-22 15:45 来自手机 | 只看该作者
一样的问题
回复

使用道具 举报

6#
ID:451718 发表于 2020-11-23 11:52 | 只看该作者
ReadMAX6675(); 这个函数要间隔一段时间运行一次。只有一次读数,大概率是间隔时间太短了造成的。  有两种解决方案:
1,把ReadMAX6675()放到定时器中运行,500ms运行一次。
2,在主程序while循环中加一个500ms的延迟函数。
回复

使用道具 举报

7#
ID:731755 发表于 2022-12-5 20:38 来自手机 | 只看该作者
robinsonlin 发表于 2020-11-23 11:52
ReadMAX6675(); 这个函数要间隔一段时间运行一次。只有一次读数,大概率是间隔时间太短了造成的。  有两种 ...

谢谢,你的方法好用
回复

使用道具 举报

8#
ID:1049152 发表于 2023-1-12 14:52 | 只看该作者
max6675的数据转换时间不能太近,间隔要在200ms左右,你可以加延时也可以用一个定时器,推荐使用定时器
回复

使用道具 举报

9#
ID:444215 发表于 2023-8-28 22:56 | 只看该作者
问题解决了吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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