The DS18B20 datasheet sequence fully interprets this article from the network. I wanted to organize it myself, but I can only post the original post due to the time relationship. This article is good. It will teach you how to understand the complicated and difficult Datasheet and hope to help you.
DS18B20 timing
Author: exploer
Ciee, cau:
This document is based on the datasheet of Maxim company DS18B20. The content of the original document is added with a gray box. The English part is the content of the official document and the Chinese part is my translation. At my own level, this document may have errors or misleading content and is not responsible for all the problems caused by this author (exploer, CIEE-CAU.
Initialization procedure-reset and presence pulses
Initialization sequence-reset and existing pulse
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 already strated 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.
All communications between DS18B20 start with an initialization sequence consisting of Reset pulses. This initialization sequence is triggered by the host, followed by the presence pulse sent by DS18B20 ). (Fig. 13, as shown below. When a response reset pulse exists, DS18B20 notifies the host that it is on the bus and is ready for operation.
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 ).
In the initialization step, the host on the bus generates a reset pulse by reducing the single bus by at least 480 μs. Then the bus host releases the bus and enters the receiving mode.
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.
When the bus is released, the upstream Pulling Resistance of 5kΩ pulls the level on the single bus back to the high level. When the DS18B20 detects the rising edge, wait for 15 to 60 us, and then send an existing pulse in the form of pulling down the bus 60 to us.
As described in this document, the host pulls down the bus to a minimum of us, and then releases the bus. The bus is restored to a high level due to the 5 k Ω pull-up resistance. After the DS18B20 detects the rising edge, it waits for 15 to 60 us and generates a pulse: Pull down the bus 60 to us. At this point, the initialization and time series have been completed.
The reset function compiled according to the preceding requirements is:
The first is the latency function: (because the DS18B20 latency is in the unit of 15us, a latency function of 15us is compiled. Note: The following delay function is 12 MHz)
/*
************************************
Function: delayxus_ds18b20
Function: DS18B20 latency Function
Parameter: T indicates the scheduled time length.
Return: None
Description: delay formula: 15-15 (approximate), 12 MHz Crystal Oscillator
**************************************** **
*/
Void delayxus_ds18b20 (unsigned int T)
{
For (T; t> 0; t --)
{
_ NOP _();
}
_ NOP _();
}
Delayed function disassembly code (for convenience of analyzing the delay formula)
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: 0x06ab9400 subb A, #0x00
C: 0x06ad 400b jc c: 06ba
41 :{
42: _ 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 _();
C: 0x06ba 00 NOP
C: 0x06bb 00 NOP
45 :}
C: 0x06bc 22 RET
After analyzing the disassembly code, we can see that the latency formula is 15 * (t + 1)
/*
************************************
Function: rst_ds18b20
Function: reset DS18B20 to read and return a pulse.
Parameter: None
Return Value: 1: reset successful; 0: reset failed
Note: At least us can be used to check whether the DS18B20 works normally.
**************************************** **
*/
Bit rst_ds18b20 ()
{
Bit ret = "1 ";
DQ = 0;/* Pull down the bus */
Delayxus_ds18b20 (32);/* The delay is 495us */
DQ = 1;/* release the bus. After DS18B20 detects the rising edge, a pulse is sent */
Delayxus_ds18b20 (4);/* Wait 15 ~ 60us. After the latency is 75 us, we can ensure that the received pulse exists (if the communication is normal )*/
Ret = DQ;
Delayxus_ds18b20 (14);/* Delay 495us, let the DS18B20 release the bus, avoid affecting the next operation */
DQ = 1;/* release the bus */
Return (~ RET );
}
Write sequence:
Read/write time slots
Read/write time slot
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.
The host writes data to DS18B20 at the write time slot, and reads data from DS18B20 at the read time slot. Each time slot on a single bus transmits only one bit of data.
Write time slots
Write time gap
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 ).
There are two types of write time slots: The write time gap "0" and the write time gap "1. The bus host uses the write "1" time gap to write logic 1 to DS18B20, And the write "0" time gap to write logic 0 to DS18B20. all write time slots must have a minimum duration of 60 us, and the adjacent two write time slots must have a minimum restoration time of 1 US. Both write time slots are generated by lowering the bus on the host (see figure 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
The duration of the time slot (at least 60 μs ).
To generate a write time slot, the host must release the bus within 15 μs after the bus is lowered. After the bus is released, the bus is restored to a high level due to the Pulling Resistance of 5kb. To generate zero time slot, the host must continue to lower the bus to meet the time slot duration (at least 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.
After the host generates a write time slot, the DS18B20 will sample the single bus in a time window between 15 and 60 us. In the sampling time window, if the bus is high, the host will write 1 to DS18B20; if the bus is low, the host will write 0 to DS18B20.
As described in this document, all write time slots must have at least 60 US duration. The adjacent two write time slots must have a restoration time of at least 1 us. All write time slots (write 0 and write 1) are generated by pulling down the bus.
In order to generate 1 time slot, the host must release the bus within 15 us after the bus is lowered (the bus must be at least 1 US ). Due to the pull-up resistance, the bus level is restored to a high level until the write time slot is completed.
In order to generate a write time slot of 0, after the bus is lowered, the host can continue to lower the bus until the write time slot is completed and the bus is released (the duration is 60-US ).
After the write time slot is generated, the DS18B20 will sample the bus within 15 to 60 us after the generation to determine whether to write 0 or 1.
Write Functions that meet the preceding requirements are:
/*
************************************
Function: wr_bit
Function: Write a bit of data to DS18B20.
Parameter: I is the bit to be written.
Return: None
English version (writing sequence generated by bus from high to low)
**************************************** **
*/
Void wr_bit (bit I)
{
DQ = 0; // write Sequence Generation
_ NOP _();
_ NOP _ (); // The bus pulling duration must be greater than 1us.
DQ = I; // write data. values 0 and 1 are acceptable.
Delayxus_ds18b20 (3); // delay of 60 us, waiting for the DS18B20 sampling to read
DQ = 1; // release the bus
}
/*
***********************************
Function: wr_byte
Function: Write the byte function into DS18B20. First, write the second bit.
Parameter: DAT is the byte data to be written.
Return: None
Note: None
**************************************** **
*/
Void wr_byte (unsigned char dat)
{
Unsigned char I = "0 ";
While (I ++ <8)
{
Wr_bit (DAT & 0x01); // write from the cursor bit
Dat> = 1; // do not write it as dat> 1
}
} Read sequence:
Read time slots
Read time gap
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 [44 h] or recall
E 2 [b8h] commands to find out the status of the operation as explained in the DS18B20 function commands section.
DS18B20 sends data to the host only after the host sends the read time slot. Therefore, the host must generate a read Time Slot immediately after the [beh] or the read power Command [b4h] is sent, so that the DS18B20 can provide the required data. In addition, the host can generate read time slots after issuing the temperature conversion command T [44 h] or recall command E 2 [b8h, to understand the operation status (This section describes the operation instructions on DS18B20 in detail ).
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 be 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.
All read time slots must have at least 60 US duration. The two adjacent read time slots must have a restoration time of at least 1 us. All read time slots are produced by lowering the bus and releasing the bus after at least 1 US (the bus is restored to a high level due to the pull-up resistance. After the host generates a read time slot, DS18B20 starts to send 0 or 1 to the bus. DS18B20 allows the bus to send 1 in a high-level manner, and 0 in a low-bus mode. when 0 is sent, the DS18B20 will release the bus at the end of the time slot, and the bus will be pulled back to the high level by the pull-up resistor (which is also the idle state of the Bus ). The data output by DS18B20 is valid after 15 us after the descent edge (the descent edge generates the read time slot) is generated. Therefore, the host release bus and sampling bus must be completed within 15 μs.
Figure 15 into strates that the sum of tinit, TRC, and tsample must be less than 15 μs for a read time slot.
Illustration 15 shows that for the read time slot, the time and duration of tinit (lower-level duration after descent edge), TRC (rising edge), and tsample (host sampling Bus) must be within 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.
Illustration 16 shows how to maximize the system time Grace: To make tinit and TRC as short as possible, put the host sampling bus at the end of the 15 μs period.
According to the document, DS18B20 can only send data to the host when the host sends a read time slot. Therefore, the host must run the be command and the B4 command immediately generates the read time slot so that the DS18B20 can provide the corresponding data. In addition, the read time slot is also generated after the 44 and B8 commands.
All read time slots must have at least 60 US duration. The two adjacent read time slots must have a restoration time of at least 1 us. All read time slots are produced by lowering the bus and releasing the bus after at least 1 US (the bus is restored to a high level due to the pull-up resistance. The data output by DS18B20 is valid after 15 us after the descent edge is generated. Therefore, the release bus and the host sampling bus must be completed within 15 us.
The functions that meet the preceding requirements are:
/*
***********************************
Function: read_bit
Function: read one bit of data from DS18B20.
Parameter: None
Return: bit I
Note: The bus is pulled from high to low and lasts for more than 1 us. Then, the bus is released to generate a read sequence for the high-level idle state.
**************************************** **
*/
Unsigned char read_bit ()
{
Unsigned char ret;
DQ = 0; // pull down the bus
_ NOP _();
DQ = 1; // release the bus
_ NOP _();
_ NOP _();
Ret = DQ; // The read Time Slot generates 7 us and then reads the bus data. The bus read operation is placed behind the 15 us time limit to ensure the validity of data reading.
Delayxus_ds18b20 (3); // The delay is 60 us, which meets the time length requirements of the read time slot.
DQ = 1; // release the bus
Return ret; // return the data read.
}
/*
************************************
Function: read_byte
Function: DS18B20 reads a byte function. It reads the second bit first.
Parameter: None
Return: one byte of data read
Note: None
**************************************** **
*/
Unsigned char read_byte ()
{
Unsigned char I;
Unsigned char dat = "0 ";
For (I = 0; I <8; I ++)
{
Dat> = 1; // read the delimiter first
If (read_bit ())
Dat | = 0x80;
}
Return (DAT );
}
/*
************************************
Function: start_ds18b20
Function: enable temperature conversion.
Parameter: None
Return: None
Note: Write the 44h command after resetting.
**************************************** **
*/
Void start_ds18b20 ()
{
DQ = 1;
Rst_ds18b20 ();
Wr_byte (0xcc); // skip
Wr_byte (0x44); // start temperature conversion
}
/*
************************************
Function: read_tem
Function: Read Temperature
Parameter: None
Return Value: int type temperature data. The temperature data of 8-bit high and 8-bit high is the temperature data of 8-bit low.
Note: Write the be command after resetting.
**************************************** **
*/
Int read_tem ()
{
Int TEM = "0 ";
Rst_ds18b20 ();
Wr_byte (0xcc); // skip
Wr_byte (0xbe); // sends a READ command
TEM = read_byte (); // eight-bit low read Temperature
TEM | = (INT) read_byte () <8); // reads an eight-bit high temperature.
Return TEM;
}
Note: The order of reading and writing data bits is not described in the DS18B20 Official Document. After checking the information, the DS18B20 reads and writes data from the second bit.
The above documents are completed in order to thoroughly understand the 1-wire bus protocol of DS18B20 and summarize the problems encountered in this process when I completed the practice of electronic Perpetual calendar. The purpose of the announcement is to make it easier for everyone to avoid detours. If you have any questions, please send an email to your brave.exploer@gmail.com.
According to the document, DS18B20 can only send data to the host when the host sends a read time slot. Therefore, the host must run the be command and the B4 command immediately generates the read time slot so that the DS18B20 can provide the corresponding data. In addition, the read time slot is also generated after the 44 and B8 commands.
All read time slots must have at least 60 US duration. The two adjacent read time slots must have a restoration time of at least 1 us. All read time slots are produced by lowering the bus and releasing the bus after at least 1 US (the bus is restored to a high level due to the pull-up resistance. The data output by DS18B20 is valid after 15 us after the descent edge is generated. Therefore, the release bus and the host sampling bus must be completed within 15 us.
The functions that meet the preceding requirements are:
/*
***********************************
Function: read_bit
Function: read one bit of data from DS18B20.
Parameter: None
Return: bit I
Note: The bus is pulled from high to low and lasts for more than 1 us. Then, the bus is released to generate a read sequence for the high-level idle state.
**************************************** **
*/
Unsigned char read_bit ()
{
Unsigned char ret;
DQ = 0; // pull down the bus
_ NOP _();
DQ = 1; // release the bus
_ NOP _();
_ NOP _();
Ret = DQ; // The read Time Slot generates 7 us and then reads the bus data. The bus read operation is placed behind the 15 us time limit to ensure the validity of data reading.
Delayxus_ds18b20 (3); // The delay is 60 us, which meets the time length requirements of the read time slot.
DQ = 1; // release the bus
Return ret; // return the data read.
}
/*
************************************
Function: read_byte
Function: DS18B20 reads a byte function. It reads the second bit first.
Parameter: None
Return: one byte of data read
Note: None
**************************************** **
*/
Unsigned char read_byte ()
{
Unsigned char I;
Unsigned char dat = "0 ";
For (I = 0; I <8; I ++)
{
Dat> = 1; // read the delimiter first
If (read_bit ())
Dat | = 0x80;
}
Return (DAT );
}
/*
************************************
Function: start_ds18b20
Function: enable temperature conversion.
Parameter: None
Return: None
Note: Write the 44h command after resetting.
**************************************** **
*/
Void start_ds18b20 ()
{
DQ = 1;
Rst_ds18b20 ();
Wr_byte (0xcc); // skip
Wr_byte (0x44); // start temperature conversion
}
/*
************************************
Function: read_tem
Function: Read Temperature
Parameter: None
Return Value: int type temperature data. The temperature data of 8-bit high and 8-bit high is the temperature data of 8-bit low.
Note: Write the be command after resetting.
**************************************** **
*/
Int read_tem ()
{
Int TEM = "0 ";
Rst_ds18b20 ();
Wr_byte (0xcc); // skip
Wr_byte (0xbe); // sends a READ command
TEM = read_byte (); // eight-bit low read Temperature
TEM | = (INT) read_byte () <8); // reads an eight-bit high temperature.
Return TEM;
}
Note: The order of reading and writing data bits is not described in the DS18B20 Official Document. After checking the information, the DS18B20 reads and writes data from the second bit.