Application of Single-Line DS18B20 Digital Temperature Sensor
Features of DS18B20:
DS18B20 single-wire digital temperature sensor, a "first-line device", has unique advantages:
(1) When connecting to the microprocessor using a single bus interface, only one port line is needed to achieve two-way communication between the microprocessor and DS18B20. The single bus has the advantages of good economy, strong anti-interference ability, suitable for on-site temperature measurement in harsh environments, and easy to use. It allows users to easily build sensor networks and introduces new concepts to the construction of measurement systems.
(2) Wide measurement temperature range: high measurement accuracy. The measurement range of DS18B20 is -55 ℃ ~+ 125 ℃; within the range of -10~+ 85°C, the accuracy is ± 0.5°C.
(3) No external components are required during use.
(4) Support multi-point networking function: Multiple DS18B20 can be connected in parallel on a single single line to achieve multi-point temperature measurement.
(5) Flexible power supply method: DS18B20 can obtain power from the data line through internal parasitic circuits. Therefore, when the timing on the data line meets certain requirements, no external power supply can be connected, thus making the system structure simpler and more reliable.
(6) Measurement parameters are configurable: The measurement resolution of DS18B20 can be set to 9~12 bits through the program.
(7) Negative voltage characteristics: When the polarity of the power supply is reversed, the thermometer will not burn due to heat, but it will not work properly.
(8) Power-off protection function: DS18B20 contains an EEPROM inside, which can still save the resolution and alarm temperature settings after the system is powered off.
DS18B20 has smaller size, wider applicable voltage, more economical, optional smaller package, and wider voltage applicable range. It is suitable for building your own economical temperature measurement system, so it is favored by designers.
DS18B20 internal structure:
Mainly composed of 4 parts: 64-bit ROM, temperature sensor, non-volatile temperature alarm trigger TH and TL, configuration register. The 64-bit serial number in the ROM is photo-engraved before leaving the factory. It can be regarded as the address serial number of the DS18B20. The 64-bit serial number of each DS18B20 is different. The cyclic redundancy check code (CRC=X^8+X^5+X^4+1) of the 64-bit ROM. The function of the ROM is to make each DS18B20 different, so that multiple DS18B20s can be connected to one bus.
DS18B20 pin arrangement:
1. GND is the power ground;
2. DQ is the digital signal input/output terminal;
3. VDD is the input terminal of the external power supply (grounded in the parasitic power wiring mode)
DS18B20 internal composition:
The scratchpad memory consists of 9 bytes. When the temperature conversion command is issued, the converted temperature value is stored in the 0th and 1st bytes of the cache memory in two-byte complement form. The microcontroller can read this data through the single-wire interface. When reading, the low bit is in front and the high bit is in the back. Corresponding temperature calculation: When the sign bit S=0, directly convert the binary bits to decimal. When S=1, first change the complement code to the original code, and then calculate the decimal value.
The lower eight bits of temperature data 0
High 8-bit data of temperature 1
High temperature threshold 2
Low temperature threshold 3
Reserved 4
Reserved 5
Count remaining value 6
Counts per degree 7
CRC check 8
The temperature sensor in DS18B20 completes the measurement of temperature and provides it in 16-bit binary format, where S is the sign bit.
For example:
+125℃ digital output 07D0H
(Positive temperature directly converts the hexadecimal number into decimal to obtain the temperature value)
-55℃ digital output is FC90H.
(For negative temperature, invert the obtained hexadecimal number, add 1, and then convert it into a decimal number)
Working sequence of DS18B20:
Initialization timing
The host first sends a low-level pulse of 480-960 microseconds, then releases the bus to high level, and detects the bus within the subsequent 480 microseconds. If a low level appears, it means that a device on the bus has responded. If there is no low level, it is always high level, indicating that there is no device response on the bus.
As a slave device, DS18B20 has been detecting whether there is a low level of 480-960 microseconds on the bus as soon as it is powered on. If so, wait 15-60 microseconds after the bus turns high, then pull the bus level low for 60-240 microseconds to respond with a pulse, telling the host that the device is ready. If it is not detected, it will keep checking and waiting.
write operation
The write cycle is a minimum of 60 microseconds and a maximum of 120 microseconds. At the beginning of the write cycle, the master first pulls the bus low for 1 microsecond to indicate the start of the write cycle. Subsequently, if the host wants to write 0, it will continue to pull the low level for at least 60 microseconds until the end of the write cycle, and then release the bus to a high level. If the host wants to write 1, it will initially pull the bus level low for 1 microsecond and then release the bus level to high level until the end of the write cycle. As a slave, DS18B20 waits for 15 microseconds after detecting that the bus is pulled down and then starts sampling the bus from 15us to 45us. It is 1 if the bus is high level during the sampling period, and 0 if the bus is low level during the sampling period.
Read operation
The read data operation timing is also divided into two processes: read 0 timing and read 1 timing. The read time slot is after the host pulls the single bus low, and then releases the single bus to high level after 1 microsecond to allow DS18B20 to transmit data to the single bus. DS18B20 starts sending data after detecting that the bus is pulled low for 1 microsecond. If it wants to send 0, it pulls the bus low until the end of the read cycle. If you want to send 1, release the bus to high level. The host initially pulls the bus low for 1 microsecond and then releases the bus, and then completes the sampling and detection of the bus within 15 microseconds including the previous 1 microsecond of pulling the bus level low. During the sampling period, the bus is low level. It is confirmed to be 0. If the bus is high during the sampling period, it is confirmed as 1. It takes at least 60us to complete a read timing process.
DS18B20 single wire communication:
The single-line communication function of DS18B20 is completed in time sharing, and it has a strict time slot concept. If a sequence disorder occurs, the 1-WIRE device will not respond to the host, so read and write timing is important. Various operations of the system on DS18B20 must be performed according to the protocol. According to the DS18B20 protocol, the microcontroller controls the DS18B20 to complete temperature conversion through the following three steps:
(1) Reset and initialize DS18B20 before each read and write. Reset requires the main CPU to pull the data line down for 500us and then release it. After receiving the signal, DS18B20 waits for about 16us~60us, and then sends out a low pulse of 60us~240us. After the main CPU receives this signal, it indicates that the reset is successful.
(2) Send a ROM command
(3) Send memory command
Specific operation examples:
Now what we have to do is to let DS18B20 perform a temperature conversion. The specific operation is:
1. The host performs a reset operation first.
2. The host then writes the ROM skip operation (CCH) command.
3. Then the host writes an operation command to convert the temperature, and then releases the bus for at least one second to allow the DS18B20 to complete the conversion operation. What should be noted here is that when writing each command byte, the low byte is written first. For example, the binary number of CCH is 11001100. When writing to the bus, it should be written from the low bit. The writing order is "zero, zero, one, one, zero, zero, one, one". The bus status of the entire operation is as shown below.
Read the temperature data in RAM. Similarly, this operation also requires three steps.
1. The host issues a reset operation and receives the response (existence) pulse of DS18B20.
2. The host issues a command (CCH) to skip the ROM operation.
3. The host issues a read RAM command (BEH), and then the host reads nine bytes of data from 0 to 8 sent by DS18B20 in sequence. If you only want to read the temperature data, just ignore the subsequent data sent by DS18B20 after reading the 0th and 1st data. Similarly, reading data is also low-order first. The bus status of the entire operation is as follows:
C language code:
sbit DQ=P3^3;
uchar t; //Set global variables, specifically used for delay programs
bit Init_DS18B20(void)
{
bit flag;
DQ=1;
_nop_(); //??????????????? for(t=0;t<2;t++);
DQ=0;
for(t=0;t<200;t++);
DQ=1;
for(t=0;t<15;t++);//???????????????? for(t=0;t<10;t++);
flag=DQ;
for(t=0;t<200;t++);
return flag;
}
uchar ReadOneChar(void)
{
uchar i=0;
uchar dat;
for(i=0;i<8;i++)
{
DQ=1;
_nop_();
DQ=0;
_nop_();
DQ=1; //Artificially pulled high to prepare for the microcontroller to detect the output level of DS18B20
for(t=0;t<3;t++);//Delay month 9us??????????????????????????? for(t=0; t<2;t++);
dat>>=1;
if(DQ==1)
{
dat|=0x80;
}
else
dat|=0x00;
for(t=0;t<1;t++);//Delay 3us, at least 1us recovery period is required between two read sequences??????????for(t=0;t<8 ;t++);
}
return dat;
}
void WriteOneChar(uchar dat)
{
uchar i=0;
for(i=0;i<8;i++)
{
DQ=1;
_nop_();
DQ=0;
_nop_();// ?????????????????????????????????????????? ??
DQ=dat&0x01;
for(t=0;t<15;t++);//The delay is about 45us, DS18B20 samples data in 15~60us????????????for(t=0;t <10;t++);
DQ=1; //Release the data line
for(t=0;t<1;t++); //Delay 3us, at least 1us recovery period is required between two write sequences
dat>>=1;
}
for(t=0;t<4;t++);
}
void ReadyReadTemp(void)
{
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0x44);
delaynms(1000);//???????????????????????????? delaynms(200);
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0xbe);
}
DS18B20 single-wire digital temperature sensor, a "first-line device", has unique advantages:
(1) When connecting to the microprocessor using a single bus interface, only one port line is needed to achieve two-way communication between the microprocessor and DS18B20. The single bus has the advantages of good economy, strong anti-interference ability, suitable for on-site temperature measurement in harsh environments, and easy to use. It allows users to easily build sensor networks and introduces new concepts to the construction of measurement systems.
(2) Wide measurement temperature range: high measurement accuracy. The measurement range of DS18B20 is -55 ℃ ~+ 125 ℃; within the range of -10~+ 85°C, the accuracy is ± 0.5°C.
(3) No external components are required during use.
(4) Support multi-point networking function: Multiple DS18B20 can be connected in parallel on a single single line to achieve multi-point temperature measurement.
(5) Flexible power supply method: DS18B20 can obtain power from the data line through internal parasitic circuits. Therefore, when the timing on the data line meets certain requirements, no external power supply can be connected, thus making the system structure simpler and more reliable.
(6) Measurement parameters are configurable: The measurement resolution of DS18B20 can be set to 9~12 bits through the program.
(7) Negative voltage characteristics: When the polarity of the power supply is reversed, the thermometer will not burn due to heat, but it will not work properly.
(8) Power-off protection function: DS18B20 contains an EEPROM inside, which can still save the resolution and alarm temperature settings after the system is powered off.
DS18B20 has smaller size, wider applicable voltage, more economical, optional smaller package, and wider voltage applicable range. It is suitable for building your own economical temperature measurement system, so it is favored by designers.
Audio plug waterproof DS18B20 temperature sensor | DS18B20 digital temperature sensor in ABS housing |
DS18B20 internal structure:
Mainly composed of 4 parts: 64-bit ROM, temperature sensor, non-volatile temperature alarm trigger TH and TL, configuration register. The 64-bit serial number in the ROM is photo-engraved before leaving the factory. It can be regarded as the address serial number of the DS18B20. The 64-bit serial number of each DS18B20 is different. The cyclic redundancy check code (CRC=X^8+X^5+X^4+1) of the 64-bit ROM. The function of the ROM is to make each DS18B20 different, so that multiple DS18B20s can be connected to one bus.
DS18B20 pin arrangement:
1. GND is the power ground;
2. DQ is the digital signal input/output terminal;
3. VDD is the input terminal of the external power supply (grounded in the parasitic power wiring mode)
DS18B20 internal composition:
The scratchpad memory consists of 9 bytes. When the temperature conversion command is issued, the converted temperature value is stored in the 0th and 1st bytes of the cache memory in two-byte complement form. The microcontroller can read this data through the single-wire interface. When reading, the low bit is in front and the high bit is in the back. Corresponding temperature calculation: When the sign bit S=0, directly convert the binary bits to decimal. When S=1, first change the complement code to the original code, and then calculate the decimal value.
The lower eight bits of temperature data 0
High 8-bit data of temperature 1
High temperature threshold 2
Low temperature threshold 3
Reserved 4
Reserved 5
Count remaining value 6
Counts per degree 7
CRC check 8
The temperature sensor in DS18B20 completes the measurement of temperature and provides it in 16-bit binary format, where S is the sign bit.
For example:
+125℃ digital output 07D0H
(Positive temperature directly converts the hexadecimal number into decimal to obtain the temperature value)
-55℃ digital output is FC90H.
(For negative temperature, invert the obtained hexadecimal number, add 1, and then convert it into a decimal number)
Working sequence of DS18B20:
Initialization timing
The host first sends a low-level pulse of 480-960 microseconds, then releases the bus to high level, and detects the bus within the subsequent 480 microseconds. If a low level appears, it means that a device on the bus has responded. If there is no low level, it is always high level, indicating that there is no device response on the bus.
As a slave device, DS18B20 has been detecting whether there is a low level of 480-960 microseconds on the bus as soon as it is powered on. If so, wait 15-60 microseconds after the bus turns high, then pull the bus level low for 60-240 microseconds to respond with a pulse, telling the host that the device is ready. If it is not detected, it will keep checking and waiting.
write operation
The write cycle is a minimum of 60 microseconds and a maximum of 120 microseconds. At the beginning of the write cycle, the master first pulls the bus low for 1 microsecond to indicate the start of the write cycle. Subsequently, if the host wants to write 0, it will continue to pull the low level for at least 60 microseconds until the end of the write cycle, and then release the bus to a high level. If the host wants to write 1, it will initially pull the bus level low for 1 microsecond and then release the bus level to high level until the end of the write cycle. As a slave, DS18B20 waits for 15 microseconds after detecting that the bus is pulled down and then starts sampling the bus from 15us to 45us. It is 1 if the bus is high level during the sampling period, and 0 if the bus is low level during the sampling period.
Read operation
The read data operation timing is also divided into two processes: read 0 timing and read 1 timing. The read time slot is after the host pulls the single bus low, and then releases the single bus to high level after 1 microsecond to allow DS18B20 to transmit data to the single bus. DS18B20 starts sending data after detecting that the bus is pulled low for 1 microsecond. If it wants to send 0, it pulls the bus low until the end of the read cycle. If you want to send 1, release the bus to high level. The host initially pulls the bus low for 1 microsecond and then releases the bus, and then completes the sampling and detection of the bus within 15 microseconds including the previous 1 microsecond of pulling the bus level low. During the sampling period, the bus is low level. It is confirmed to be 0. If the bus is high during the sampling period, it is confirmed as 1. It takes at least 60us to complete a read timing process.
DS18B20 single wire communication:
The single-line communication function of DS18B20 is completed in time sharing, and it has a strict time slot concept. If a sequence disorder occurs, the 1-WIRE device will not respond to the host, so read and write timing is important. Various operations of the system on DS18B20 must be performed according to the protocol. According to the DS18B20 protocol, the microcontroller controls the DS18B20 to complete temperature conversion through the following three steps:
(1) Reset and initialize DS18B20 before each read and write. Reset requires the main CPU to pull the data line down for 500us and then release it. After receiving the signal, DS18B20 waits for about 16us~60us, and then sends out a low pulse of 60us~240us. After the main CPU receives this signal, it indicates that the reset is successful.
(2) Send a ROM command
(3) Send memory command
Specific operation examples:
Now what we have to do is to let DS18B20 perform a temperature conversion. The specific operation is:
1. The host performs a reset operation first.
2. The host then writes the ROM skip operation (CCH) command.
3. Then the host writes an operation command to convert the temperature, and then releases the bus for at least one second to allow the DS18B20 to complete the conversion operation. What should be noted here is that when writing each command byte, the low byte is written first. For example, the binary number of CCH is 11001100. When writing to the bus, it should be written from the low bit. The writing order is "zero, zero, one, one, zero, zero, one, one". The bus status of the entire operation is as shown below.
Read the temperature data in RAM. Similarly, this operation also requires three steps.
1. The host issues a reset operation and receives the response (existence) pulse of DS18B20.
2. The host issues a command (CCH) to skip the ROM operation.
3. The host issues a read RAM command (BEH), and then the host reads nine bytes of data from 0 to 8 sent by DS18B20 in sequence. If you only want to read the temperature data, just ignore the subsequent data sent by DS18B20 after reading the 0th and 1st data. Similarly, reading data is also low-order first. The bus status of the entire operation is as follows:
C language code:
sbit DQ=P3^3;
uchar t; //Set global variables, specifically used for delay programs
bit Init_DS18B20(void)
{
bit flag;
DQ=1;
_nop_(); //??????????????? for(t=0;t<2;t++);
DQ=0;
for(t=0;t<200;t++);
DQ=1;
for(t=0;t<15;t++);//???????????????? for(t=0;t<10;t++);
flag=DQ;
for(t=0;t<200;t++);
return flag;
}
uchar ReadOneChar(void)
{
uchar i=0;
uchar dat;
for(i=0;i<8;i++)
{
DQ=1;
_nop_();
DQ=0;
_nop_();
DQ=1; //Artificially pulled high to prepare for the microcontroller to detect the output level of DS18B20
for(t=0;t<3;t++);//Delay month 9us??????????????????????????? for(t=0; t<2;t++);
dat>>=1;
if(DQ==1)
{
dat|=0x80;
}
else
dat|=0x00;
for(t=0;t<1;t++);//Delay 3us, at least 1us recovery period is required between two read sequences??????????for(t=0;t<8 ;t++);
}
return dat;
}
void WriteOneChar(uchar dat)
{
uchar i=0;
for(i=0;i<8;i++)
{
DQ=1;
_nop_();
DQ=0;
_nop_();// ?????????????????????????????????????????? ??
DQ=dat&0x01;
for(t=0;t<15;t++);//The delay is about 45us, DS18B20 samples data in 15~60us????????????for(t=0;t <10;t++);
DQ=1; //Release the data line
for(t=0;t<1;t++); //Delay 3us, at least 1us recovery period is required between two write sequences
dat>>=1;
}
for(t=0;t<4;t++);
}
void ReadyReadTemp(void)
{
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0x44);
delaynms(1000);//???????????????????????????? delaynms(200);
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0xbe);
}