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通知主機它在匯流排上並且準備好操作了。
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)。
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隻有在主機發出讀時隙時才能發送資料到主機。因此,主機必須在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匯流排協議,同時也是總結在這個過程中所遇到的問題而完成的。現在公布出來,目的是方便大家,讓同路人少走彎路。如有任何問題,請發郵件到 brave.exploer@gmail.com 。
由文檔可知,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讀寫資料都是從最低位讀寫的。