找回密码
 立即注册

QQ登录

只需一步,快速开始

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

52单片机接两个DS18B20,测出的温度显示相差很大,大概10度左右,大神可以给些建议...

[复制链接]
跳转到指定楼层
楼主
ID:455316 发表于 2019-5-25 19:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
100黑币
  1. #include <REGX52.H>
  2. #include<intrins.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int

  5. typedef unsigned int u16;          //对数据类型进行声明定义
  6. typedef unsigned char u8;

  7. sbit IRIN=P3^2;

  8. u8 IrValue[6];
  9. u8 Time;


  10. sbit wela = P0^7;
  11. sbit dula = P0^6;

  12. sbit EN=P2^7;//使能
  13. sbit RS=P2^6;//数据/命令选择端(H/L)
  14. sbit RW=P2^5;//读/写端(H/L)

  15. sbit DS1=P2^4;  //DS18B20one接入口
  16. sbit DS2=P2^3;  //DS18B20two接入口

  17. sbit Dcfcold=P2^2;
  18. sbit Dcfhot=P2^1;
  19. sbit Dcfmix=P2^0;


  20. bit f1=1;  //正负温度标志位
  21. bit f2=1;

  22. unsigned char  m=1,y=0,o=0;//寄存器
  23. unsigned int sdata1;                //存放整数温度
  24. unsigned char xiaoshu11;           //存放小数后第一位温度数值
  25. unsigned char xiaoshu12;           //存放小数后第二位温度数值
  26. unsigned int sdata2;               //存放整数温度
  27. unsigned char xiaoshu21;          //存放小数后第一位温度数值
  28. unsigned char xiaoshu22;          //存放小数后第二位温度数值

  29. unsigned char code huany[]=" Welcome to use "; //欢迎使用
  30. unsigned char code tt[]  =" ter thermometer"; //温度计
  31. unsigned char code duqu[]="   loading...   "; //读取
  32. unsigned char code tab1[]="      C       C  ";        //框架
  33. unsigned char code tab2[]="U:   C   L:   C  ";
  34. unsigned char code shuzu[]="0123456789+- .*";        //数字,,10加号,,,11减号,,,12空格,,,,13点,,,,14*

  35. uchar u,l,p,Key_value;
  36. //uchar  str[4];
  37. uchar  str[4];//uchartostr函数转换的字符串 同时可以把16进制转成10进制

  38. /*******************************************************************************
  39. * 函 数 名         : delay
  40. * 函数功能                   : 延时函数,i=1时,大约延时10us
  41. *******************************************************************************/
  42. void Irdelay(u16 i)
  43. {
  44.         while(i--);        
  45. }



  46. void Ir_work(void)//红外键值散转程序
  47. {
  48.        switch(IrValue[2])                     //判断第三个数码值
  49.                  {
  50.                          case 0x40:u++;break;
  51.                          case 0x19:u--;break;
  52.                          case 0x07:l++;break;
  53.                          case 0x09:l--;break;
  54.            //              case 0x0C:mix=1;break;
  55.            //              case 0x16:imix=0;break;
  56.                          default:break;
  57.                          }

  58.            //       irpro_ok=0;//处理完成标志

  59.   }


  60. /*******************************************************************************
  61. * 函数名         : IrInit()
  62. * 函数功能                   : 初始化红外线接收
  63. * 输入           : 无
  64. * 输出                  : 无
  65. *******************************************************************************/

  66. void IrInit()
  67. {
  68.         IT0=1;//下降沿触发
  69.         EX0=1;//打开中断0允许
  70.         EA=1;        //打开总中断

  71.         IRIN=1;//初始化端口
  72. }


  73. /*******************************************************************************
  74. * 函 数 名       : main
  75. * 函数功能                 : 主函数
  76. * 输    入       : 无
  77. * 输    出             : 无
  78. *******************************************************************************/
  79. //void main()
  80. //{        
  81. //        IrInit();
  82. //        while(1)
  83. //        {        
  84. //                DisplayData[0] = smgduan[IrValue[2]/16];
  85. //                DisplayData[1] = smgduan[IrValue[2]%16];
  86. //                DisplayData[2] = smgduan[16];
  87. //            DigDisplay();               
  88. //        }               
  89. //}

  90. /*******************************************************************************
  91. * 函数名         : ReadIr()
  92. * 函数功能                   : 读取红外数值的中断函数
  93. * 输入           : 无
  94. * 输出                  : 无
  95. *******************************************************************************/

  96. void ReadIr() interrupt 0
  97. {
  98.         u8 j,k;
  99.         u16 err;
  100.         Time=0;                                         
  101.         Irdelay(700);        //7ms
  102.         if(IRIN==0)                //确认是否真的接收到正确的信号
  103.         {         
  104.                
  105.                 err=1000;                                //1000*10us=10ms,超过说明接收到错误的信号
  106.                 /*当两个条件都为真是循环,如果有一个条件为假的时候跳出循环,免得程序出错的时
  107.                 侯,程序死在这里*/        
  108.                 while((IRIN==0)&&(err>0))        //等待前面9ms的低电平过去                  
  109.                 {                        
  110.                         Irdelay(1);
  111.                         err--;
  112.                 }
  113.                 if(IRIN==1)                        //如果正确等到9ms低电平
  114.                 {
  115.                         err=500;
  116.                         while((IRIN==1)&&(err>0))                 //等待4.5ms的起始高电平过去
  117.                         {
  118.                                 Irdelay(1);
  119.                                 err--;
  120.                         }
  121.                         for(k=0;k<4;k++)                //共有4组数据
  122.                         {                                
  123.                                 for(j=0;j<8;j++)        //接收一组数据
  124.                                 {

  125.                                         err=60;               
  126.                                         while((IRIN==0)&&(err>0))//等待信号前面的560us低电平过去
  127.                                         {
  128.                                                 Irdelay(1);
  129.                                                 err--;
  130.                                         }
  131.                                         err=500;
  132.                                         while((IRIN==1)&&(err>0))         //计算高电平的时间长度。
  133.                                         {
  134.                                                 Irdelay(10);         //0.1ms
  135.                                                 Time++;
  136.                                                 err--;
  137.                                                 if(Time>30)
  138.                                                 {
  139.                                                         return;
  140.                                                 }
  141.                                         }
  142.                                         IrValue[k]>>=1;         //k表示第几组数据
  143.                                         if(Time>=8)                        //如果高电平出现大于565us,那么是1
  144.                                         {
  145.                                                 IrValue[k]|=0x80;
  146.                                         }
  147.                                         Time=0;                //用完时间要重新赋值                                                        
  148.                                 }
  149.                         }
  150.                 }
  151.                 if(IrValue[2]!=~IrValue[3])
  152.                 {
  153.                         return;
  154.                 }
  155.         }                        
  156. }
  157. /**************************************/
  158. /*************LCD1602*****************/
  159. void delay(uint z) //延时函数
  160. {
  161.         uint x,y;
  162.         for(x=z;x>0;x--)
  163.                 for(y=120;y>0;y--);
  164. }


  165. void write_com(uchar com)  //写命令
  166. {
  167.         P0=com;
  168.         RS=0;
  169.         EN=0;
  170.         delay(10);
  171.         EN=1;
  172.         delay(10);
  173.         EN=0;
  174.                
  175. }
  176. void write_data(uchar date)          //写数据
  177. {
  178.         P0=date;
  179.         RS=1;
  180.         EN=0;
  181.         delay(10);
  182.         EN=1;
  183.         delay(10);
  184.         EN=0;
  185.                
  186. }
  187. void init()
  188. {
  189.     RW=0;
  190.         dula=0;
  191.         wela=0;
  192.         write_com(0x38);   //显示模式设置:16×2显示,5×7点阵,8位数据接口
  193.         delay(20);
  194.         write_com(0x0c);   //显示模式设置
  195.         delay(20);
  196.         write_com(0x06);   //显示模式设置:光标右移,字符不移
  197.         delay(20);
  198.         write_com(0x01);   //清屏幕指令,将以前的显示内容清除
  199.         delay(20);        
  200. }

  201. uchar *uchartostr(uchar num)        //将一个字节的数据转换为字符串 或10进制
  202. {
  203.         uchar x2,x1,x0,i;
  204.         x2=num/100;
  205.         x1=num%100/10;
  206.         x0=num%100%10;
  207.         i=0;
  208.         if(x2!=0)
  209.         {
  210.                 str[i]=x2+48;
  211.                 i++;
  212.         }
  213.         if(x1!=0)
  214.         {
  215.                 str[i]=x1+48;
  216.                 i++;
  217.         }
  218.         str[i]=x0+48;
  219.         i++;
  220.         str[i]='\0';
  221.         return str;
  222. }

  223. void lcd_dis(uchar X,uchar Y,uchar *dis)        //显示数据数组
  224. {                          
  225.    uchar  pos;
  226.         switch(X)
  227.         {
  228.                 case 0:X=0x00;break;
  229.                 case 1:X=0x40;break;
  230.                 break;
  231.                 default:break;
  232.         }

  233.    pos =0x80+X+Y;  
  234.    write_com(pos);     //显示地址
  235.    while(*dis!='\0')
  236.    {
  237.         write_data(*dis++);               
  238.    }
  239. }




  240. void delay_50us(unsigned int t)         //延时函数
  241. {
  242. unsigned int j,k;
  243. for (;t>0;t--)
  244. {
  245.   for(j=10;j>0;j--)
  246.   {
  247.    for(k=1;k>0;k--);
  248.   }
  249. }
  250. }





  251. /////////////////////////////18B20one//////////////////////////////

  252. void delay_18B201(unsigned int i)        //18b20全局延时
  253. {
  254.   while(i--);
  255. }

  256.   Init_DS18B201(void)
  257.   {
  258.   unsigned char x=0;
  259.   DS1=1;//DQ复位
  260.   delay_18B201(10);//稍做延时
  261.   DS1=0;//单片机将DQ拉低
  262.   delay_18B201(80);//精确延时大于480us
  263.   DS1=1;//拉高总线
  264.   delay_18B201(20);
  265.   x=DS1;//稍做延时后如果x=0则初始化成功x=1则初始化失败
  266.   delay_18B201(30);
  267.   return x;
  268. }
  269. //读一个字节
  270. ReadOneChar1(void)
  271. {
  272.    unsigned char i=0;
  273.    unsigned char dat=0;
  274.    for(i=8;i>0;i--)
  275.    {
  276.       DS1=0;//给脉冲信号
  277.       dat>>=1;
  278.       DS1=1;//给脉冲信号
  279.       if(DS1)
  280.       dat|=0x80;
  281.       delay_18B201(4);
  282.    }
  283.    return(dat);
  284. }
  285. //写一个字节
  286. WriteOneChar1(unsigned char dat)
  287. {
  288.    unsigned char i=0;
  289.    for(i=8;i>0;i--)
  290.    {
  291.       DS1=0;
  292.       DS1=dat&0x01;
  293.       delay_18B201(5);
  294.       DS1=1;
  295.       dat>>=1;
  296.    }
  297.    return(dat);
  298. }
  299. void ReadTemperature1(void)
  300. {
  301.    unsigned char L=0;
  302.    unsigned char H=0;
  303.    Init_DS18B201();
  304.    WriteOneChar1(0xCC);//跳过读序号列号的操作
  305.    WriteOneChar1(0x44);//启动温度转换
  306.    delay_18B201(100);
  307.    Init_DS18B201();
  308.    WriteOneChar1(0xCC);//跳过读序号列号的操作
  309.    WriteOneChar1(0xBE);//读取温度寄存器等(共可读9个寄存器前两个就是 温度               
  310.    L=ReadOneChar1();                 //温度低8位
  311.    H=ReadOneChar1();                 //温度高8位

  312.             f1=1;
  313.                 sdata1 = L/16+H*16;                   //整数部分
  314.             xiaoshu11 = (L&0x0f)*10/16;                 //小数第一位
  315.             xiaoshu12 =(L&0x0f)*100/16%10;        //小数第二位
  316.                              

  317. }
  318. ///////////////////18B20two/////////////
  319. void delay_18B202(unsigned int i)        //18b20全局延时
  320. {
  321.   while(i--);
  322. }

  323.   Init_DS18B202(void)
  324.   {
  325.   unsigned char x=0;
  326.   DS2=1;//DQ复位
  327.   delay_18B202(10);//稍做延时
  328.   DS2=0;//单片机将DQ拉低
  329.   delay_18B202(80);//精确延时大于480us
  330.   DS2=1;//拉高总线
  331.   delay_18B202(20);
  332.   x=DS2;//稍做延时后如果x=0则初始化成功x=1则初始化失败
  333.   delay_18B202(30);
  334.   return x;
  335. }
  336. //读一个字节
  337. ReadOneChar2(void)
  338. {
  339.    unsigned char i=0;
  340.    unsigned char dat=0;
  341.    for(i=8;i>0;i--)
  342.    {
  343.       DS2=0;//给脉冲信号
  344.       dat>>=1;
  345.       DS2=1;//给脉冲信号
  346.       if(DS2)
  347.       dat|=0x80;
  348.       delay_18B202(4);
  349.    }
  350.    return(dat);
  351. }
  352. //写一个字节
  353. WriteOneChar2(unsigned char dat)
  354. {
  355.    unsigned char i=0;
  356.    for(i=8;i>0;i--)
  357.    {
  358.       DS2=0;
  359.       DS2=dat&0x01;
  360.       delay_18B202(5);
  361.       DS2=1;
  362.       dat>>=1;
  363.    }
  364.    return(dat);
  365. }
  366. void ReadTemperature2(void)
  367. {
  368.    unsigned char L=0;
  369.    unsigned char H=0;
  370.    Init_DS18B202();
  371.    WriteOneChar2(0xCC);//跳过读序号列号的操作
  372.    WriteOneChar2(0x44);//启动温度转换
  373.    delay_18B202(100);
  374.    Init_DS18B202();
  375.    WriteOneChar2(0xCC);//跳过读序号列号的操作
  376.    WriteOneChar2(0xBE);//读取温度寄存器等(共可读9个寄存器前两个就是 温度               
  377.    L=ReadOneChar2();                 //温度低8位
  378.    H=ReadOneChar2();                 //温度高8位


  379.             f2=1;
  380.                 sdata2 = L/16+H*16;                   //整数部分
  381.             xiaoshu21 = (L&0x0f)*10/16;                 //小数第一位
  382.             xiaoshu22 =(L&0x0f)*100/16%10;        //小数第二位
  383.                              

  384. }


  385.   void display0()//显示welcome   to use the  
  386. {
  387.        unsigned char i,w,h=0x8f,i1,w1,h1=0xcf,w2,i2,h2,g=2;
  388.        init();
  389.            for(w=0;w<16;w++)  //显示欢迎
  390.            {
  391.                write_com(h--);//第一行自减左移
  392.                for(i=0;i<16;i++)
  393.                {
  394.                   write_data(huany[i]);
  395.                           delay_50us(5);
  396.                }
  397.            }
  398.            for(w1=0;w1<16;w1++)//第二行自减左移
  399.            {
  400.                write_com(h1--);
  401.                for(i1=0;i1<16;i1++)
  402.                {
  403.                   write_data(tt[i1]);
  404.                           delay_50us(5);
  405.                }
  406.            }
  407.                   
  408.        delay_50us(5000);//显示loading
  409.            init();
  410.            write_com(0x80);
  411.            for(w2=0;w2<16;w2++)//向右显示
  412.            {            
  413.            write_data(duqu[w2]);
  414.                    delay_50us(200);               
  415.            }
  416.            while(g--)//小数点循环亮灭
  417.            {   
  418.                write_com(0x8a);
  419.                for(i2=0;i2<3;i2++)
  420.                {
  421.                       write_data(duqu[i2]);

  422.                    }
  423.                    write_com(0x8a);
  424.                for(h2=10;h2<14;h2++)
  425.                    {  
  426.                       delay_50us(300);
  427.                       write_data(duqu[h2]);
  428.                           delay_50us(300);
  429.                    }
  430.            }

  431. }

  432. void display1()        //显示框架
  433. {
  434.        unsigned char p,q;
  435.        init();//全局清零
  436.        write_com(0x80);
  437.        for(q=0;q<16;q++)       //显示TMHL
  438.        {
  439.           write_data(tab1[q]);
  440.        }

  441.        write_com(0xc0);
  442.        for(p=0;p<16;p++)
  443.            {  
  444.        write_data(tab2[p]);
  445.            }
  446.                   write_com(0xc4);
  447.             write_data(0Xdf);
  448.                 write_com(0xcd);
  449.             write_data(0xdf);
  450. }


  451. void displayone(unsigned int date)         //显示温度,
  452. {
  453.       if(date/100==0) //100度内
  454.       {
  455.          write_com(0x80);
  456.          write_data(shuzu[date/10]);//十位
  457.          write_com(0x81);
  458.          write_data(shuzu[date%10]);//个位
  459.          write_com(0x82);
  460.          write_data(shuzu[13]);//小数点
  461.          write_com(0x83);
  462.          write_data(shuzu[xiaoshu11]);//小数点后一位
  463.          write_com(0x84);
  464.          write_data(shuzu[xiaoshu12]); //小数点后二位
  465.          write_com(0x85);
  466.          write_data(0xdf);
  467.                  }
  468. }
  469. void displaytwo(unsigned int date)         //显示温度,
  470. {
  471.       if(date/100==0) //100度内
  472.       {

  473.          write_com(0x88);
  474.          write_data(shuzu[date/10]);//十位
  475.          write_com(0x89);
  476.          write_data(shuzu[date%10]);//个位
  477.          write_com(0x8a);
  478.          write_data(shuzu[13]);//小数点
  479.          write_com(0x8b);
  480.          write_data(shuzu[xiaoshu21]);//小数点后一位
  481.          write_com(0x8c);
  482.          write_data(shuzu[xiaoshu22]); //小数点后二位
  483.          write_com(0x8d);
  484.          write_data(0xdf);


  485.      }
  486. }


  487. //void fuhao()        //温度正负子程序
  488. //{
  489. //            if(f==1) //温度为正
  490. //         {
  491. //            write_com(0x84);
  492. //        write_data(shuzu[12]);
  493. //         }
  494. //         else if(f==0) //温度为负,显示负号
  495. //         {
  496. //            write_com(0x84);
  497. //        write_data(shuzu[11]);
  498. //         }
  499. //}








  500. void key()
  501. {
  502.         Key_value = P1;

  503.         Key_value = Key_value & 0x0f;

  504.         if ( Key_value != 0x0f )
  505.                   {
  506.                         switch(Key_value)
  507.                         {
  508.                                 case 0x0e: u++; break;
  509.                                 case 0x0d: u--; break;
  510.                                 case 0x0b: l++; break;
  511.                                 case 0x07: l--; break;
  512.                         }

  513.                    }        
  514.         }

  515. void  compare()//温度数据比较
  516. {


  517.            if(sdata1>=u) //温度大于上限,开冷关热
  518.              {
  519.               displayone(sdata1);
  520.                           displaytwo(sdata2);   
  521.                           Dcfcold =1;                   //1.5
  522.                           Dcfhot  =0;                           //1.3
  523.              }
  524.             else if(l>0&sdata1<=l)        //温度小于下限,开热关冷
  525.                    {
  526.                      displayone(sdata1);
  527.                                          displaytwo(sdata2);                                       
  528.                                          Dcfcold =0;     //1.5
  529.                                          Dcfhot  =1;           //1.4
  530.                                    }
  531.                         else if(sdata1<u&sdata1>l)
  532.                                    {
  533.                                      displayone(sdata1);
  534.                                           displaytwo(sdata2);
  535.                                          Dcfcold =0;    //1.5
  536.                                          Dcfhot  =0;           //1.4
  537.                                          Dcfmix  =0;
  538.                                    }
  539.             else         
  540.                         displayone(sdata1);
  541.                         displaytwo(sdata2);                                 
  542.   }        

  543. void main()
  544. {

  545. //P1=0x00;
  546.   u=35;
  547.   l=20;
  548.   IrInit();
  549.   display0(); //欢迎,
  550.   display1();//框架
  551.   while(1)
  552.   {
  553.                  lcd_dis(1,2,uchartostr( u ) );
  554.              lcd_dis(1,11,uchartostr( l ) );
  555.          ReadTemperature1();        //测温程序
  556.                  ReadTemperature2();        //测温程序
  557.         // fuhao();//负号显示程序
  558.          key();        //按键
  559.          compare();
  560.                 Ir_work();
  561.   }

  562. }
复制代码

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

使用道具 举报

沙发
ID:96682 发表于 2019-5-25 22:56 | 只看该作者
???是在同一个空间还是不同空间?
回复

使用道具 举报

板凳
ID:455316 发表于 2019-5-26 10:03 | 只看该作者
wc86110 发表于 2019-5-25 22:56
???是在同一个空间还是不同空间?

同一空间
回复

使用道具 举报

地板
ID:96682 发表于 2019-5-26 13:36 | 只看该作者
同一空间存在温差也很正常,得看两个探头所在周围环境有没有热源(温差)
回复

使用道具 举报

5#
ID:111517 发表于 2019-5-26 14:16 | 只看该作者
你这个是用的是几个DS18B20呢?两个18B20应该得 有使用序列号相关的代码吧,我没有看见相关代码
回复

使用道具 举报

6#
ID:544389 发表于 2019-5-26 15:53 | 只看该作者
如果这个温差是恒定的话,可以考虑后期先对数据进行处理(加减定值),然后再显示
回复

使用道具 举报

7#
ID:241073 发表于 2019-5-27 09:49 | 只看该作者
这是我曾用32写的,可做参考!!!http://www.51hei.com/bbs/dpj-161142-1.html
回复

使用道具 举报

8#
ID:330198 发表于 2019-5-27 11:22 | 只看该作者
两个端口有没有上拉电阻?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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