pengenalan: Artikel ini menerangkan secara terperinci aplikasi penderia suhu digital DS18B20 tersuai dalam membina termometer digital. Termasuk prinsip kerja, sambungan perkakasan, pengaturcaraan perisian dan pelaksanaan simulasi. Sediakan gambarajah simulasi protues yang lengkap, C kod sumber dan analisis hasil untuk membantu pembaca memahami dan mengamalkan penggunaan DS18B20 dengan mendalam.
Maklumat parameter: bekalan kuasa: 3.0V – 5.5V; Resolusi boleh laras: 9 – 12 sedikit; Julat suhu: -55 ℃ hingga +125 ℃; Keluaran : Merah (Vcc), kuning (DATA), Hitam (Gnd);
Apa yang anda dapat: anda akan dapat 4 Penderia suhu DS18B20, 4 modul penyesuai dan 4 wayar pelompat perempuan ke perempuan; Modul penyesuai mempunyai perintang tarik naik, yang boleh serasi dengan Raspberry Pi tanpa perintang luaran;
Sensor suhu DS18B20: saiz perumahan keluli tahan karat adalah lebih kurang. 6 x 50 mm/ 0.2 x 2 inci, dan kabel haba suhu digital mempunyai jumlah panjang lebih kurang. 1 m/ 39.4 inci, yang cukup panjang untuk memenuhi keperluan anda;
Material berkualiti: probe diperbuat daripada bahan keluli tahan karat yang berkualiti, yang kalis air, kalis lembapan dan tidak mudah berkarat, supaya dapat mengelakkan litar pintas;
Permohonan yang luas: sensor suhu DS18B20 ini serasi dengan Raspberry Pi, dan digunakan secara meluas dalam pemantauan suhu parit kabel, dandang, apa, rumah hijau pertanian, bilik bersih, dll.
1. Ciri penderia DS18B20
Sensor DS18B20 memainkan peranan penting dalam bidang pemantauan suhu moden. Ia boleh mengukur suhu dengan ketepatan yang tinggi, dan resolusinya boleh diselaraskan mengikut keperluan, untuk mencapai pemantauan suhu dengan darjah ketepatan yang berbeza. Di samping itu, saiz kecil DS18B20 menjadikannya sesuai untuk digunakan dalam persekitaran dengan ruang terhad, dan ciri-cirinya yang mudah digunakan mengurangkan ambang teknikal daripada pemula kepada profesional.
Sebelum meneroka lebih lanjut parameter prestasi DS18B20, adalah perlu terlebih dahulu memahami prinsip kerjanya. DS18B20 menyampaikan data suhu melalui isyarat digital, yang membawa kemudahan kepada pengumpulan data suhu. Berbanding dengan penderia suhu analog tradisional, penderia digital seperti DS18B20 boleh memberikan bacaan yang lebih tepat dan kurang sensitif kepada bunyi semasa penghantaran isyarat.
Untuk menggunakan sepenuhnya kelebihan DS18B20 ini, kita mesti mempunyai pemahaman yang mendalam tentang parameter prestasinya. Parameter ini termasuk julat pengukuran suhu, ketepatan, resolusi, dan bekalan voltan. Parameter ini bukan sahaja menentukan sama ada DS18B20 boleh memenuhi keperluan aplikasi tertentu, tetapi juga mempengaruhi prestasi dan kebolehpercayaan keseluruhan sistem.
Dalam bab ini, kami akan memperkenalkan parameter prestasi DS18B20 secara terperinci, menganalisis prinsip kerjanya, dan meneroka kelebihannya dalam aplikasi yang berbeza. Melalui kandungan ini, pembaca akan mendapat pemahaman yang lebih mendalam tentang penderia DS18B20 dan meletakkan asas yang kukuh untuk aplikasi dan pengaturcaraan yang lebih kompleks seterusnya.
2. Penjelasan terperinci tentang protokol komunikasi 1-Wayar DS18B20
Sebab mengapa penderia DS18B20 digunakan secara meluas adalah disebabkan oleh protokol komunikasinya yang unik – 1-Protokol komunikasi wayar. Protokol ini memudahkan keperluan untuk sambungan perkakasan dan menyediakan cara yang cekap untuk menghantar data. Bab ini akan menganalisis secara mendalam mekanisme kerja dan proses pertukaran data protokol komunikasi 1 talian untuk meletakkan asas yang kukuh untuk amalan pengaturcaraan seterusnya.
2.1 Asas Protokol Komunikasi 1-Wayar
2.1.1 Ciri-ciri Protokol Komunikasi 1-Wayar:
DS18B20 1-Wire Communication Protocol juga dipanggil “bas tunggal” teknologi. Ia mempunyai ciri-ciri berikut: – Komunikasi bas tunggal: Hanya satu talian data digunakan untuk penghantaran data dua arah, yang sangat mengurangkan kerumitan pendawaian berbanding kaedah komunikasi penderia berbilang wayar tradisional. – Sambungan berbilang peranti: Menyokong penyambungan berbilang peranti pada satu bas data, dan mengenal pasti dan berkomunikasi melalui kod pengenalan peranti. – Penggunaan kuasa yang rendah: Semasa komunikasi, peranti boleh berada dalam keadaan siap sedia berkuasa rendah apabila tidak mengambil bahagian dalam komunikasi. – Ketepatan tinggi: Dengan masa penghantaran data yang lebih singkat, ia boleh mengurangkan gangguan luar dan meningkatkan ketepatan data.
2.1.2 Format data dan analisis masa komunikasi 1 wayar
Format data protokol komunikasi 1 wayar mengikut peraturan pemasaan tertentu. Ia termasuk pemasaan permulaan, menulis masa dan membaca masa:
Masa permulaan: Hos mula-mula memulakan pemasaan pengesanan kehadiran (Nadi Kehadiran) dengan menurunkan bas untuk tempoh masa tertentu, dan sensor kemudian menghantar nadi kehadiran sebagai tindak balas.
Tulis masa: Apabila hos menghantar masa tulis, ia mula-mula menarik ke bawah bas selama kira-kira 1-15 mikrosaat, kemudian melepaskan bas, dan sensor menarik bas ke bawah 60-120 mikrosaat untuk bertindak balas.
Baca masa: Hos memberitahu penderia untuk menghantar data dengan menarik bas ke bawah dan melepaskannya, dan sensor akan mengeluarkan bit data pada bas selepas kelewatan tertentu.
3. Kaedah Sambungan Perkakasan Termometer
Sambungan perkakasan adalah langkah pertama dan paling penting dalam membina termometer digital. Sambungan yang betul antara sensor DS18B20 dan mikropengawal akan memastikan penghantaran data yang tepat dan menyediakan asas yang kukuh untuk pengaturcaraan perisian dan pemprosesan data selanjutnya. This chapter will introduce in detail the interface design principles between DS18B20 and microcontroller and the specific steps of circuit connection, and cover the relevant content of power supply and signal conditioning.
3.1 Interface between DS18B20 and microcontroller
3.1.1 Interface circuit design principles
The interface circuit design of DS18B20 needs to follow several core principles to ensure stable and efficient operation of the device:
Stable power supply: DS18B20 can obtain power from the data line “DQ” (called “parasitic power mode”), or it can be independently powered by an external power supply. Regardless of which method is used, the power supply must be stable to prevent data transmission errors caused by power supply fluctuations.
Signal integrity: Since DS18B20 transmits data through a single line, signal integrity is particularly critical. It is necessary to consider the anti-interference ability of the signal and the matching of the electrical characteristics of the signal.
Circuit protection: Overcurrent protection and electrostatic discharge (ESD) protection measures should be included in the circuit design to avoid damage to the sensor or microcontroller.
3.1.2 Specific steps for circuit connection
Connecting DS18B20 to a microcontroller usually follows the following steps:
Power connection: Connect the VDD pin of DS18B20 to a 3.3V or 5V power supply (depending on the voltage level of the microcontroller), and the GND pin to the ground line.
Data line connection: The DQ pin is connected to a digital I/O pin of the microcontroller. In order to ensure the stability of data transmission, a pull-up resistor can be added between the data line and the power supply, with a typical value of 4.7kΩ to 10kΩ.
Reset and presence pulse pin processing: Biasalah, the reset pin (RST) and presence pulse pin (PAR) of DS18B20 do not need to be connected externally, they are internally used signals.
In this section, we designed a basic circuit through which the DS18B20 temperature sensor can be connected to a microcontroller. The following is an example circuit diagram based on Arduino Uno and the corresponding description:
flowchart LR
DS18B20 — |VDD| 5V
DS18B20 — |Gnd| Gnd
DS18B20 — |DQ| 2
DQ — |Tarik ke atas| 5V
Antara mereka, DS18B20 represents the digital temperature sensor, 5V is the power output of the microcontroller, GND is the ground wire, dan 2 represents the Arduino’s pin No. 2, which is used for data transmission. The connection between DQ and 5V represents the pull-up resistor.
3.2 Power supply and signal conditioning
3.2.1 Choice of power supply method
DS18B20 provides two power supply methods:
Parasitic power mode: In this mode, the data line (DQ) can not only transmit data, but also power the DS18B20. Pada masa ini, the high-level voltage on the data line should be at least 3.0V to ensure sufficient power supply current. This mode is usually used when the bus length is short and data transmission is not too frequent.
External power supply mode: In this mode, DS18B20 has an independent power input VDD. Powering with an external power supply can enhance the signal strength of the sensor and improve anti-interference ability, which is suitable for long-distance transmission or frequent data transmission.
3.2.2 Signal filtering and stabilization
In order to ensure signal stability and accurate data reading, the signal needs to be properly filtered and stabilized:
Pull-up resistor: The pull-up resistor is added between the data line and the power supply to ensure that the data line is in a high-level state when idle.
De-jitter circuit: In order to eliminate erroneous readings caused by line interference or instantaneous voltage fluctuations, the signal can be software-de-jittered on the microcontroller side.
ESD protection: ESD protection components (such as TVS diodes) are added to the ports of sensors and microcontrollers to prevent damage caused by electrostatic discharge.
This section further elaborates on the factors that should be considered when selecting power supply and signal conditioning in table form:
| Project | Parasitic power mode | External power mode | Penerangan | | — | — | — | — | | Senario yang berkenaan | Short lines, infrequent data | Long lines, frequent data | Select according to actual application scenarios | | Power supply stability | Lebih rendah | Higher | External power supply is recommended for long lines or high frequencies | | Cost | Lebih rendah | Higher | External power supply requires additional power management components | | Anti-interference | Weaker | Stronger | External power supply is more suitable for high-interference environments |
The above connection methods and signal processing strategies can effectively integrate the DS18B20 temperature sensor into any microcontroller system. The next chapter will introduce how to use C language to:
Functional programming practice of DS18B20:
4. DS18B20 digital thermometer C language programming
4.1 Programming foundation and environment preparation
4.1.1 Program design ideas and framework construction
Before starting to write the C language program of the DS18B20 digital thermometer, you first need to establish the basic ideas of program design. The DS18B20 sensor communicates with the microcontroller through the 1-wire communication protocol. Oleh itu, the main task of the program is to implement the related operations of the 1-wire communication protocol, including initializing DS18B20, sending instructions, reading temperature data, and converting and displaying the read data.
The program framework is roughly divided into the following parts:
Inisialisasi: Initialize the microcontroller and DS18B20 sensor.
Main loop: Contains a loop that continuously reads sensor data.
1-wire communication function library: Contains functions for implementing the one-wire communication protocol.
Data processing: Convert the raw data returned by the sensor into readable temperature values.
Display output: Display the processed temperature data on the LCD screen or output it to the computer through the serial port.
4.1.2 Development environment construction and configuration
In order to program and develop the DS18B20 digital thermometer, you need to prepare the development environment and configure it appropriately. The following are the basic steps for development:
Select the development environment: Select the appropriate integrated development environment (IDE) according to the type of microcontroller, such as for development based on the ARM Cortex-M series microcontroller. You can use Keil MDK or STM32CubeIDE.
Configure the compiler: According to the IDE used, configure the compiler to ensure that the C language code can be compiled correctly.
Build the hardware development board: Select a suitable microcontroller development board, such as based on STM32, ESP32, dll.
Connect the development board: Connect the DS18B20 sensor to the specified pin of the microcontroller through the 1-wire communication protocol.
Write code: Create a new C language project in the IDE and start writing program code.
Compile and debug: Use the IDE tool to compile the code and run it on the development board for debugging.
#termasuk <stdio.h>
// DS18B20 first-line communication function library declaration
batal DS18B20_Init();
void DS18B20_Reset();
batalkan DS18B20_WriteByte(unsigned char dat);
unsigned char DS18B20_ReadByte();
int DS18B20_ReadTemperature();
int utama() {
// Initialize DS18B20 sensor
DS18B20_Haba();
// Main loop
sementara(1) {
// Read temperature value
int temperature = DS18B20_ReadTemperature();
// Output temperature value to serial port or other display device
printf(“Current temperature: %d\n”, suhu);
}
kembali 0;
}
4.2 DS18B20 temperature reading program implementation
4.2.1 Construction of the one-wire communication function library
In order to realize the temperature reading of DS18B20, you first need to build a one-wire communication function library. The following are the implementation methods of several key functions:
DS18B20_Haba(): Initialize the one-wire communication timing.
DS18B20_Reset(): Reset the sensor and detect its pulse.
DS18B20_WriteByte(unsigned char dat): Write a byte of data to the sensor.
DS18B20_ReadByte(): Read a byte of data from the sensor.
DS18B20_ReadTemperature(): Read the temperature and convert it.
The implementation of the one-wire communication function library of DS18B20 is quite complicated because it requires precise control of the pin level changes to follow the one-wire communication protocol. The following is an example of a function implementation:
void DS18B20_Reset() {
// One-line communication reset sequence, including pulling down the data line, delay, releasing the bus, and detecting the presence pulse
// …
}
The purpose of this function is to send a reset pulse to the DS18B20. After the reset is successful, the DS18B20 will return a presence pulse.
4.2.2 Implementation of the temperature reading algorithm
Reading the temperature value of the DS18B20 sensor is a more complicated process, because it is necessary to send specific instructions to the sensor in a certain timing and read the returned data correctly. The algorithm for reading the temperature value is as follows:
Reset the sensor.
Send the “skip ROM” command (0xCC).
Send the “convert temperature” command (0x44).
Wait for the conversion to complete.
Send the “read register” command (0xBE).
Read two bytes of temperature data.
The following code shows how to read the temperature value of the DS18B20:
int DS18B20_ReadTemperature() {
unsigned char temp_low, temp_high;
unsigned int temp;
// Reset the sensor and skip ROM instructions
DS18B20_Reset();
DS18B20_WriteByte(0xCC); // Skip ROM commands
// Send conversion temperature command
DS18B20_WriteByte(0x44);
// Wait for the conversion to complete. Here you need to wait according to the conversion time of DS18B20
// …
// Reset the sensor and read the temperature data
DS18B20_Reset();
DS18B20_WriteByte(0xCC); // Skip ROM commands
DS18B20_WriteByte(0xBE); // Read register command
// Read two bytes of data
temp_low = DS18B20_ReadByte();
temp_high = DS18B20_ReadByte();
// Combine two bytes of data into a 16-bit integer
temp = (temp_high << 8) | temp_low;
// Return the temperature value, converting appropriately based on the resolution of the DS18B20
temp balik;
}
4.2.3 Program debugging and exception handling
When writing a DS18B20 reading program, program debugging and exception handling are very important. During debugging, you may need to use the serial port debugging assistant to check whether the output temperature value is correct, or use a logic analyzer to monitor the signal timing of the first-line communication. Exception handling needs to take into account hardware failures, communication errors, and abnormal responses of DS18B20.
The following are some debugging and exception handling strategies:
Data verification: After each data read, use a checksum or check bit to confirm the correctness of the data.
Exception capture: Add an exception capture mechanism to the program, such as a timeout retry mechanism, reset the sensor, dll.
Debug information: Add sufficient debugging information output to the program to help locate the problem.
int utama() {
// Initialize DS18B20 sensor
DS18B20_Haba();
// Main loop
sementara(1) {
int temperature;
// Read temperature and check for errors
temperature = DS18B20_ReadTemperature();
jika (suhu < 0) {
printf(“Error reading temperature!\n”);
// You can choose to retry or other error handling mechanisms
} else {
printf(“Current temperature: %d\n”, suhu);
}
}
kembali 0;
}
This chapter introduces the C language programming foundation and environment preparation of the DS18B20 digital thermometer, as well as the implementation of the temperature reading program, and emphasizes the importance of program debugging and exception handling. Through the introduction of this chapter, readers should be able to build a development environment, understand the importance of the first-line communication function library, and write a basic temperature reading program. The following chapters will further delve into the construction and use of the Proteus simulation environment, providing a simulation test method for actual hardware assembly.
5. Proteus simulation diagram and simulation result analysis
5.1 Proteus simulation environment construction
5.1.1 Basic operation of Proteus software
Before starting to build the simulation model of the DS18B20 digital thermometer, you first need to understand and master the basic operation of Proteus software. Proteus is a powerful electronic circuit simulation software that can not only design circuit schematics, but also design circuit PCB layouts and provide simulation functions. Here are some key steps to help you get started with Proteus:
Open the Proteus software and create a new project.
Search and select the required components in the component library, such as DS18B20 sensors, microcontrollers, bekalan kuasa, connecting wires, dll.
Drag the selected components to the design area and use the mouse to place and layout them.
Use the wiring tool to connect the pins of each component to form a complete circuit.
Double-click a component or wire to modify its properties, such as resistance value, power supply voltage, dll.
Make sure all components are connected correctly and check for errors or omissions.
5.1.2 Create a DS18B20 simulation project
The steps to create a simulation project for the DS18B20 digital thermometer are as follows:
Start Proteus and select “New Project” to create a new project.
After setting the project name and location, click “Seterusnya”.
Select a project template, seperti “Microprocessor Based”, and click “Seterusnya”.
In the “Project Items” tab, check “Include default components” and select a microcontroller (such as PIC, AVR, dll.) and a DS18B20 sensor.
Click “Finish” to complete the project creation.
Seterusnya, create a circuit schematic:
Select the “PICK DEVICE” tool, find and select the microcontroller and DS18B20 sensor in the component library.
Use the “PLACE DEVICE” tool to place the selected component in the design area.
Use the “WIRE” tool to connect the microcontroller and the relevant pins of the DS18B20 sensor.
After completing the connection, use the “TEXT” tool to add annotations to the circuit diagram for easy understanding and modification.
5.2 Simulation test and data analysis
5.2.1 Set simulation parameters and conditions
Before starting the simulation, you need to set the parameters and conditions for the simulation run:
Double-click the microcontroller component to enter the property setting interface.
Select the previously written program file path at “Program File”.
Set the power supply parameters to ensure that both the microcontroller and the DS18B20 sensor have the correct power supply voltage.
Seterusnya, set the time parameters for the simulation:
In the simulation control panel, select “Global Settings”.
Adjust the simulation speed and maximum simulation time.
Set appropriate breakpoints to analyze data during the simulation process.
5.2.2 Simulate and read temperature data
Run the simulation and simulate temperature data:
Click the “Play” button in the simulation control panel to start the simulation.
Use the “DEBUG” tool to view the program running status and variable values.
Simulate the DS18B20 sensor to read the temperature value, which is usually achieved by modifying the virtual thermometer in the simulation environment.
To read temperature data in the simulation, you can refer to the following steps:
Find the temperature simulation settings in the properties of the DS18B20 component.
Modify the temperature value to test the system response under different temperature conditions.
Observe how the microcontroller program processes the temperature data.
5.2.3 Result Analysis and Troubleshooting
Analyze the simulation results and confirm the performance of the thermometer:
Monitor the data in the output window to check whether the temperature reading is accurate.
Use the logic analyzer tool to monitor whether the data communication process is normal.
Check for any abnormal signals or unstable outputs.
Perform fault diagnosis and debugging:
If the temperature reading is inaccurate or there is an error, check the connection method and configuration of the DS18B20.
Analyze the program code to ensure that the first-line communication and data conversion algorithms are implemented correctly.
Use the “Stop” function of the simulation software to pause the simulation and observe the current status of the system.
English
Afrikaans
العربية
বাংলা
bosanski jezik
Български
Català
粤语
中文(简体)
中文(漢字)
Hrvatski
Čeština
Nederlands
Eesti keel
Suomi
Français
Deutsch
Ελληνικά
हिन्दी; हिंदी
Magyar
Bahasa Indonesia
Italiano
日本語
한국어
Latviešu valoda
Lietuvių kalba
македонски јазик
Bahasa Melayu
Norsk
پارسی
Polski
Português
Română
Русский
Cрпски језик
Slovenčina
Slovenščina
Español
Svenska
ภาษาไทย
Türkçe
Українська
اردو
Tiếng Việt





