• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

用单片机LM1602和DS18B20显示温度只有-25~+2

992b81030c187f9c 2020-06-02 浏览量:436
  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]temperaturetemperatureflag;
  10. uint wei_baiwei_shiwei_gexiaoshufuhaowendu;

  11. void delay(int a)
  12. {
  13.   int ij;
  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 itemp=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=0tempH=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. }
复制代码
0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
最佳答案
  • 这与1602的关系不大,主要是你对18B20的读取与温度转换代码的问题,你不要读取兵器就转成字符,先转成数字温度,看一下对不对,可能是转字符的问题
    • 发布于 2020-06-24
    • 举报
    • 评论 0
    • 0
    • 0

其他答案 数量:0

相关问题

问题达人换一批

用单片机LM1602和DS18B20显示温度只有-25~+2