小弟 学习18b20 不会读数

2020-02-04 09:10发布

小弟 想用12864显示18b20温度  可是对 18b20读数不了解  在网上找的程序 没有解释 看不太懂  求 哪位前辈  传个以前做过的程序  或指点一下思路   小弟 万分感谢
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
17条回答
INT0
2020-02-06 15:49
贴一个

/*DS18B20 正常显示 by whj 11 11 18*/
#include<reg52.h>
//#include<intrins.h>
//#include<absacc.h>
//#include <stdio.h>  
#define uint  unsigned int   
#define uchar unsigned char
   

sbit DS = P2^0; //DS18B20的数据输入/输出脚DQ,根据情况设定
uchar flag1;            //正负标志

uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
uchar code wei[]={0x00,0x01,0x02,0x04,0x08};

uint temp,t;

void delay(uint count)      //delay
{
  uint i;
  while(count)
  {
    i=200;
    while(i>0)
    i--;
    count--;
  }
}
void dsreset(void)       //send reset and initialization command
{
  uint i;
  DS=0;
  i=103;
  while(i>0)i--;
  DS=1;
  i=4;
  while(i>0)i--;
}

bit tmpreadbit(void)       //read a bit
{
   uint i;
   bit dat;
   DS=0;i++;          //i++ for delay
   DS=1;i++;i++;
   dat=DS;
   i=8;while(i>0)i--;
   return (dat);
}

uchar tmpread(void)   //read a byte date
{
  uchar i,j,dat;
  dat=0;
  for(i=1;i<=8;i++)
  {
    j=tmpreadbit();
    dat=(j<<7)|(dat>>1);   //读出的数据最低位在最前面,这样刚好一个字节在DAT里
  }
  return(dat);
}

void tmpwritebyte(uchar dat)   //write a byte to ds18b20
{
  uint i;
  uchar j;
  bit testb;
  for(j=1;j<=8;j++)
  {
    testb=dat&0x01;
    dat=dat>>1;
    if(testb)     //write 1
    {
      DS=0;
      i++;i++;
      DS=1;
      i=8;while(i>0)i--;
    }
    else
    {
      DS=0;       //write 0
      i=8;while(i>0)i--;
      DS=1;
      i++;i++;
    }

  }
}

void tmpchange(void)  //DS18B20 begin change
{
  dsreset();
  delay(1);
  tmpwritebyte(0xcc);  // address all drivers on bus
  tmpwritebyte(0x44);  //  initiates a single temperature conversion
}

uint tmp()               //get the temperature
{
  float tt;
  uchar a,b;
  dsreset();
  delay(1);
  tmpwritebyte(0xcc);
  tmpwritebyte(0xbe);
  a=tmpread();
  b=tmpread();
  temp=b;
  temp<<=8;             //two byte  compose a int variable
  temp=temp|a;
  tt=temp*0.0625;
  temp=tt*10+0.5;
  return temp;
}
void display(uint temp)
{
        uchar A1,A2,A2t,A3;
        A1=temp/100;
        A2t=temp%100;
        A2=A2t/10;
        A3=A2t%10;
                t=400;
                P1= wei[1];;
               
                if (flag1==1)
                {
                P0=0x40;
                }
                else
                {
                P0=0x00;
                }
                while(t--);
               
                t=400;
                P1= wei[2];
                P0=table[A1];
                while(t--);
               
                t=400;
                P1= wei[3];                 
                P0=table1[A2];
                while(t--);
               
                t=400;
                P1= wei[4];
                P0=table[A3];
                while(t--);
                P1=wei[0];
               
               
}

void main()
{
uchar a;
  do
  {
    tmpchange();
        for(a=10;a>0;a--)
          {   
                  display(tmp());
          }
  } while(1);
}

一周热门 更多>