STM32/STM8技术william hill官网
直播中

一二三4

7年用户 9经验值
擅长:可编程逻辑 控制/MCU
私信 关注
[问答]

DS18B20温度刚开始显示为+85度,几秒后温度正常?

请问怎么增加延时使得温度直接测量,没有+85度,并且通过按键增加/减少的温度显示正常,不延时。
代码如下:
main:
#include
#include"lcd.h"
#include"temp.h"
void LcdDisplay(int);
***it K1=P2^0;
***it K2=P2^1;
***it K3=P2^2;
***it K4=P2^3;
extern int th=20;
extern int tl=-10;
***it beep=P1^3;
/***************
蜂鸣器
*************/
void SPEAK()
{
                beep=0;
                Delay1ms(50);
                beep=1;        
}
/*********
主程序
**********/
void main()
{
    LcdInit();        
        LcdWriteCom(0xc7);   
        LcdWriteData('C');
        while(1)                                  //按键扫描
        {  
           if(K1==0)  
           {   
             Delay1ms(500);  
            if(K1==0);  
            th++;                                  //增加温度上限
       }  
           if(K2==0)  
          {   
           Delay1ms(500);
           if(K2==0);  
           th--;                                  //减小温度上限
          }
         if(K3==0)  
          {   
            Delay1ms(500);
            if(K3==0);  
            tl++;                                   //增加温度下限
          }  
         if(K4==0)  
         {   
            Delay1ms(500);  
            if(K4==0);
            tl--;                                   //减小温度下限
          }

           LcdDisplay(Ds18b20ReadTemp());  //显示读取温度
           }  
}  
void LcdDisplay(int temp)   
{      
     int i,tt,rr,mm;     
         unsigned char datas[] = {0, 0, 0, 0},datas1[] = {0, 0, 0},datas2[] = {0, 0, 0};
         float tp;   
         if(temp< 0)                                          //判断温度是否为负值
          {   
            LcdWriteCom(0xc0);                      //负温度,显示"-"
            LcdWriteData('-');      
                i=1;   
                temp=temp-1;   
                temp=~temp;   
                tp=temp;
                temp=tp*0.0625*10+0.5;           //将温度的高位与低位合并对结果进行4舍5入
                 mm=-temp;   
          }  
        else   
                {                                                 //正温度,显示"+"
                  LcdWriteCom(0xc0);      
                  LcdWriteData('+');      
                  tp=temp;
                  temp=tp*0.0625*10+0.5;           //将温度的高位与低位合并对结果进行4舍5入
                  mm=temp;   
                }   
                datas[0] = temp / 1000;                   //百位
                datas[1] = temp % 1000 / 100;             //十位
                datas[2] = temp % 100 / 10;            //个位
                datas[3] = temp% 10;                             //小数位
                if(th < 0)      
           {     
                  LcdWriteCom(0x89);      
                  LcdWriteData('-');      
                  tt=-th;   
           }  
                else
                {      
                   LcdWriteCom(0x89);      
                   LcdWriteData('+');
                   tt=th;   
                }   
                datas1[0] = tt / 100;  
                datas1[1] = tt% 100 / 10;  
                datas1[2] = tt % 10;  
                LcdWriteCom(0x87);                                //温度上限显示
                LcdWriteData('H');  
                LcdWriteCom(0x88);     
                LcdWriteData(':');  
                LcdWriteCom(0x8a);      
                LcdWriteData('0'+datas1[0]);
            LcdWriteCom(0x8b);      
                LcdWriteData('0'+datas1[1]);  
                LcdWriteCom(0x8c);      
                LcdWriteData('0'+datas1[2]);   
                if(tl < 0)
                  {
                        LcdWriteCom(0x90);      
                        LcdWriteData('-');      
                        rr=-tl;     
                  }   
            else   
                  {      
                        LcdWriteCom(0x90);      
                        LcdWriteData('+');     
                        rr=tl;   
                  }   
                        datas2[0] = rr / 100;  
                        datas2[1] = rr% 100 / 10;  
                        datas2[2] = rr % 10;  
                        LcdWriteCom(0x8e);                          //温度下限显示
                        LcdWriteData('L');  
                        LcdWriteCom(0x8f);      
                        LcdWriteData(':');
                        LcdWriteCom(0x91);      
                        LcdWriteData('0'+datas2[0]);  
                        LcdWriteCom(0x92);      
                        LcdWriteData('0'+datas2[1]);  
                    LcdWriteCom(0x93);      
                        LcdWriteData('0'+datas2[2]);
                                                         
                        if(mm>=(th*10)||mm<=(tl*10)||th<=tl)
                           {
                                P0=0;
                                SPEAK();
                           }
                        else               
                        {
                                P0=0xff;
                                beep=1;
                        }
                                                            
                        LcdWriteCom(0x80);      
                        LcdWriteData('T');   
                        LcdWriteCom(0x81);      
                        LcdWriteData('E');  
                    LcdWriteCom(0x82);      
                        LcdWriteData('M');
                        LcdWriteCom(0x83);   
                        LcdWriteData('P');
                        LcdWriteCom(0x84);      
                        LcdWriteData(' ');   
                        LcdWriteCom(0x85);     
                        LcdWriteData(' ');   
                        LcdWriteCom(0xc1);      
                        LcdWriteData('0'+datas[0]);   
                        LcdWriteCom(0xc2);      
                        LcdWriteData('0'+datas[1]);   
                        LcdWriteCom(0xc3);      
                        LcdWriteData('0'+datas[2]);   
                        LcdWriteCom(0xc4);     
                        LcdWriteData('.');      
                        LcdWriteCom(0xc5);   
                        LcdWriteData('0'+datas[3]);  
                        LcdWriteCom(0xc6);     
                        LcdWriteData('"');
                        LcdWriteCom(0xc7);     
                        LcdWriteData('C');  
  }      
temp:   

#include"temp.h"  
void Delay1ms(unsigned int y) //延时1MS函数
{  
   unsigned int x;  
   for(y;y>0;y--)   
   for(x=110;x>0;x--);  
}  
unsigned char Ds18b20Init() //初始化
{  
   unsigned int i;  
   DSPORT=0;      
   i=70;   
   while(i--);   
   DSPORT=1;
   i=0;   
   while(DSPORT)  
    {   
      i++;   
      if(i>5000)     
      return 0;//失败   
    }   
      return 1;//成功
}  
void Ds18b20WriteByte(unsigned char dat)   //写字节
{  
    unsigned int i,j;  
    for(j=0;j<8;j++)  
      {     
       DSPORT=0;     //每写入一位数据之前先把总线拉 低1us (数据手册上模糊)   
       i++;
           DSPORT=dat&0x01;   
           i=6;   
           while(i--);   
           DSPORT=1;   
           dat>>=1;   
           }  
}  
         unsigned char Ds18b20ReadByte()
           {  
             unsigned char byte,bi;
             unsigned int i,j;   
                 for(j=8;j>0;j--)  
                   {   
                    DSPORT=0;
                    i++;  
                    DSPORT=1;   
                     i++;
                        
             i++;   
                         bi=DSPORT;   
                         byte=(byte>>1)|(bi<<7);        
                         i=4;   
                         while(i--);
                        }      
                return byte;  
                }  
void Ds18b20ChangTemp() //温度转换
{

        Ds18b20Init();  
        Delay1ms(1);  
        Ds18b20WriteByte(0xcc);   
        Ds18b20WriteByte(0x44);

}
void Ds18b20ReadTempCom() //读取温度命令
{
        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);          //跳过ROM
        Ds18b20WriteByte(0xbe);          //发送读温度命令
}  
int Ds18b20ReadTemp() //读取温度
{  
        int temp=0;  
        unsigned char tmh,tml;
        Delay1ms(750);
        Ds18b20ChangTemp();   
        Ds18b20ReadTempCom();
        tml=Ds18b20ReadByte();             //读取温度值低位
        tmh=Ds18b20ReadByte();            //读取温度值高位
        temp=tmh;
        temp<<=8;
        temp|=tml;                        //高低8位结合
                                                                                                                  
    return temp;
}

已退回2积分

回帖(2)

刘青贺

2017-5-25 11:01:17
我也遇到了这个情况,一开始温度很高得有80或者90左右吧,后来就稳定了,不知道是什么原因,你这解决了吗?
举报

cjjcjj1

2017-5-25 13:38:50
你好!这是正常情况,在正式测温前,读取一次温度,读取后延时 750 毫秒以上,问题就能解决
举报

更多回帖

发帖
×
20
完善资料,
赚取积分