Welcome to ROSMASTER R2 repository
8. RGB colorful light bar
8. RGB colorful light bar
8. RGB colorful light bar8.1. Purpose of Experiment8.2 Configuring pin information8.3. Experimental flow chart analysis8.4. Core Code Explanation8.5 Hardware Connection8.6. Experimental Effect
8.1. Purpose of Experiment
Use the SPI communication of STM32 to simulate the communication protocol of WS2812B module to drive the RGB light bar to show the effect.
8.2 Configuring pin information
- Import the ioc file from Beep's project and name it RGB_Strip.
According to the schematic diagram, we can see that the pin connected to the RGB strip is PB5. The RGB strip can be driven by timer PWM output or SPI output, and the PB5 pin supports redefining it as timer PWM or SPI output, considering the subsequent timer conflict problem, so we adopt the SPI communication method to drive the RGB strip here.

- Set the mode of SPI3 to Transmit Only Master so that the PB5 pin is automatically set to SPI3_MOSI.

- Modify the parameters of SPI3 sending data, refer to the following picture for specific parameters.

- Add DMA settings for SPI3_TX.

8.3. Experimental flow chart analysis

8.4. Core Code Explanation
- Create a new bsp_rgb.h and bsp_rgb.c, and add the following to bsp_rgb.h:

Among them, the ws2812_t structure is used to store the cache data of the light bar.
- in bsp_rgb.c file in the following new content: according to the WS2812 communication protocol, the SPI three bits to simulate the WS2812 a. SPI output three bits of the value of 0b110, corresponding to the high level of the WS2812, SPI output three bits of the value of 0b100, corresponding to the low level of the WS2812.

3.WS2812_Set_Color_One() function sets a single RGB lamp color value, index=[0, MAX_RGB-1], RGB=[0x00000000, 0x00FFFFFF], the arrangement of the RGB value: red is at the top, green in the middle, and blue at the end.

- RGB_Set_Color(index, r, g, b) and RGB_Set_Color_U32(index, color) set the RGB color value of the light bar, index=[0, MAX_RGB-1] control the corresponding lamp bead color, index=255 control the color of all lamp beads.


5.RGB_Clear() clears the RGB lamp cache.

- RGB_Update() function to refresh the RGB light bar color. RGB lights modify the color must be called after the RGB_Update() function to refresh the display, otherwise there will be no product effect.

- Send data using SPI3 DMA send.

8.5 Hardware Connection
As the RGB light bar needs to be connected to the location of the RGB light bar on the expansion board, the interface has been set up to prevent reverse connection, find the interface to see the direction of insertion can be.

8.6. Experimental Effect
After burning the program, the LED flashes every 200 milliseconds, and the RGB strip will display red, green, and blue in sequence, switching one color every 500 milliseconds.