深圳市瑞申电子有限公司

深圳市瑞申电子有限公司是一家10年专业大功率电感生产加工厂商,主要以大功率,大电流、扁平线圈电感、平面变压器设计、生产、销售工厂。设计、绕线、组装、检测、包装、出货等全制程的工艺流程!拥有完整、科学的质量管理体系。专业技术团队10人,我们的诚信、实力和产品质量获得业界的认可。欢迎各界朋友莅临参观、指导和业务洽谈。 ...

关于ds18b20的温度采集显示0解惑

时间:2021-05-14 06:15:08 点击:
uint Read_temperature()
{
        uint temt;
        unsigned long int temperature = 0;
        uchar dat_l = 0,dat_h = 0;
         Init_da18b20();
        //************开始转换**************
         Writer_ds18b20(0xcc);//忽视r o m指令 后面跟44h可完成温度转换
         Writer_ds18b20(0x44);
         delay_ms(800);
         //**********读暂存数据************
         Init_da18b20();
         Writer_ds18b20(0xcc);
         Writer_ds18b20(0xbe);//一个从机只能跟一条读寄存器指令
         dat_l = Read_ds18b20();
         dat_h = Read_ds18b20();
         Init_da18b20();//***********读取暂存结束*************

         //temperature = (dat_h * 256 )+ dat_l;
           temperature = dat_h;
           temperature <<= 8;
           temperature += dat_l;
           temt =temperature * 0.0625;

   return temt; //temperature;
}
我的ds18b20的温度采集函数是这样的   这是最终版   一开始时候显示00.00 后来发现是声明的问题   一开始我只有 uint temt的反回值  没有temperature这个中间变量   返回值都为00.00  是uint 即unsigned int的范围不够  溢出 。还发现temperature = (dat_h * 256 )+ dat_l;这样显示的温度是实际温度的2倍   不能使用()也很奇怪  想不通


给你一个现成的程序
#include
#define uchar unsigned char
#define uint unsigned int
//=====================================================
sbit DQ=P1^6;        
//======================================================      
sbit LED1=P1^3;
sbit LED2=P1^2;
sbit FMQ=P1^1;
//======================================================
unsigned int temp, temp1,temp2, xs;
//======================================================
void delay1(uint m)     
    {
      uint i,j;
      for(i=m;i>0;i--)
       for(j=110;j>0;j--);
         }
//======================================================
void delay(uint m)   
    {
      while(m--);
         }
//======================================================
void Init_DS18B20()            //初始化18b20
{
unsigned char x=0;
DQ = 1;         
delay(8);
DQ = 0;         
delay(80); //>480us
DQ = 1;            
delay(4);
x=DQ;     //延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}
//=========================================================
uchar readchar()          //读出一字节
{
uchar i=0;
uchar dat = 0;
for (i=0;i<8;i++)
{
    DQ = 0;
    dat>>=1;
    DQ = 1;
    if(DQ)
    dat|=0x80;   
    delay(4);
}
   return(dat);
}
//===================================================
writechar(unsigned char dat)         //写入一字节
{
   unsigned char i=0;
   for (i=0; i<8; i++)
   {
    DQ = 0;
    DQ = dat&0x01;   
    delay(5); //60us~120us
    DQ = 1;        
    dat>>=1; //从最低位到最高位传入
}
}
//===========================================================
readtemperature()                                          //读取温度
  {
    uchar a=0;
    unsigned  b=0;
    unsigned  t=0;

    Init_DS18B20();
    writechar(0xCC);    // 跳过读序号列号的操作/
    writechar(0x44); // 启动温度转换
    delay(5);       // 重要
    Init_DS18B20();
    writechar(0xCC); //跳过读序号列号的操作
    writechar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度/
    delay(3);
    a=readchar();    //读取温度值低位  /
    b=readchar();      //读取温度值高位    /
    temp1=b<<4;            //高8位中后三位数的值
    temp1+=(a&0xf0)>>4;      //低8位中的高4位值加上高8位中后三位数的值   temp1室温整数值
    temp2=a&0x0f;             //小数的值
    temp=((b*256+a)>>4);    //当前采集温度值除16得 实际温度值    zhenshu   
    xs=temp2*0.0625*10; //小数位,若为0.5则算为5来显示   xs小数    xiaoshu
//===============================================================================
             if(temp>=30.0)                                //报警模块
           FMQ=0;          
           else
           FMQ=1;
           if(temp>=25.0 && temp<30.0)
           LED1=0,
           LED2=1;
           else
           LED1=1;
  }
//================================================================
void wenduxianshi()                 //数码管显示
{
                int a,b,c;
                a=(temp/10);
                switch(a)
                {
                        case 0 :P2=0x00 |0x04;
                                        P3=0x28;
                                        break;
                        case 1 :P2=0x18 |0x04;
                                        P3=0xe8;
                                        break;
                        case 2 :P2=0x10 |0x04;
                                        P3=0x30;
                                        break;
                        case 3 :P2=0x10 |0x04;
                                        P3=0xa0;
                                        break;
                        case 4 :P2=0x08 |0x04;
                                        P3=0xe0;
                                        break;
                        case 5 :P2=0x80 |0x04;
                                        P3=0xa0;
                                        break;
                        case 6 :P2=0x80 |0x04;
                                        P3=0x20;
                                        break;
                        case 7 :P2=0x10 |0x04;
                                        P3=0xe8;
                                        break;
                        case 8 :P2=0x00 |0x04;
                                        P3=0x20;
                                        break;
                        case 9 :P2=0x00 |0x04;
                                        P3=0xa0;
                                        break;
                }
                delay(800);
            b=(temp%10);
                switch(b)
                                {
                                        case 0 :P2=0x00 |0x20;
                                                        P3=0x28 &0xdf;
                                                        break;
                                        case 1 :P2=0x18 |0x20;
                                                        P3=0xe8 &0xdf;
                                                        break;
                                        case 2 :P2=0x10 |0x20;
                                                        P3=0x30 &0xdf;
                                                        break;
                                        case 3 :P2=0x10 |0x20;
                                                        P3=0xa0 &0xdf;
                                                        break;
                                        case 4 :P2=0x08 |0x20;
                                                        P3=0xe0 &0xdf;
                                                        break;
                                        case 5 :P2=0x80 |0x20;
                                                        P3=0xa0 &0xdf;
                                                        break;
                                        case 6 :P2=0x80 |0x20;
                                                        P3=0x20 &0xdf;
                                                        break;
                                        case 7 :P2=0x10 |0x20;
                                                        P3=0xe8 &0xdf;
                                                        break;
                                        case 8 :P2=0x00 |0x20;
                                                        P3=0x20 &0xdf;
                                                        break;
                                        case 9 :P2=0x00 |0x20;
                                                        P3=0xa0 &0xdf;
                                                        break;
                                 }
                 delay(800);


            c=(xs%10);
                switch(c)
                {
                        case 0 :P2=0x00 |0x40;
                                        P3=0x28;
                                        break;
                        case 1 :P2=0x18 |0x40;
                                        P3=0xe8;
                                        break;
                        case 2 :P2=0x10 |0x40;
                                        P3=0x30;
                                        break;
                        case 3 :P2=0x10 |0x40;
                                        P3=0xa0;
                                        break;
                        case 4 :P2=0x08 |0x40;
                                        P3=0xe0;
                                        break;
                        case 5 :P2=0x80 |0x40;
                                        P3=0xa0;
                                        break;
                        case 6 :P2=0x80 |0x40;
                                        P3=0x20;
                                        break;
                        case 7 :P2=0x10 |0x40;
                                        P3=0xe8;
                                        break;
                        case 8 :P2=0x00 |0x40;
                                        P3=0x20;
                                        break;
                        case 9 :P2=0x00 |0x40;
                                        P3=0xa0;
                                        break;
                        }
                                         delay(800);


                P2=0x80;
                P3=0x38 |0x04;
                       


       
}
//======================================================
main()
{
         while(1)
         {     
       readtemperature();
               

       wenduxianshi();
          
         }
}
//=======================================================


现成代码我也有   但是我在意的是我的代码到底哪里出问题了

大功率电感厂家 |大电流电感工厂

  • “国家海岸”旁的精彩网络
    2012年初开业,便相继赢得了媒体评出的 最佳海滨度假酒店 、 2012海南酒店金椰奖 等奖项,在短短的几个月内,三亚海棠湾喜来登度假酒店迅速成为了三亚地区最受关注的高端酒店之一。 作为全球最
  • 交流点焊机改高频逆变
    有哪位师傅可以把老式的交流点焊机(排焊机、龙门机)改成高频逆变?随时发邮件给我:ggs215@163.com
  • 基于MEMS加速度传感器五大功能简化用户设计
    4月08日 第三届·无线通信技术研讨会 立即报名 12月04日 2015•第二届中国IoT大会 精彩回顾 10月30日ETF•智能硬件开发技术培训会 精彩回顾 10月23日ETF•第三届 消费
  • 大功率电感