微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > DS18B20温度读不出来

DS18B20温度读不出来

时间:10-02 整理:3721RD 点击:
我用51单片机读18B20的温度,读不出来,程序是这样的

#include<reg52.h>

#include<stdio.h>

#define unchar unsigned char

#define unint unsigned int

sbit tda=P1^4;

unchar i,t;

unint tt;

void delay(unchar z)

{

       uncharx,y;

       for(x=0;x<z;x++)

              for(y=0;y<110;y++);

}

void reset()

{

       tda=1;

       i++;

       tda=0;

       i=103;

       while(i>0)

              i--;

       tda=1;

       i=4;

       while(i>0)

              i--;

}

void write(unchar date)

{

       unchartemp;

       for(t=0;t<8;t++)

       {

              temp=date&0x01;

              date=date>>1;

              if(temp==1)

              {

                     tda=0;

                     i++;

                     i++;

                     tda=1;

                     i=8;

                     while(i>0)

                            i--;

              }

              else

              {

                     tda=0;

                     i=8;

                     while(i>0)

                            i--;

                     tda=1;

                     i++;

                     i++;

              }

              tda=1;

       }

}

bit read_bit()

{

       bitdat;

       tda=0;

       i++;

       tda=1;

       i++;

       i++;

       dat=tda;

       i=8;

       while(i>0)

              i--;

       return(dat);

}

unchar read()

{

       unchardat,j,z;

       dat=0;

       for(z=1;z<=8;z++)

       {

              j=read_bit();

              dat=(j<<7)|(dat>>1);

       }

       return(dat);

}

void start()

{

       reset();

       delay(1);

       write(0xcc);

       write(0x44);

}

unint get()

{

       unchara,b;

       uninttemp;

       temp=0x00;

       reset();

       delay(1);

       write(0xcc);

       write(0xbe);

       a=read();

       b=read();

       temp=b;

       temp=temp<<8;

       temp=temp|a;

       temp=temp*0.0625+0.5;

       return(temp);

}

main()

{

       while(1)

       {

              start();

              delay(5);

              tt=get();

              if(tt>0&&tt<=25)

                     P2=0x01;

              if(tt>25&&tt<=30)

                     P2=0x02;

              if(tt>30&&tt<=35)

                     P2=0x04;

              if(tt>35)

                     P2=0x0f;

              if(tt==0)

                     P2=0x00;

       }

}


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top