天天看點

DS18B20 Datasheet時序完全解讀

DS18B20 Datasheet時序完全解讀

本文章轉自網絡,本來想自己整理 但由于時間關系隻能貼上原帖,這篇文章不錯,教會大家如何看懂複雜難懂的datasheet 希望對大家有幫助

DS18B20時序

Author:exploer   

CIEE ,CAU  2008-9-16:

    本文檔參照Maxim公司DS18B20的Datasheet而完成。其中,加了灰框的部分為原文檔内容,其中英文部分為官方文檔内容,中文部分為本人的翻譯。限于本人水準,本文檔可能存在錯誤或者讓人誤解的内容,對于是以引起的一切問題作者(exploer,CIEE-CAU)概不負責。

INITIALIZATION PROCEDURE—RESET AND PRESENCE PULSES

初始化序列——複位和存在脈沖

    All communication with the DS18B20 begins with an initialization sequence that consists of a reset pulse from the master followed by a presence pulse from the DS18B20. This is illustrated in Figure 13. When the DS18B20 sends the presence pulse in response to the reset, it is indicating to the master that it is on the bus and ready to operate.

    DS18B20的所有通信都由由複位脈沖組成的初始化序列開始。該初始化序列由主機發出,後跟由DS18B20發出的存在脈沖(presence pulse)。下圖(插圖13,即如下截圖)闡述了這一點。當發出應答複位脈沖的存在脈沖後,DS18B20通知主機它在總線上并且準備好操作了。 

DS18B20 Datasheet時序完全解讀

  During the initialization sequence the bus master transmits (TX) the reset pulse by pulling the 1-Wire bus low for a minimum of 480μs. The bus master then releases the bus and goes into receive mode (RX).

    在初始化步驟中,總線上的主機通過拉低單總線至少480μs來産生複位脈沖。然後總線主機釋放總線并進入接收模式。

    When the bus is released, the 5kΩ pullup resistor pulls the 1-Wire bus high. When the DS18B20 detects this rising edge, it waits 15μs to 60μs and then transmits a presence pulse by pulling the 1-Wire bus low for 60μs to 240μs.

    當總線釋放後,5kΩ的上拉電阻把單總線上的電平拉回高電平。當DS18B20檢測到上升沿後等待15到60us,然後以拉低總線60-240us的方式發出存在脈沖。

    如文檔所述,主機将總線拉低最短480us,之後釋放總線。由于5kΩ上拉電阻的作用,總線恢複到高電平。DS18B20檢測到上升沿後等待15到60us,發出存在脈沖:拉低總線60-240us。至此,初始化和存在時序完畢。

    根據上述要求編寫的複位函數為: 

    首先是延時函數:(由于DS18B20延時均以15us為機關,故編寫了延時機關為15us的延時函數,注意:以下延時函數晶振為12MHz)

    /*

    ************************************

    函數:Delayxus_DS18B20

    功能:DS18B20延時函數

    參數:t為定時時間長度

    傳回:無

    說明: 延時公式:15n+15(近似),晶振12Mhz

    ******************************************

    */

    void Delayxus_DS18B20(unsigned int t)

    {

          for(t;t>0;t--)

          {

                 _nop_();_nop_();_nop_();_nop_();

          }     

          _nop_(); _nop_();

    }

    延時函數反彙編代碼(友善分析延時公式)

    C:0x0031   7F01    MOV     R7,#0x01

    C:0x0033   7E00    MOV     R6,#0x00

    C:0x0035   1206A6  LCALL   delayxus(C:06A6)

       38: void Delayxus_DS18B20(unsigned int t)

       39: {

       40:        for(t;t>0;t--)

    C:0x06A6   D3      SETB    C

    C:0x06A7   EF      MOV     A,R7

    C:0x06A8   9400    SUBB    A,#0x00

    C:0x06AA   EE      MOV     A,R6

    C:0x06AB   9400    SUBB    A,#0x00

    C:0x06AD   400B    JC      C:06BA

       41:        {

       42:                _nop_();_nop_();_nop_();_nop_();

    C:0x06AF   00      NOP     

    C:0x06B0   00      NOP     

    C:0x06B1   00      NOP     

    C:0x06B2   00      NOP     

       43:        }       

    C:0x06B3   EF      MOV     A,R7

    C:0x06B4   1F      DEC     R7

    C:0x06B5   70EF    JNZ     Delayxus_DS18B20 (C:06A6)

    C:0x06B7   1E      DEC     R6

    C:0x06B8   80EC    SJMP    Delayxus_DS18B20 (C:06A6)

       44:        _nop_(); _nop_();

    C:0x06BA   00      NOP     

    C:0x06BB   00      NOP     

       45: }

    C:0x06BC   22      RET     

    分析上述反彙編代碼,可知延時公式為15*(t+1)

    /*

    ************************************

    函數:RST_DS18B20

    功能:複位DS18B20,讀取存在脈沖并傳回

    參數:無

    傳回:1:複位成功 ;0:複位失敗

    說明: 拉低總線至少480us ;可用于檢測DS18B20工作是否正常

    ******************************************

    */

    bit RST_DS18B20()

    {            

          bit ret="1";

          DQ=0;/*拉低總線 */

          Delayxus_DS18B20(32);/*為保險起見,延時495us */

          DQ=1;/*釋放總線 ,DS18B20檢測到上升沿後會發送存在脈沖*/

          Delayxus_DS18B20(4);/*需要等待15~60us,這裡延時75us後可以保證接受到的是存在脈沖(如果通信正常的話) */

          ret=DQ;

          Delayxus_DS18B20(14);/*延時495us,讓ds18b20釋放總線,避免影響到下一步的操作 */

       DQ=1;/*釋放總線 */

          return(~ret);

    }

    寫時序:

    READ/WRITE TIME SLOTS

    讀寫時隙

    The bus master writes data to the DS18B20 during write time slots and reads data from the DS18B20 during read time slots. One bit of data is transmitted over the 1-Wire bus per time slot.

     主機在寫時隙向DS18B20寫入資料,并在讀時隙從DS18B20讀入資料。在單總線上每個時隙隻傳送一位資料。

    WRITE TIME SLOTS

    寫時間隙

    There are two types of write time slots: “Write 1” time slots and “Write 0” time slots. The bus master uses a Write 1 time slot to write a logic 1 to the DS18B20 and a Write 0 time slot to write a logic 0 to the DS18B20. All write time slots must be a minimum of 60μs in duration with a minimum of a 1μs recovery

time between individual write slots. Both types of write time slots are initiated by the master pulling the 1-Wire bus low (see Figure 14).

     有兩種寫時隙:寫“0”時間隙和寫“1”時間隙。總線主機使用寫“1”時間隙向DS18B20寫入邏輯1,使用寫“0”時間隙向DS18B20寫入邏輯0.所有的寫時隙必須有最少60us的持續時間,相鄰兩個寫時隙必須要有最少1us的恢複時間。兩種寫時隙都通過主機拉低總線産生(見插圖14)。 

DS18B20 Datasheet時序完全解讀

To generate a Write 1 time slot, after pulling the 1-Wire bus low, the bus master must release the 1-Wire bus within 15μs. When the bus is released, the 5kΩ pullup resistor will pull the bus high. To generate a Write 0 time slot, after pulling the 1-Wire bus low, the bus master must continue to hold the bus low for

the duration of the time slot (at least 60μs).

         為産生寫1時隙,在拉低總線後主機必須在15μs内釋放總線。在總線被釋放後,由于5kΩ上拉電阻的作用,總線恢複為高電平。為産生寫0時隙,在拉低總線後主機必須繼續拉低總線以滿足時隙持續時間的要求(至少60μs)。

    The DS18B20 samples the 1-Wire bus during a window that lasts from 15μs to 60μs after the master initiates the write time slot. If the bus is high during the sampling window, a 1 is written to the DS18B20. If the line is low, a 0 is written to the DS18B20.

    在主機産生寫時隙後,DS18B20會在其後的15到60us的一個時間視窗内采樣單總線。在采樣的時間視窗内,如果總線為高電平,主機會向DS18B20寫入1;如果總線為低電平,主機會向DS18B20寫入0。

    如文檔所述,所有的寫時隙必須至少有60us的持續時間。相鄰兩個寫時隙必須要有最少1us的恢複時間。所有的寫時隙(寫0和寫1)都由拉低總線産生。

    為産生寫1時隙,在拉低總線後主機必須在15us内釋放總線(拉低的電平要持續至少1us)。由于上拉電阻的作用,總線電平恢複為高電平,直到完成寫時隙。

    為産生寫0時隙,在拉低總線後主機持續拉低總線即可,直到寫時隙完成後釋放總線(持續時間60-120us)。

    寫時隙産生後,DS18B20會在産生後的15到60us的時間内采樣總線,以此來确定寫0還是寫1。

    滿足上述要求的寫函數為:

    /*

    ************************************

    函數:WR_Bit

    功能:向DS18B20寫一位資料

    參數:i為待寫的位

    傳回:無

    說明: 總線從高拉到低産生寫時序

    ******************************************

    */

    void WR_Bit(bit i)

    {

          DQ=0;//産生寫時序 

          _nop_();

          _nop_();//總線拉低持續時間要大于1us

          DQ=i;//寫資料 ,0和1均可

          Delayxus_DS18B20(3);//延時60us,等待ds18b20采樣讀取 

          DQ=1;//釋放總線 

    }

    /*

    ***********************************

    函數:WR_Byte

    功能:DS18B20寫位元組函數,先寫最低位

    參數:dat為待寫的位元組資料

    傳回:無

    說明:無

    ******************************************

    */

    void WR_Byte(unsigned char dat)

    {

          unsigned char i="0";

          while(i++<8)

          {

                 WR_Bit(dat&0x01);//從最低位寫起 

                 dat>>=1; //注意不要寫成dat>>1

          }

    }

讀時序:

    READ TIME SLOTS

    讀時間隙

    The DS18B20 can only transmit data to the master when the master issues read time slots. Therefore, the master must generate read time slots immediately after issuing a Read Scratchpad [BEh] or Read Power Supply [B4h] command, so that the DS18B20 can provide the requested data. In addition, the master can generate read time slots after issuing Convert T [44h] or Recall   

E 2[B8h] commands to find out the status of the operation as explained in the DS18B20 Function Commands section.

     DS18B20隻有在主機發出讀時隙後才會向主機發送資料。是以,在發出讀暫存器指令 [BEh]或讀電源指令[B4h]後,主機必須立即産生讀時隙以便DS18B20提供所需資料。另外,主機可在發出溫度轉換指令T [44h]或Recall指令E 2[B8h]後産生讀時隙,以便了解操作的狀态(在 DS18B20操作指令這一節會詳細解釋)。

    All read time slots must be a minimum of 60μs in duration with a minimum of a 1μs recovery time between slots. A read time slot is initiated by the master device pulling the 1-Wire bus low for a minimum of 1μs and then releasing the bus (see Figure 14). After the master initiates the read time slot, the DS18B20 will begin transmitting a 1 or 0 on bus. The DS18B20 transmits a 1 by leaving the bus high and transmits a 0 by pulling the bus low. When transmitting a 0, the DS18B20 will release the bus by the end of the time slot, and the bus will be pulled back to its high idle state by the pullup resister. Output data from the DS18B20 is valid for 15μs after the falling edge that initiated the read time slot. Therefore, the master must release the bus and then sample the bus state within 15μs from the start of the slot.

    所有的讀時隙必須至少有60us的持續時間。相鄰兩個讀時隙必須要有最少1us的恢複時間。所有的讀時隙都由拉低總線,持續至少1us後再釋放總線(由于上拉電阻的作用,總線恢複為高電平)産生。在主機産生讀時隙後,DS18B20開始發送0或1到總線上。DS18B20讓總線保持高電平的方式發送1,以拉低總線的方式表示發送0.當發送0的時候,DS18B20在讀時隙的末期将會釋放總線,總線将會被上拉電阻拉回高電平(也是總線空閑的狀态)。DS18B20輸出的資料在下降沿(下降沿産生讀時隙)産生後15us後有效。是以,主機釋放總線和采樣總線等動作要在15μs内完成。

    Figure 15 illustrates that the sum of TINIT, TRC, and TSAMPLE must be less than 15μs for a read time slot.

    插圖15表明了對于讀時隙,TINIT(下降沿後低電平持續時間), TRC(上升沿)和TSAMPLE(主機采樣總線)的時間和要在15μs以内。

    Figure 16 shows that system timing margin is maximized by keeping TINIT and TRC as short as possible and by locating the master sample time during read time slots towards the end of the 15μs period.

    插圖16顯示了最大化系統時間寬限的方法:讓TINIT 和TRC盡可能的短,把主機采樣總線放到15μs這一時間段的尾部。

DS18B20 Datasheet時序完全解讀

    由文檔可知,DS18B20隻有在主機發出讀時隙時才能發送資料到主機。是以,主機必須在BE指令,B4指令後立即産生讀時隙以使DS18B20提供相應的資料。另外,在44指令,B8指令後也要産生讀時隙。

    所有的讀時隙必須至少有60us的持續時間。相鄰兩個讀時隙必須要有最少1us的恢複時間。所有的讀時隙都由拉低總線,持續至少1us後再釋放總線(由于上拉電阻的作用,總線恢複為高電平)産生。DS18B20輸出的資料在下降沿産生後15us後有效。是以,釋放總線和主機采樣總線等動作要在15us内完成。

    滿足以上要求的函數為:

    /*

    ***********************************

    函數:Read_Bit

    功能:向DS18B20讀一位資料

    參數:無

    傳回:bit i

    說明: 總線從高拉到低,持續至1us以上,再釋放總線為高電平空閑狀态産生讀時序

    ******************************************

    */

    unsigned char Read_Bit()

    {

          unsigned char ret;

          DQ=0;//拉低總線

          _nop_(); _nop_();   

          DQ=1;//釋放總線       

          _nop_(); _nop_();

          _nop_(); _nop_();

          ret=DQ;//讀時隙産生7 us後讀取總線資料。把總線的讀取動作放在15us時間限制的後面是為了保證資料讀取的有效性

          Delayxus_DS18B20(3);//延時60us,滿足讀時隙的時間長度要求 

       DQ=1;//釋放總線 

          return ret; //傳回讀取到的資料 

    }

    /*

    ************************************

    函數:Read_Byte

    功能:DS18B20讀一個位元組函數,先讀最低位

    參數:無

    傳回:讀取的一位元組資料

    說明: 無

    ******************************************

    */

     unsigned char Read_Byte()

    {

          unsigned char i;

          unsigned char dat="0";

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

          {

          dat>>=1;//先讀最低位 

          if(Read_Bit())

                 dat|=0x80;

          }

          return(dat);

    }

    /*

    ************************************

    函數:Start_DS18B20

    功能:啟動溫度轉換

    參數:無

    傳回:無

    說明: 複位後寫44H指令

    ******************************************

    */

    void Start_DS18B20()

    {

          DQ=1;

          RST_DS18B20();

          WR_Byte(0xcc);// skip

          WR_Byte(0x44);//啟動溫度轉換

    }

    /*

    ************************************

    函數:Read_Tem

    功能:讀取溫度

    參數:無

    傳回:int型溫度資料,高八位為高八位溫度資料,低八位為低八位溫度資料

    說明: 複位後寫BE指令

    ******************************************

    */

    int Read_Tem()

    {

          int tem="0";

          RST_DS18B20();

          WR_Byte(0xcc);// skip

          WR_Byte(0xbe);//發出讀取指令

          tem=Read_Byte();//讀出溫度低八位

          tem|=(((int)Read_Byte())<<8);//讀出溫度高八位

          return tem;

    }

    注: DS18B20官方文檔中沒有說明讀寫資料位的順序,查了下資料,DS18B20讀寫資料都是從最低位讀寫的。

    上述文檔是我在完成實習電子萬年曆的過程中,為了徹底了解DS18B20的1-wire總線協定,同時也是總結在這個過程中所遇到的問題而完成的。現在公布出來,目的是友善大家,讓同路人少走彎路。如有任何問題,請發郵件到 [email protected] 。 

    由文檔可知,DS18B20隻有在主機發出讀時隙時才能發送資料到主機。是以,主機必須在BE指令,B4指令後立即産生讀時隙以使DS18B20提供相應的資料。另外,在44指令,B8指令後也要産生讀時隙。

    所有的讀時隙必須至少有60us的持續時間。相鄰兩個讀時隙必須要有最少1us的恢複時間。所有的讀時隙都由拉低總線,持續至少1us後再釋放總線(由于上拉電阻的作用,總線恢複為高電平)産生。DS18B20輸出的資料在下降沿産生後15us後有效。是以,釋放總線和主機采樣總線等動作要在15us内完成。

    滿足以上要求的函數為:

    /*

    ***********************************

    函數:Read_Bit

    功能:向DS18B20讀一位資料

    參數:無

    傳回:bit i

    說明: 總線從高拉到低,持續至1us以上,再釋放總線為高電平空閑狀态産生讀時序

    ******************************************

    */

    unsigned char Read_Bit()

    {

          unsigned char ret;

          DQ=0;//拉低總線

          _nop_(); _nop_();   

          DQ=1;//釋放總線       

          _nop_(); _nop_();

          _nop_(); _nop_();

          ret=DQ;//讀時隙産生7 us後讀取總線資料。把總線的讀取動作放在15us時間限制的後面是為了保證資料讀取的有效性

          Delayxus_DS18B20(3);//延時60us,滿足讀時隙的時間長度要求 

       DQ=1;//釋放總線 

          return ret; //傳回讀取到的資料 

    }

    /*

    ************************************

    函數:Read_Byte

    功能:DS18B20讀一個位元組函數,先讀最低位

    參數:無

    傳回:讀取的一位元組資料

    說明: 無

    ******************************************

    */

     unsigned char Read_Byte()

    {

          unsigned char i;

          unsigned char dat="0";

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

          {

          dat>>=1;//先讀最低位 

          if(Read_Bit())

                 dat|=0x80;

          }

          return(dat);

    }

    /*

    ************************************

    函數:Start_DS18B20

    功能:啟動溫度轉換

    參數:無

    傳回:無

    說明: 複位後寫44H指令

    ******************************************

    */

    void Start_DS18B20()

    {

          DQ=1;

          RST_DS18B20();

          WR_Byte(0xcc);// skip

          WR_Byte(0x44);//啟動溫度轉換

    }

    /*

    ************************************

    函數:Read_Tem

    功能:讀取溫度

    參數:無

    傳回:int型溫度資料,高八位為高八位溫度資料,低八位為低八位溫度資料

    說明: 複位後寫BE指令

    ******************************************

    */

    int Read_Tem()

    {

          int tem="0";

          RST_DS18B20();

          WR_Byte(0xcc);// skip

          WR_Byte(0xbe);//發出讀取指令

          tem=Read_Byte();//讀出溫度低八位

          tem|=(((int)Read_Byte())<<8);//讀出溫度高八位

          return tem;

    }

    注: DS18B20官方文檔中沒有說明讀寫資料位的順序,查了下資料,DS18B20讀寫資料都是從最低位讀寫的。

    上述文檔是我在完成實習電子萬年曆的過程中,為了徹底了解DS18B20的1-wire總線協定,同時也是總結在這個過程中所遇到的問題而完成的。現在公布出來,目的是友善大家,讓同路人少走彎路。如有任何問題,請留言交流。