4.5 DS18B20 Temperature Sensor
4.5.1 Schematic diagram introduction
Figure 4-5-1
The DS18B20 module on the experimental board is connected to the P3.7 IO port of the microcontroller. When inserting the DS18B20 chip, insert it with the arc facing upwards. The specific effect can be seen in the picture above.
4.5.2 DS18B20 Temperature Sensor Introduction
DS18B20 is a commonly used digital temperature sensor that outputs a digital signal. Its temperature detection and digital data output are fully integrated on one chip, making it more resistant to interference.
The main features of DS18B20 are as follows:
(1) Fully digital temperature conversion and output.
(2) Advanced single bus data communication. (Temperature can be collected with one line)
(3) Maximum 12-bit resolution, accuracy up to ±0.5 degrees Celsius.
(4) The maximum duty cycle at 12-bit resolution is 750 milliseconds.
(5) Parasitic working mode can be selected.
(6) The detection temperature range is –55° C ~+125° C (–67° F ~+257° F)
(7) Built-in EEPROM, temperature limit alarm function.
(8) 64-bit photolithography ROM, built-in product serial number, convenient for multiple machines to connect. (Supports one line to operate multiple chips)
(9) Various packaging forms to adapt to different hardware systems. The packaging can be seen in the picture below.
Figure 4-5-3
The pin functions of the chip in the picture: GND voltage ground, DQ single data bus, VDD power supply voltage, NC empty pin
4.5.3 Introduction to the working principle of DS18B20
The temperature detection and digital data output of DS18B20 are fully integrated on one chip, making it more resistant to interference.
One of its working cycles can be divided into two parts: temperature detection and data processing.
There are three types of memory inside DS18B20:
(1) ROM read-only memory: used to store the DS18B20 ID code. The first 8 bits are the single-line serial code (DS18B20 code is 19H), the next 48 bits are the chip's unique serial number, and the last 8 bits are the CRC code (redundancy check) of the above 56 bits. The data is set when the chip leaves the factory and cannot be changed by the user. DS18B20 has a total of 64 bits of ROM (8+48+8).
(2) RAM data register: used for internal calculation and data access. Data will be lost after power failure. DS18B20 has 9 bytes of RAM, each byte is 8 bits. The 1st and 2nd bytes are the data value information after temperature conversion. The 3rd and 4th bytes are the mirror image of the user's EEPROM (commonly used for temperature alarm value storage). Its value will be refreshed during power-on reset. The 5th byte is the mirror image of the user's 3rd EEPROM. The 6th, 7th and 8th bytes are count registers, which are designed to allow users to obtain higher temperature resolution. They are also temporary storage units for internal temperature conversion and calculation. The 9th byte is the CRC code of the first 8 bytes.
(3) EEPROM non-volatile memory: used to store data that needs to be saved for a long time. For example: upper and lower temperature alarm values and calibration data. DS18B20 has a total of 3 bytes of EEPROM, and both are mirrored in RAM for user convenience.
Figure 4-5-4
DS18B20 works in 12-bit resolution mode by default. The 12-bit data obtained after conversion is stored in two 8-bit RAMs of DS18B20 (the first two bytes). The first 5 bits in binary are sign bits. If the measured temperature is greater than 0, these 5 bits are 0. The actual temperature can be obtained by multiplying the measured value by 0.0625. If the temperature is less than 0, these 5 bits are 1. The measured value needs to be inverted, added by 1, and then multiplied by 0.0625 to get the actual temperature.
Data extraction can also use bit operations. The data read out is 2 bytes with a total of 16 bits (H and L). The lowest 4 bits are decimal places, and the rest are integer places. If the data read is a negative number, you need to -1 and then negate it to get the real data.
For example:
(Hardware platform description: CPU is STC90C516RD, crystal frequency is 12MHZ, working in 12T mode, one machine cycle is 1us)
int temp=0;temp=DS18B20_ReadTemp(); //Read the temperature collected by DS18B20 once (return H+L bits)if(temp<0) //If the temperature is negative{ temp=temp-1; temp=~temp; printf("DS18b20=-%d.%drn",temp>>4,temp&0xF);}else{printf("DS18b20=%d.%drn",temp>>4,temp&0xF);}
4.5.4 Introduction to common instructions for DS18B20 to operate ROM/RAM
1. Read 64-bit ROM encoding instruction: 0x33
This command allows the bus controller to read the 64-bit ROM of the DS18B20. This command can only be used when there is only one DS18B20 on the bus. If more than one is connected, data conflicts will occur during communication.
2. Match chip instruction: 0x55
This instruction is followed by a 64-bit serial number issued by the controller. When there are multiple DS18B20s on the bus, only the chip with the same serial number as the one issued by the controller can respond, and the other chips will wait for the next reset. This instruction is suitable for single-chip and multi-chip connection.
3. Skip ROM code matching: 0xCC
This instruction makes the chip not respond to the ROM code. In the case of a single bus, this instruction can be used to save time. If this instruction is used when multiple chips are connected, data conflicts will occur, resulting in errors.
4. Start temperature conversion: 0x44
After receiving this instruction, the chip will perform a temperature conversion and store the converted temperature value in the 1st and 2nd addresses of the RAM. Since the chip is busy with the temperature conversion process, when the controller sends a read time slot, the bus outputs "0", and when the storage work is completed, the bus will output "1". In the parasitic working mode, a strong pull-up must be used immediately after issuing this instruction and maintained for at least 500MS to maintain the chip working.
5. Read data from RAM: 0xBE
This instruction will read data from RAM, starting from address 0 and continuing to address 9 to complete the reading of the entire RAM data. The chip allows the reset signal to terminate the reading process, that is, the subsequent unnecessary bytes can be skipped to reduce the reading time.
4.5.5 DS18B20 Timing Diagram
(1). DS18B20 reset and response timing diagram
Figure 4-5-5 Reset and response timing diagram
Figure 4-5-6
A reset must be performed before each communication with DS18B20. The reset time, waiting time, and response time should be strictly programmed according to the timing.
(2) Data writing timing
Figure 4-5-7 Data writing timing diagram
The timing diagram above is the timing diagram for writing data to DS18B20. The diagram is divided into two parts. The left part is the timing diagram for sending data 0, and the right part is the timing diagram for sending data 1.
According to the timing diagram (right part), before sending data each time (regardless of whether it is data 0 or data 1), the bus needs to be pulled low for at least 1us.
If the next data to be sent is 0, the data line needs to be pulled low for at least 60us. The sampling time of the bus by DS18B20 is within 15~60us. During the sampling time, if the bus is at a low level, it means writing 0, and if the bus is at a high level, it means writing 1.
If the next data to be sent is 1, the data line also needs to be pulled low for at least 60us. The sampling time of the bus by DS18B20 is within 15~60us. During the sampling time, if the bus is at a low level, it means writing 0, and if the bus is at a high level, it means writing 1.
Note: During communication, data is transmitted to DS18B20 in units of one byte, and the reading or writing of bytes starts from the low bit.
(3) Data read timing
Figure 4-5-8 Read data timing diagram
The timing diagram above is the timing diagram for reading data from DS18B20. The diagram is divided into two parts. The left part shows the change process of the bus level when reading data 0, and the right part shows the change process of the bus level when reading data 1.
According to the timing diagram (right part), before reading data each time (regardless of data 0 or data 1), the bus needs to be pulled down by the host for at least 1us, and then released (pulled high);
After that, you need to wait for 15us before you can read the bus data. DS18B20 will maintain the bus level for the next 45us, and the data read from the bus during this period are all valid data.
Note: After the bus is pulled low for 1us, you must wait for 15us before reading the bus data to ensure that the bus data is accurate.
During the communication process, the data output by DS18B20 is transmitted starting from the low bit.
4.5.6 Steps to read temperature
The scenario where there is only one DS18B20 on the bus (read the temperature of DS18B20 once):
1. Send a reset pulse to the bus and detect the response signal of DS18B20 (to ensure that there is no problem with the DS18B20 hardware)
2. Send a command to skip the ROM number check (command 0xCC)
3. Send temperature conversion command (command 0x44)
(When DS18B20 is converting temperature, the bus will remain in a high level state and will not respond to bus commands)
4. Send a reset pulse to the bus and detect the response signal of DS18B20
5. Send command to skip ROM number check (command 0xCC)
6. Send the command to read the temperature (command 0xBE)
7. Then read the lower 8 bits of the temperature data
8. Then read the high 8 bits of temperature data
4.5.7 Sample code for reading DS18B20 temperature (single DS18B20 scenario)
The following code demonstrates the process of looping and reading the temperature of DS18B20. The temperature is read once every 1 second in the main function.
When writing DS18B20 timing code, pay attention to time control.
The current experimental board environment: using STC90C516RD microcontroller, the crystal oscillator is 12MHZ, working in 12T mode, the time consumed to execute an i++ statement in the code is about 12us. The delay time in the program is calculated based on this time. If the program is to be ported to other microcontrollers, attention should be paid to the time issue.
Previous article:Design of infrared remote control decoding program based on 51 single chip microcomputer
Next article:Design of two DS18B20 temperature sensors based on 51 single chip microcomputer
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- I would like to ask about the phase relationship of the all-pass response
- 【Chuanglong TL570x-EVM】HELLO_WORLD of python!
- Operational amplifier stability analysis problem
- 【NXP Rapid IoT Review】+ My Review Summary
- VHDL Introduction. Questions and Answers. Classic Examples. Experience Summary
- ROM and RAM How does the microcontroller execute step by step after power is turned on?
- How does the program execute from the reset sequence to the main function?
- [AutoChips AC7801x motor demo board review] + Motor drive - BLDC sensorless method
- EEWORLD University ---- Practical Projects for IoT Embedded Engineers
- AC 220v to DC 24v (LM7824CT)