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
}
}
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讀寫數據都是從最低位讀寫的。
上述文檔是我在完成實習電子萬年歷的過程中,為了徹底理解DS18B20的1-wire總線協議,同時也是總結在這個過程中所遇到的問題而完成的。現在公布出來,目的是方便大家,讓同路人少走彎路。如有任何問題,請留言交流。
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讀寫數據都是從最低位讀寫的。
上述文檔是我在完成實習電子萬年歷的過程中,為了徹底理解DS18B20的1-wire總線協議,同時也是總結在這個過程中所遇到的問題而完成的。現在公布出來,目的是方便大家,讓同路人少走彎路。如有任何問題,請留言交流。