專(zhuān)注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

DS18B20數(shù)字溫度計(jì)單片機(jī)接口應(yīng)用

作者:89s51   來(lái)源:不詳   點(diǎn)擊數(shù):  更新時(shí)間:2007年05月16日   【字體:

1.DS18B20基本知識(shí)

 DS18B20數(shù)字溫度計(jì)是DALLAS公司生產(chǎn)的1-Wire,即單總線器件,具有線路簡(jiǎn)單,體積小的特點(diǎn)。因此用它來(lái)組成一個(gè)測(cè)溫系統(tǒng),具有線路簡(jiǎn)單,在一根通信線,可以掛很多這樣的數(shù)字溫度計(jì),十分方便。

1、DS18B20產(chǎn)品的特點(diǎn)

  (1)、只要求一個(gè)端口即可實(shí)現(xiàn)通信。

 。2)、在DS18B20中的每個(gè)器件上都有獨(dú)一無(wú)二的序列號(hào)。

 。3)、實(shí)際應(yīng)用中不需要外部任何元器件即可實(shí)現(xiàn)測(cè)溫。

 。4)、測(cè)量溫度范圍在-55C到+125。C之間。

  (5)、數(shù)字溫度計(jì)的分辨率用戶可以從9位到12位選擇。

  (6)、內(nèi)部有溫度上、下限告警設(shè)置。

2、DS18B20的引腳介紹

  TO-92封裝的DS18B20的引腳排列見(jiàn)圖1,其引腳功能描述見(jiàn)表1。

(底視圖)

圖1

 

表1 DS18B20詳細(xì)引腳功能描述

序號(hào)

名稱(chēng)

引腳功能描述

1

GND

地信號(hào)

2

DQ

數(shù)據(jù)輸入/輸出引腳。開(kāi)漏單總線接口引腳。當(dāng)被用著在寄生電源下,也可以向器件提供電源。

3

VDD

可選擇的VDD引腳。當(dāng)工作于寄生電源時(shí),此引腳必須接地。

3.             DS18B20的使用方法

由于DS18B20采用的是1-Wire總線協(xié)議方式,即在一根數(shù)據(jù)線實(shí)現(xiàn)數(shù)據(jù)的雙向傳輸,而對(duì)AT89S51單片機(jī)來(lái)說(shuō),硬件上并不支持單總線協(xié)議,因此,我們必須采用軟件的方法來(lái)模擬單總線的協(xié)議時(shí)序來(lái)完成對(duì)DS18B20芯片的訪問(wèn)。

由于DS18B20是在一根I/O線上讀寫(xiě)數(shù)據(jù),因此,對(duì)讀寫(xiě)的數(shù)據(jù)位有著嚴(yán)格的時(shí)序要求。DS18B20有嚴(yán)格的通信協(xié)議來(lái)保證各位數(shù)據(jù)傳輸?shù)恼_性和完整性。該協(xié)議定義了幾種信號(hào)的時(shí)序:初始化時(shí)序、讀時(shí)序、寫(xiě)時(shí)序。所有時(shí)序都是將主機(jī)作為主設(shè)備,單總線器件作為從設(shè)備。而每一次命令和數(shù)據(jù)的傳輸都是從主機(jī)主動(dòng)啟動(dòng)寫(xiě)時(shí)序開(kāi)始,如果要求單總線器件回送數(shù)據(jù),在進(jìn)行寫(xiě)命令后,主機(jī)需啟動(dòng)讀時(shí)序完成數(shù)據(jù)接收。數(shù)據(jù)和命令的傳輸都是低位在先。

DS18B20的復(fù)位時(shí)序

DS18B20的讀時(shí)序

對(duì)于DS18B20的讀時(shí)序分為讀0時(shí)序和讀1時(shí)序兩個(gè)過(guò)程。

對(duì)于DS18B20的讀時(shí)隙是從主機(jī)把單總線拉低之后,在15秒之內(nèi)就得釋放單總線,以讓DS18B20把數(shù)據(jù)傳輸?shù)絾慰偩上。DS18B20在完成一個(gè)讀時(shí)序過(guò)程,至少需要60us才能完成。

DS18B20的寫(xiě)時(shí)序

對(duì)于DS18B20的寫(xiě)時(shí)序仍然分為寫(xiě)0時(shí)序和寫(xiě)1時(shí)序兩個(gè)過(guò)程。

對(duì)于DS18B20寫(xiě)0時(shí)序和寫(xiě)1時(shí)序的要求不同,當(dāng)要寫(xiě)0時(shí)序時(shí),單總線要被拉低至少60us,保證DS18B20能夠在15us到45us之間能夠正確地采樣IO總線上的“0”電平,當(dāng)要寫(xiě)1時(shí)序時(shí),單總線被拉低之后,在15us之內(nèi)就得釋放單總線。

 

 

4.             實(shí)驗(yàn)任務(wù)

用一片DS18B20構(gòu)成測(cè)溫系統(tǒng),測(cè)量的溫度精度達(dá)到0.1度,測(cè)量的溫度的范圍在-20度到+100度之間,用8位數(shù)碼管顯示出來(lái)。

5.            電路原理圖

6.            系統(tǒng)板上硬件連線

(1).    把“單片機(jī)系統(tǒng)”區(qū)域中的P0.0-P0.7用8芯排線連接到“動(dòng)態(tài)數(shù)碼顯示”區(qū)域中的ABCDEFGH端子上。

(2).    把“單片機(jī)系統(tǒng)”區(qū)域中的P2.0-P2.7用8芯排線連接到“動(dòng)態(tài)數(shù)碼顯示”區(qū)域中的S1S2S3S4S5S6S7S8端子上。

(3).    把DS18B20芯片插入“四路單總線”區(qū)域中的任一個(gè)插座中,注意電源與地信號(hào)不要接反。

(4).    把“四路單總線”區(qū)域中的對(duì)應(yīng)的DQ端子連接到“單片機(jī)系統(tǒng)”區(qū)域中的P3.7/RD端子上。

7.            C語(yǔ)言源程序

#include <AT89X52.H>

#include <INTRINS.h>

unsigned char code displaybit[]={0xfe,0xfd,0xfb,0xf7,

                                 0xef,0xdf,0xbf,0x7f};

unsigned char code displaycode[]={0x3f,0x06,0x5b,0x4f,

                                    0x66,0x6d,0x7d,0x07,

                                    0x7f,0x6f,0x77,0x7c,

                                    0x39,0x5e,0x79,0x71,0x00,0x40};

unsigned char code dotcode[32]={0,3,6,9,12,16,19,22,

                                25,28,31,34,38,41,44,48,

                                50,53,56,59,63,66,69,72,

                                75,78,81,84,88,91,94,97};

unsigned char displaycount;

unsigned char displaybuf[8]={16,16,16,16,16,16,16,16};

unsigned char timecount;

unsigned char readdata[8];

sbit DQ=P3^7;

bit sflag;

bit resetpulse(void)

{

  unsigned char i;

  DQ=0;

  for(i=255;i>0;i--);

  DQ=1;

  for(i=60;i>0;i--);

  return(DQ);

  for(i=200;i>0;i--);

}

void writecommandtods18b20(unsigned char command)

{

  unsigned char i;

  unsigned char j;

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

    {

      if((command & 0x01)==0)

        {

          DQ=0;

          for(j=35;j>0;j--);

          DQ=1;

        }

        else

          {

            DQ=0;

            for(j=2;j>0;j--);

            DQ=1;

            for(j=33;j>0;j--);

          }

      command=_cror_(command,1);     

    }

}

unsigned char readdatafromds18b20(void)

{

  unsigned char i;

  unsigned char j;

  unsigned char temp;

  temp=0;

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

    {

      temp=_cror_(temp,1);

      DQ=0;

      _nop_();

      _nop_();

      DQ=1;

      for(j=10;j>0;j--);

      if(DQ==1)

        {

          temp=temp | 0x80;

        }

        else

          {

            temp=temp | 0x00;

          }

      for(j=200;j>0;j--);

    }

  return(temp);

}

void main(void)

{

  TMOD=0x01;

  TH0=(65536-4000)/256;

  TL0=(65536-4000)%6;

  ET0=1;

  EA=1;

  while(resetpulse());

  writecommandtods18b20(0xcc);

  writecommandtods18b20(0x44);

  TR0=1;

  while(1)

    {

      ;

    }

}

void t0(void) interrupt 1 using 0

{

  unsigned char x;

  unsigned int result;

  TH0=(65536-4000)/256;

  TL0=(65536-4000)%6;

  if(displaycount==2)

    {

      P0=displaycode[displaybuf[displaycount]] | 0x80;

    }

    else

      {

        P0=displaycode[displaybuf[displaycount]];

      }

  P2=displaybit[displaycount];

  displaycount++;

  if(displaycount==8)

    {

      displaycount=0;

    }

  timecount++;

  if(timecount==150)

    {

      timecount=0;

      while(resetpulse());

      writecommandtods18b20(0xcc);

      writecommandtods18b20(0xbe);

      readdata[0]=readdatafromds18b20();

      readdata[1]=readdatafromds18b20();

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

        {

          displaybuf[x]=16;

        }

      sflag=0;

      if((readdata[1] & 0xf8)!=0x00)

        {

          sflag=1;

          readdata[1]=~readdata[1];

          readdata[0]=~readdata[0];

          result=readdata[0]+1;

          readdata[0]=result;

          if(result>255)

            {

              readdata[1]++;

            }

        }

      readdata[1]=readdata[1]<<4;

      readdata[1]=readdata[1] & 0x70;

      x=readdata[0];

      x=x>>4;

      x=x & 0x0f;

      readdata[1]=readdata[1] | x;

      x=2;

      result=readdata[1];

      while(result/10)

        {

          displaybuf[x]=result;

          result=result/10;

          x++;

        }

      displaybuf[x]=result;

      if(sflag==1)

        {

          displaybuf[x+1]=17;

        }

      x=readdata[0] & 0x0f;

      x=x<<1;

      displaybuf[0]=(dotcode[x]);

      displaybuf[1]=(dotcode[x])/10;

      while(resetpulse());

      writecommandtods18b20(0xcc);

      writecommandtods18b20(0x44);

    }

}

關(guān)閉窗口