This video is the 11th part of a CH32 tutorial series, focusing on implementing a one-wire communication protocol with the DS18B20 digital thermometer using the CH32V003 microcontroller. The tutorial details the setup of a bi-directional GPIO pin (PD2) as an open-drain output and floating input, essential for the single-wire communication. It demonstrates the implementation of the precise timing sequences required for the DS18B20's reset/presence detection, as well as bit-level and byte-level read and write operations. The collected 12-bit raw temperature data is then converted to Celsius and displayed on a serial terminal, showcasing a practical application that lays the groundwork for more complex future projects.
Summarizing the CH32V003 DS18B20 One-Wire Communication Implementation
0:00 - Project Introduction: Continues the CH32 tutorial series by implementing a one-wire communication protocol with the DS18B20 digital thermometer sensor. This serves as a foundation for future, more complex projects.
0:31 - DS18B20 Features: The DS18B20 is a unique digital thermometer chip known for its one-wire communication, requiring only a single data line in addition to VCC and Ground. It measures temperatures from -55°C to 125°C with an accuracy of ±0.5°C between -10°C and 85°C, making it suitable for household temperature monitoring or logging.
1:48 - Development Environment & Goal: The project uses Moon River Studio 2 for development. The immediate goal is to print temperature values to a serial terminal, with the data intended for transfer elsewhere in future projects.
3:08 - GPIO Pin Configuration: A dedicated GPIO pin (PD2) is set up to handle both input and output. It's configured as an open-drain output (for pulling the line low) and a floating input (for reading the chip's response when the line is released). An external 4.7kΩ (or 3.3kΩ used in demo) pull-up resistor is essential for the data line's default high state.
8:04 - Reset Pulse and Presence Detection: The communication initiates with a reset pulse: the microcontroller pulls the data line low for at least 480 microseconds, then releases it and switches to input mode. The DS18B20 responds with a low "presence" pulse within 15 to 60 microseconds, confirming its readiness to communicate.
Master Write Zero: Pull the line low for 60-120 microseconds, then release.
Master Write One: Pull the line low for a short duration (e.g., 5 microseconds), then release.
Master Read (Zero/One): Pull the line low, wait briefly, then release. Read the state of the line within 15 microseconds; if low, it's a zero; if high (due to pull-up), it's a one.
17:29 - DS18B20_ReadBit Function: Microcontroller pulls line low, waits ~6µs, switches to input, reads the bit, and then waits for the remainder of the 60µs time slot.
20:00 - DS18B20_WriteBit Function: If writing '1', pull low for 5µs, then release. If writing '0', pull low for 60µs, then release.
22:51 - Byte-Level Read/Write Functions:DS18B20_WriteByte iterates through 8 bits, sending each using the DS18B20_WriteBit function. DS18B20_ReadByte also iterates, reading bits and reconstructing the byte using bit-shifting and logical OR operations.
27:20 - Temperature Reading Sequence: The DS18B20_GetTemperatureRaw function first checks for the chip's presence. It then sends 0xCC (Skip ROM) and 0x44 (Convert Temperature) commands. After waiting for the conversion (which can take a significant amount of time, detected by polling the data line), it sends 0xCC and 0xBE (Read Scratchpad) to retrieve the 2-byte raw temperature data (LSB first, then MSB).
31:11 - Temperature Conversion and Display: The 12-bit raw temperature data is scaled (multiplied by 10) and divided by 16 (the LSB value for 12-bit resolution) to prepare it for display. Due to the microcontroller's limitations in directly printing floating-point numbers over UART, the scaled integer is split into whole and fractional parts, which are then printed sequentially to mimic a floating-point number in Celsius.
35:28 - Live Demonstration: The code successfully displays raw temperature values and converted Celsius readings (e.g., 24.1°C) on the serial terminal, demonstrating real-time temperature changes when the sensor is manipulated.
36:20 - Resources and Conclusion: The video concludes by highlighting the ease of setting up one-wire communication and its utility for future projects. Links to a website for detailed explanations and a GitHub page for the full source code (main.c) are provided.