1-wire protocol
As a single-host multi-slave bus system, the number of slave devices mounted on a 1-wire bus is almost unrestricted. For non-logical conflicts, all the 1-wire bus interfaces of the slave device are open-circuit, therefore, when using the bus, you must add a pull resistance (usually around 5kb Ω ). The basic operations of a host on the 1-wire bus are divided into three types: reset, read, and write. All the read and write operations are performed after the previous high. Resetting, reading, and writing are the basis of 1-wire bus communication. The following describes the timing requirements of these three operations through specific programs. (In the program, DQ represents the 1-wire bus, defined as p1.0, and uchar is defined as unsigned.
Char)
1-reset the wire bus
Reset is the most important operation in 1-wire bus communication. The host must send a reset signal before each bus communication. As shown in program 1.1, when a reset signal is generated, the host first pulls down the bus by 480 ~ 960 μs is then released. Because of the existence of the pull-up resistor, the bus becomes high. 1-The wire bus device receives 15 ~ Within 60 μs, the bus will be pulled down by 60 ~ 240 μs. During this period, the host can use DQ sampling to determine if any slave device is mounted to the current bus. The Return Value of the function reset () is 0, indicating that a device is mounted to the bus. The return value is 1, indicating that no device is mounted to the bus.
Program 1.1 bus Reset
Uchar reset (void)
{
Uchar tdq;
DQ = 0; // The host pulls down the bus.
Delay480 μs (); // wait 480 μs
DQ = 1; // host releases the bus
Delay60 μs (); // wait 60 μs
Tdq = DQ; // The host samples the bus
Delay480 μs (); // wait until the reset ends.
Return tdq; // return the sample value.
}
1-write operations on the wire bus
Because there is only one I/O line, write operations on the host 1-wire bus can only be performed in one byte, and can be written to the bus eight times in a row. As shown in the program 1.2, when the MCS-51 microcontroller clock frequency is 12 MHz, the Program Statement _ NOP _ (); can produce 1 μs delay, when calling this function, the header file "intrins. H ". Writing 1 bit to 1-Wire Bus requires at least 60 μs, and two consecutive write operations must have an interval of more than 1 μs. If the wbit to be written is 0, the host pulls down the bus 60 μs and then releases it. The write 0 operation is complete. If the wbit to be written is 1, the host pulls down the bus and ~ Release within 15 μs, wait for 60 μs, and write 1.
Program 1.2 writes 1 bit to the bus
Void writebit (uchar wbit)
{
_ NOP _();
// Ensure the interval between two write operations is greater than 1 μs
DQ = 0;
_ NOP _();
// Ensure that the host pulls down the bus by more than 1 μs
If (wbit)
{
// Write 1 to the bus
DQ = 1;
Delay60 μs ();
}
Else
{
// Write 0 to the bus
Delay60 μs ();
DQ = 0;
}
}
1-read operations on the wire bus
Similar to write operations, the host can only perform read operations on the 1-Wire Bus in one byte. The host can read one byte after eight consecutive reads. Reading 1 bit from 1-wire bus also requires at least 60 μs, and at least two consecutive read operations must be performed at an interval of more than 1 μs. As shown in program 1.3, when reading data from the bus, the host first lowers the bus by more than 1 μs and then releases it ~ The sampling value of the host to the bus within 15 μs is the data read.
Program 1.3 reads 1 bit from the bus
Uchar readbit ()
{
Uchar tdq;
_ NOP _();
// Ensure the interval between two consecutive write operations is greater than 1 μs
DQ = 0;
_ NOP _();
// Ensure that the bus is not less than 1 μs.
DQ = 1;
_ NOP _();
Tdq = DQ;
// The host samples the bus
Delay60 μs ();
// Wait until the read operation ends
Return tdq;
// Return the read data
}
1-Wire Bus Rom FUNCTION command
A 64-bit ROM code is printed on the DS18B20, which is a device identification identifier. When multiple DS18B20 are mounted on the bus, a specific device can be operated through Rom encoding. The Rom FUNCTION command is used to perform operations on the ROM code of the device. There are five Rom function commands with a length of 8 bits (1 byte ).
① Read Rom (33 H)
When the 1-wire bus device mounted to the BUS receives this command, the Rom encoding will be sent to the host in sequence from low to high with the help of Host read operations. When there are multiple DS18B20 mounted on the bus, this command will make all devices send their own ROM code to the host at the same time, which will cause data conflict.
② Match Rom (55 H)
After the host sends this command, it must send a 64-bit ROM code immediately. The slave device that matches this ROM code will respond to the subsequent commands of the host, while other slave devices will be in the waiting status. This command is mainly used to select a specific device on the bus for access.
③ Skip Rom (CCH)
After sending this command, the host can access the slave device without providing Rom encoding. Similar to the read Rom command, this command is only applicable to single-node 1-wire bus systems. When multiple devices are mounted on the bus, data conflicts may occur.
④ Find Rom (f0h)
When the host does not know the ROM code of the device on the bus, you can use this command and use a specific algorithm to find out the number of devices on the bus and the ROM code of each slave device.
⑤ Alarm search (ECH)
This command is used to find the DS18B20 that meets the alarm conditions on the bus. It uses the alarm SEARCH Command and works with a specific search algorithm, you can find the number of devices that meet the alarm conditions on the bus and the ROM code of each device.