【郭天祥 TX-1C经典版 51单片机开发板试用体验】+18b20

  • mr_liu
  • LV4工程师
  • |      2017-09-29 23:07:26
  • 浏览量 956
  • 回复:1
本帖最后由 mr_liu 于 2017-9-29 23:26 编辑 bg1.png五、 DS18B20+数码管显示 1、介绍 实现ds18b20温度采集,并实时显示在数码管上 2、效果图【当把手放到传感器上,温度快速上升】 3、18b20驱动程序分享
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); 

  }

  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;

}
  • 0
  • 收藏
  • 举报
  • 分享
我来回复

登录后可评论,请 登录注册

所有回答 数量:1
great_CC 2017-10-30
old brother,stable.
0   回复
举报
发布
x
收藏成功!点击 我的收藏 查看收藏的全部帖子