Application of DS18B20 Digital Temperature Sensor in Temperature Measurement

January 21, 2023

Photocoupler

1 Introduction

In modern industrial production, people need to detect and control the temperature. The use of 51 single-chip microcomputer to control the temperature has the advantages of convenient control, simple configuration and great flexibility. The design of this paper discusses the application of DS18B20 digital temperature sensor in temperature measurement according to practical experience; it has high measurement accuracy. It is suitable for chemical production and power engineering industries with high temperature accuracy requirements.

2. Temperature control system

The DS18B20 digital temperature sensor produced by DALLAS in the United States can directly convert the measured temperature into a serial digital signal for processing by the microcomputer, and realize 9-bit temperature reading by simple programming. And a plurality of DS18B20 can be connected to a plurality of address lines and communicate with the single chip microcomputer.

The DS18B20 uses the 1-Wire bus protocol, which implements bidirectional transmission of data on one data line. For the AT89S51 microcontroller, the hardware does not support the single bus protocol. Therefore, we must use software to simulate the single. The protocol timing of the bus completes access to the DS18B20 chip. Since the DS18B20 reads data on an I/O line, there are strict timing requirements for the data bits read and written.

In addition, since the DS18B20 single-line communication function is completed in a time-sharing manner, that is, asynchronous communication, it has a strict time slot concept, so the read and write timing is very important. The system's various operations on the DS18B20 must be performed in accordance with the protocol. The operating agreement is:

Initialize DS18B20 (reset pulse) → send ROM function command → send memory operation command → process data DS18B20 temperature sensor temperature measurement error: in the range of 0--70 °C, the upper and lower limits of DS18B20 are +0.5 °C and -0.5 ° C, and the typical product error is only 0.25 ° C.

3. System software design

The system software design here refers to the software design of the single chip microcomputer. The system uses the C language to program the program, mainly including the display subroutine, the reset program, the write program read program, the interrupt response and the like.

3.1 Software Design of DS18B20

The first-line work protocol flow of the DS18B20 is: initialization → ROM operation instruction → memory operation instruction → data transmission. Its working timing includes initialization timing, write timing, and read timing. Therefore, the host computer controls the DS18B20 to complete the temperature conversion. It must go through three steps: reset the DS18B20 before each read and write. After the reset is successful, send a ROM instruction and finally send the RAM instruction, so that the DS18B20 can perform the predetermined operation. The reset requires the main CPU to pull down the data line for 500 microseconds, then release. After receiving the signal, the DS18B20 waits for about 16~60 microseconds, and then sends a low pulse of 60~240 microseconds. The main CPU receives this signal to indicate that the reset is successful.

3.2 Write data to DS18B20

The write timing is generated when the host pulls the data line from a high logic level to a low logic level. There are two types of write timing: write 1 timing and write 0 timing. All timings must have a duration of a minimum of 60 microseconds, with a minimum recovery time of 1 microsecond between write cycles. The DS18B20 samples the 1/0 line between the 15 s to 60 s windows after the 1/0 line changes from high to low. If the line is high, write 1 occurs. If the line is low, write 0 occurs. For the DS18B20 write 0 timing and write 1 timing requirements are different, when writing 0 timing, the single bus should be pulled down at least 60us To ensure that the DS18B20 can correctly sample the "0" level on the IO bus between 15us and 45us. When the 1st timing is to be written, after the single bus is pulled low, the single bus must be released within 15us.

3.3 Reading data from DS18B20

When reading data from the DS18B20, the host generates a read sequence. The read timing is generated when the host pulls the data line from a logic high to a low level. The data line must be held at a low logic level for at least 1 microsecond; the output data from the DS18B20 is valid for 15 microseconds after the falling edge of the read time slice. Therefore, in order to read the state of 15 microseconds from the start of the read sequence, the host must stop driving the 1/0 pin low. At the end of the read sequence, the 1/0 pin is pulled back high by an external pull-up resistor. Level. The minimum duration of all read timings is 60 microseconds, and there must be a recovery time of at least 1 microsecond between each read sequence.

3.4 Interrupt Service Program Design

When the CPU processes an event A, another event B occurs to request the CPU to quickly process (interrupt occurs); the CPU temporarily interrupts the current work, and goes to process event B (interrupt response and interrupt service); wait for the CPU to event B After the processing is completed, go back to the place where the original event A was interrupted and continue to process event A (interrupt return). This process is called interrupt.

AT89C51 microcontroller interrupt processing: interrupt response condition and time is interrupt source interrupt request; interrupt source enable bit of this interrupt source is 1; CPU open interrupt (ie EA = 1). When the above three items are satisfied at the same time, the CPU is likely to respond to the interruption.

The timer mode control register TMOD is used to set the function mode and working mode of Tn and the gate control signal. You can only byte address, the byte address is 89H, the upper four bits are used for timer T1, and the lower four bits are used for timing. T0. Timer/Counter mode control register TMOD cannot be bit-addressed. It can only be used for byte transfer instructions to set the timer mode. The lower nibble is defined as timer 0 and the high nibble is defined as timer 1. At reset, all bits of TMOD are 0.

3.5 main program flow chart

The program starts with initialization, resets the system, and then sends a detection signal. If the system detects the sampling signal, the P3.7 port of the AT89C51 microcontroller receives the serial data, and then calls the subroutine: DS18B2O initialization, reading data, Write data. Before the temperature is converted and the temperature is read, the DS18B20 is first initialized by generating a reset pulse, and then the DS18B20 sends an acknowledge pulse, and the subsequent operation is started when the MCU receives the acknowledge pulse.

The program should be performed in strict timing to ensure data integrity. Finally, the collected temperature is displayed by the digital tube.

4 Conclusion

This paper combines various information processing technologies and single bus technology for the problems of temperature detection, and designs a real-time, comprehensive and scientific temperature detection system. In this system, modular and hierarchical design is adopted. The single-bus communication standard is adopted between the single-chip microcomputer and the temperature sensor, and the detection data is read by a simple and efficient communication circuit. The excellent function of the system can realize comprehensive, real-time and automatic detection of temperature, timely discover problems and take effective protective measures.

Energy storage power supply

SHENZHEN CHONDEKUAI TECHNOLOGY CO.LTD , https://www.szsiheyi.com