mirror of
https://gitee.com/dlmu-cone/tronone-h7-scaffold
synced 2026-07-24 11:37:44 +08:00
add ws2812task
This commit is contained in:
42
User_Code/module/periph/ws2812/ws2812.c
Normal file
42
User_Code/module/periph/ws2812/ws2812.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @file ws2812.c
|
||||
* @brief WS2812 LED driver
|
||||
* @author TuxMonkey (nqx2004@gmail.com)
|
||||
* @version 1.0
|
||||
* @date 2025-07-08
|
||||
*
|
||||
* @copyright Copyright (c) 2025 DLMU-C.ONE
|
||||
*
|
||||
* @par 修改日志:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2025-07-08 <td>1.0 <td>TuxMonkey <td>内容
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "ws2812.h"
|
||||
|
||||
#define WS2812_LowLevel 0xC0 // 0码
|
||||
#define WS2812_HighLevel 0xF0 // 1码
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* @param [in] r My Param doc
|
||||
* @param [in] g My Param doc
|
||||
* @param [in] b My Param doc
|
||||
*/
|
||||
void WS2812_Ctrl(uint8_t r, uint8_t g, uint8_t b) {
|
||||
uint8_t txbuf[24];
|
||||
uint8_t res = 0;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
txbuf[7 - i] = (((g >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1;
|
||||
txbuf[15 - i] = (((r >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1;
|
||||
txbuf[23 - i] = (((b >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1;
|
||||
}
|
||||
HAL_SPI_Transmit(&WS2812_SPI_UNIT, &res, 0, 0xFFFF);
|
||||
while (WS2812_SPI_UNIT.State != HAL_SPI_STATE_READY);
|
||||
HAL_SPI_Transmit(&WS2812_SPI_UNIT, txbuf, 24, 0xFFFF);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
HAL_SPI_Transmit(&WS2812_SPI_UNIT, &res, 1, 0xFFFF);
|
||||
}
|
||||
}
|
||||
12
User_Code/module/periph/ws2812/ws2812.h
Normal file
12
User_Code/module/periph/ws2812/ws2812.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef WS2812_H
|
||||
#define WS2812_H
|
||||
|
||||
|
||||
#include "main.h"
|
||||
|
||||
|
||||
#define WS2812_SPI_UNIT hspi6
|
||||
extern SPI_HandleTypeDef WS2812_SPI_UNIT;
|
||||
|
||||
void WS2812_Ctrl(uint8_t r, uint8_t g, uint8_t b);
|
||||
#endif
|
||||
Reference in New Issue
Block a user