找回密码
 立即注册

QQ登录

只需一步,快速开始

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

用单片机LM1602和DS18B20显示温度只有-25~+25,所以哪里有问题啊?

[复制链接]
跳转到指定楼层
楼主
ID:744425 发表于 2020-6-1 21:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
求助求助!!!!!

我的问题又更新了。为什么proteus仿真的时候,只有-25~+25的数值显示是正确的啊?怎么改呢?
  1. #include <REG51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. sbit RS=P2^0;
  5. sbit RW=P2^1;
  6. sbit EN=P2^2;
  7. sbit DQ=P3^4;

  8. uchar code table1[]="Temperature is:";
  9. uint table2[6],temperature,temperatureflag;
  10. uint wei_bai,wei_shi,wei_ge,xiaoshu,fuhao,wendu;

  11. void delay(int a)
  12. {
  13.   int i,j;
  14.   for(i=a;i>0;i--)
  15.     for(j=0;j<110;j++);
  16. }

  17. void delayus(int a)
  18. {
  19.   while(a--);
  20. }

  21. void write_com(uchar com)
  22. {
  23.   P0=com;
  24.   RS=0;RW=0;EN=1;
  25.   delay(10);
  26.   EN=0;
  27. }

  28. void write_dat(uchar dat)
  29. {
  30.   P0=dat;
  31.   RS=1;RW=0;EN=1;
  32.   delay(10);
  33.   EN=0;
  34. }

  35. void init_LM1602()
  36. {
  37.   write_com(0x01);
  38.   write_com(0x38);
  39.   write_com(0x0c);
  40.   write_com(0x06);
  41. }

  42. uchar read_onebyte()
  43. {
  44.   uchar i,temp=0;
  45.   for(i=0;i<8;i++)
  46.   {
  47.     DQ=0;
  48.          temp>>=1;
  49.          DQ=1;
  50.          if(DQ) temp|=0x80;
  51.          delayus(4);
  52.   }
  53.   return temp;
  54. }

  55. void write_onebyte(uchar dat)
  56. {
  57.   uchar i;
  58.   for(i=0;i<8;i++)
  59.   {
  60.     DQ=0;
  61.          DQ=dat&0x01;
  62.          delayus(10);
  63.          DQ=1;
  64.          dat>>=1;
  65.   }
  66. }

  67. void init_DS18B20()
  68. {
  69.   uchar x=0;
  70.   DQ=1;
  71.   delayus(8);
  72.   DQ=0;
  73.   delayus(80);
  74.   DQ=1;
  75.   delay(4);
  76.   x=DQ;
  77.   delayus(20);
  78. }

  79. uchar read_Temparature()
  80. {
  81.   uchar tempL=0,tempH=0;
  82.   init_DS18B20();
  83.   write_onebyte(0xcc);
  84.   write_onebyte(0x44);
  85.   delayus(100);
  86.   init_DS18B20();
  87.   write_onebyte(0xcc);
  88.   write_onebyte(0xbe);
  89.   tempL=read_onebyte();
  90.   tempH=read_onebyte();

  91. /*  if(tempH&0xF0)     //显示负数
  92.   {
  93.     tempL=~tempL;
  94.     tempH=~tempH;
  95.     temperature=(tempH*256+tempL)/16*(-1)-1;
  96.          temperatureflag=1;
  97.   }
  98.   else              //显示正数
  99.   {
  100.     temperature=(tempH*256+tempL)*0.0625;
  101.          temperatureflag=0;
  102.   }*/

  103.   temperature=tempH;
  104.   temperature=temperature<<8;
  105.   temperature=temperature|tempL;
  106.   if(temperature>0x0fff)
  107.   {
  108.     temperature=~temperature+2;
  109.         temperatureflag=1;
  110.   }
  111.   else temperatureflag=0;

  112.   temperature=temperature*0.0625*10;
  113.   return temperature;
  114. }

  115. void display()
  116. {
  117.   wendu=read_Temparature();
  118.   if(temperatureflag==1) fuhao=0x2d;
  119.   else fuhao=0x2b;

  120.   wei_bai=wendu/1000+0x30;
  121.   wei_shi=wendu%1000/100+0x30;
  122.   wei_ge=wendu%100/10+0x30;
  123.   xiaoshu=wendu%10+0x30;

  124.   write_com(0xc3);
  125.   write_dat(fuhao);
  126.   write_dat(wei_bai);
  127.   write_dat(wei_shi);
  128.   write_dat(wei_ge);
  129.   write_dat(0x2e);
  130.   write_dat(xiaoshu);
  131.   write_dat(0xdf);
  132.   write_dat(0x43);
  133. }

  134. void main()
  135. {
  136.   uchar i;
  137.   init_LM1602();
  138.   while(1)
  139.   {
  140.     write_com(0x80);
  141.     for(i=0;i<15;i++)
  142.     {
  143.       write_dat(table1[i]);
  144.            delay(5);
  145.     }
  146.     display();
  147.   }
  148. }
复制代码

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

使用道具 举报

沙发
ID:744425 发表于 2020-6-1 23:47 | 只看该作者
就是,超出-25~+25范围时,温度显示就不正常了
回复

使用道具 举报

板凳
ID:213173 发表于 2020-6-2 06:11 | 只看该作者
uchar read_Temparature() 函数数据类型错误,应为uint read_Temparature() 。
回复

使用道具 举报

地板
ID:584814 发表于 2020-6-2 07:46 | 只看该作者
可以实物显示的未必都能过仿真,所以如果你永远仿真的话可以问自己的老师
回复

使用道具 举报

5#
ID:648281 发表于 2020-6-2 08:30 | 只看该作者
把温度变量 unsigned char 改成 unsigned int
回复

使用道具 举报

6#
ID:744425 发表于 2020-6-2 11:33 | 只看该作者
man1234567 发表于 2020-6-2 07:46
可以实物显示的未必都能过仿真,所以如果你永远仿真的话可以问自己的老师

哎,按道理是要去做实物的,奈何回不了学校就只能仿真了
回复

使用道具 举报

7#
ID:744425 发表于 2020-6-2 11:35 | 只看该作者
wulin 发表于 2020-6-2 06:11
uchar read_Temparature() 函数数据类型错误,应为uint read_Temparature() 。

啊,真的,解决了!谢谢!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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