当前位置: 代码迷 >> 单片机 >> 有关DS18B20的,初学不如何明白
  详细解决方案

有关DS18B20的,初学不如何明白

热度:132   发布时间:2016-04-28 16:18:02.0
有关DS18B20的,初学不怎么明白!
大家好,最近看DS18B20的程序,有一些不明白,请大家指教!
#include   <reg52.h>
#define   uchar   unsigned   char
#define   uint   unsigned   int
sbit   DS=P2^2;                       //define   interface
uint   temp;                           //   variable   of   temperature
uchar   flag1;                         //   sign   of   the   result   positive   or   negative
sbit   dula=P2^6;
sbit   wela=P2^7;
unsigned   char   code   table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
                                                0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned   char   code   table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
                                                0x87,0xff,0xef};

void   delay(uint   count)             //delay
{
    uint   i;
    while(count)
    {
        i=200;
        while(i> 0)
        i--;
        count--;
    }
}
///////功能:串口初始化,波特率9600,方式1///////
void   Init_Com(void)
{
          TMOD   =   0x20;
          PCON   =   0x00;
          SCON   =   0x50;
          TH1   =   0xFd;
          TL1   =   0xFd;
          TR1   =   1;
}

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;