This video, part of a CH32 tutorial series, focuses on implementing the SPI (Serial Peripheral Interface) communication protocol on the CH32V003 microcontroller. Using an ADXL345 3-axis accelerometer as a slave device, the tutorial demonstrates how to configure the microcontroller as an SPI master. Key aspects covered include comparing SPI to I2C (highlighting SPI's speed, full-duplex capability, and simplified addressing with Chip Select), setting up the SPI peripheral and associated GPIO pins in software, implementing functions for SPI data transfer, reading and writing device registers (including multi-byte reads), and specific configuration of the ADXL345 sensor for measurement mode and data rate. The video concludes by showing how to read raw acceleration data, process it into G units using integer math tricks to simulate floating-point output, and visualize the real-time sensor readings using the Arduino Serial Plotter.
Implementing SPI Communication on the CH32V003 Microcontroller with the ADXL345 Accelerometer
0:00 Introduction to SPI: This video focuses on the SPI communication protocol using the CH32V003 microcontroller, following previous tutorials on GPIO, ADC, I2C, and Timers.
0:29 Hardware Setup: The demonstration uses a CH32V003F4P6 microcontroller, an ADXL345 3-axis accelerometer, and a debugger module, wired for 4-wire SPI communication (Chip Select, MOSI, MISO, SCLK) and USB-UART communication.
5:00 SPI vs I2C: SPI is presented as a faster, full-duplex communication protocol compared to I2C. A major advantage of SPI is the Chip Select (CS) pin, which simplifies addressing multiple identical devices on the same bus without address conflicts, unlike I2C.
8:16 SPI Initialization: The video details the process of configuring the CH32V003's SPI1 peripheral as a master, setting up the required GPIO pins (PC4 for CS, PC5 for SCLK, PC6 for MOSI, PC7 for MISO) with their alternative functions and modes (Output Push-Pull for CS, AF Push-Pull for SCLK/MOSI, Input Floating for MISO).
12:46 SPI Configuration: The SPI settings include 8-bit data size, Master mode, Two Lines Full Duplex direction, Software NSS (Chip Select), CPOL High, and CPHA 2nd edge, as required by the ADXL345 datasheet. A baud rate of 1.5MHz (48MHz/32 prescaler) is set.
17:07 SPI Transfer Function: A custom spi_transfer function is implemented to send a single 8-bit byte and simultaneously receive an 8-bit byte, handling the necessary flag checks for transmit buffer empty and receive buffer not empty.
19:50 Register Write Function: A write_register function is created to write a value to a specific register address on the ADXL345, manually pulling the CS pin low before the transaction and high afterwards, and using spi_transfer for the address and data.
21:24 Register Read Function: A read_register function is implemented to read one or multiple bytes from a starting address. It constructs a command byte by setting the read (D7) and optionally the multibyte (D6) bits, sends the command via spi_transfer, and then reads the specified number of bytes into a buffer using spi_transfer with dummy data to generate clock cycles.
27:27 ADXL345 Chip Initialization: The ADXL345 is configured by writing to its registers: setting measurement mode (0x2D=0x08), data format (0x31=0x08 for full resolution and +/-2g range), and data rate (0x2C=0x0F for 3200Hz).
31:24 Device ID Verification: Communication with the ADXL345 is verified by reading its Device ID register (0x00) and checking if the returned value is the expected 0xE5.
34:30 Testing with UART: Successful initialization and Device ID check are demonstrated by printing confirmation messages and the read value (229, which is 0xE5) to the serial terminal via the microcontroller's UART.
35:15 Reading Acceleration Data: The core acceleration data (X, Y, Z axes, each 16-bit) is read by initiating a 6-byte multi-byte read starting from register address 0x32.
36:33 Processing Raw Data: The two 8-bit bytes received for each axis are combined into a single 16-bit integer value, handling the LSB-first byte order.
38:51 Scaling for G Units: Since the microcontroller lacks native floating-point print capabilities, a trick is used to convert the raw integer values into scaled G units (multiplied by 10000) using integer arithmetic and extracting the whole and fractional parts for printing.
43:04 Printing for Plotting: The scaled acceleration values are printed to the UART in a specific comma-separated format (X:value,Y:value,Z:value\n) suitable for visualization.
44:36 Serial Plotter Demonstration: The Arduino IDE's built-in Serial Plotter is used to display the real-time X, Y, and Z acceleration data as plots, visually demonstrating how tilting the ADXL345 sensor affects the G readings.
48:40 Resources: The video concludes by mentioning other ADXL345 features (like tap detection) and directing viewers to a companion article on the creator's website and the project code on GitHub for further details.