This tutorial demonstrates how to integrate an SD card with a CH32 microcontroller for data logging applications, building upon previous SPI communication examples. The video details the hardware setup, including the CH32 development board, an SD card module, and a DS18B20 temperature sensor. It explains the process of adapting and configuring the FAT FS library for the CH32, alongside developing custom SPI and SD card communication handlers. Key code functionalities, such as dynamic SPI baud rate adjustment, robust file reading (from a predefined text file), and temperature data logging (to a newly created file), are explained. The tutorial emphasizes practical considerations like memory optimization by conditionally compiling debug output and adhering to file naming conventions. The video concludes with a successful live demonstration of reading from and writing real-time temperature data to an SD card, validating the system's functionality for remote data acquisition.
Interfacing SD Cards with CH32 Microcontrollers: A Comprehensive Tutorial
0:00 Introduction to SD Card Integration: The video continues the CH32 tutorial series, focusing on integrating an SD card with the microcontroller for data storage, expanding on previous SPI communication projects.
0:11 Hardware Setup: The presenter demonstrates the physical connections, including the CH32 development board, an SD card reader module (utilizing SPI), and a DS18B20 digital thermometer. Key detail: the SD card module requires a 5V input, which is then regulated down to 3.3V on-board for the components.
2:20 Utilizing the FAT FS Library: The project leverages the open-source FAT FS library for file system management, noting that it requires some minor adjustments to work seamlessly with CH32 microcontrollers. The complete project code will be shared on GitHub.
3:22 Core Library Files and Custom Handlers: Key FAT FS files are introduced, including diskio.c/h (low-level disk I/O), ff.c/h (generic file system), and ff.h (the main configuration file). The presenter also details the need for custom SPI and SD card communication handler files developed specifically for this project.
5:50 SD Card Detection and SPI Configuration: The tutorial explains the implementation of SD card insertion detection and the strategy of using two different SPI baud rate prescalers (slow for initial setup, then high speed for data transfer). The chip select pin is assigned to PC4.
7:54 Code Structure and Memory Optimization: The video details the project's file organization and introduces a DEBUG macro. This macro allows for conditional compilation of printf (UART) debugging output, saving approximately 2KB of flash memory, which is crucial for standalone microcontroller applications.
12:42 SD Card Initialization Sequence: A detailed walkthrough of the initialization process is provided: setting up card detection, a 10-second countdown for user card insertion, detecting the card's presence, successful initialization, determining the SD card's size (e.g., 8GB), switching to high-speed SPI operation, and finally, mounting the file system.
16:39 File Reading Demonstration: The process of opening and reading a read_test.txt file from the SD card is shown, with its contents printed line by line to the serial terminal. A key takeaway is the 8-character filename limitation imposed by the FAT FS configuration to optimize memory usage.
18:49 Data Logging (File Writing) Implementation: The tutorial covers creating/overwriting a write_test.txt file, writing a header message, and then appending real-time temperature readings (from the DS18B20 sensor) every second for 10 entries. This involves using f_lseek to find the end of the file and f_write for appending data.
22:50 Live Demonstration and Verification: The presenter provides a live demonstration via the serial terminal, showing the entire process from card detection and initialization to reading a test file and logging temperature data. The logged data is then verified by physically removing the SD card and inspecting its contents on a computer, confirming successful read and write operations.
27:54 Conclusion and Project Resources: The video concludes by summarizing the successful implementation of SD card read/write functionality with the CH32 for data logging. Viewers are encouraged to visit the presenter's website for an accompanying article and GitHub page to access the full project files.