1结构
实物
内部结构
2引脚与接线
电压特性
引脚意思
脚号 | 符号 | 管脚名 | 功能 |
2 | DO(DOUT) | 数据输出 | 控制信号输出 |
3 | GND | 地 | 接地 |
4 | DI(DIN) | 数据输入 | 控制信号输入 |
1 | VDD | 电源 | 供电管脚 |
多个如何接线
3数据传输方法
是如何控制多个的
在硬件连接上,只需将前一个WS2812B的"DOUT"连接到下一个WS2812B的"DIN",即可实现多个灯珠的串接控制。
每个灯珠需要24bit的控制数据,超过24bit的数据会通过"DOUT"流到下一个灯珠
控制一个和控制多个的区别
控制一个WS2812B灯珠的数据结构:
当控制一个WS2812B灯珠时,数据缓冲区仅需要容纳24位数据(每个颜色通道8位,即GRB格式)。数据缓冲区可以定义为:
uint8_t buffer[DATA_SIZE]; // DATA_SIZE = 24
控制两个WS2812B灯珠的数据结构:
当控制两个WS2812B灯珠时,数据缓冲区需要容纳两个灯珠的数据,即48位数据(每个灯珠24位)。数据缓冲区可以定义为:
uint8_t buffer[2 * DATA_SIZE]; // DATA_SIZE = 24, 所以2 * DATA_SIZE = 48
24bit 数据结构
G7 G6 G5 G4 G3 G2 G1 G0 R7 R6 R5 R4 R3 R2 R1 R0 B7 B6 B5 B4 B3 B2 B1 B0
注:高位先发,按照GRB的顺序发送数据。
数据传输时间
手册上介绍数据发送速度是:800Kbps
所以每个数据位的时间是:1/800000=0.00000125s=1.25us
这1.25us可以表示高位或低位,24个1.25us就是一个灯的颜色,发完一个灯的颜色后需要发送大于280us的低电平让数据从锁存器表现在灯上。
数位位0:
周期1/3的高电平,1.25*(1/3)us
周期2/3的低电平,1.25*(2/3)us
数位位1:
周期2/3的高电平,1.25*(1/3)us
周期1/3的低电平,1.25*(1/3)us
每个LED灯珠都期望接收一个24位的数据流(每个颜色通道8位,即G-R-B格式),数据传输的规则如下:
-
位传输时间:每个位的传输时间是固定的,通常大约为1.25微秒(us)。
-
0和1的编码:
- 0:逻辑"0"的编码是高电平持续约0.4微秒,随后是低电平持续约0.85微秒。
- 1:逻辑"1"的编码是高电平持续约0.8微秒,随后是低电平持续约0.45微秒。
-
颜色数据顺序:数据传输的顺序是先发送最低有效位(LSB),然后依次发送更高的位,对于颜色数据,首先是绿色(G),然后是红色(R),最后是蓝色(B)。
-
数据帧间隔:在连续的数据帧之间,需要有一个至少50微秒的低电平间隔,以标识数据帧的结束和下一个数据帧的开始。
-
传输开始前的准备:在开始传输之前,数据线应该处于低电平状态,以确保所有LED灯珠准备好接收数据。
-
传输结束后的复位:传输完成后,需要保持数据线至少50微秒的低电平,以复位WS2812B并显示最后传输的颜色。
基于这些规则,以下是控制WS2812B灯珠所需的基本时间计算:
- 单个灯珠的传输时间:24位 * 1.25微秒/位 = 30微秒。
- 多个灯珠的传输时间:对于N个灯珠,总的传输时间是N * 30微秒。
4实例代码
思路1
tim1+pwm+dma (以下使用的是stm32f407vet6)
TIM1 PWM
DMA
tim.文件
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.c
* @brief This file provides code for the configuration
* of the TIM instances.
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
TIM_HandleTypeDef htim1;
DMA_HandleTypeDef hdma_tim1_ch1;
/* TIM1 init function */
void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 209-1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
HAL_TIM_MspPostInit(&htim1);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspInit 0 */
/* USER CODE END TIM1_MspInit 0 */
/* TIM1 clock enable */
__HAL_RCC_TIM1_CLK_ENABLE();
/* TIM1 DMA Init */
/* TIM1_CH1 Init */
hdma_tim1_ch1.Instance = DMA2_Stream1;
hdma_tim1_ch1.Init.Channel = DMA_CHANNEL_6;
hdma_tim1_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim1_ch1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim1_ch1.Init.MemInc = DMA_MINC_ENABLE;
hdma_tim1_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_tim1_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_tim1_ch1.Init.Mode = DMA_CIRCULAR;
hdma_tim1_ch1.Init.Priority = DMA_PRIORITY_LOW;
hdma_tim1_ch1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_tim1_ch1) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(tim_baseHandle,hdma[TIM_DMA_ID_CC1],hdma_tim1_ch1);
/* USER CODE BEGIN TIM1_MspInit 1 */
/* USER CODE END TIM1_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspPostInit 0 */
/* USER CODE END TIM1_MspPostInit 0 */
__HAL_RCC_GPIOE_CLK_ENABLE();
/**TIM1 GPIO Configuration
PE9 ------> TIM1_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/* USER CODE BEGIN TIM1_MspPostInit 1 */
/* USER CODE END TIM1_MspPostInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspDeInit 0 */
/* USER CODE END TIM1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM1_CLK_DISABLE();
/* TIM1 DMA DeInit */
HAL_DMA_DeInit(tim_baseHandle->hdma[TIM_DMA_ID_CC1]);
/* USER CODE BEGIN TIM1_MspDeInit 1 */
/* USER CODE END TIM1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
主要代码
解析RGB数据
double Hight_Data = 100; //1 (166.4~96.5)
double Low_Data = 50; //0 (36.6~63.2)
int RGB_buffur[300+24]={0};//颜色数据缓冲区 总值324 其中300为复位(280μs以上)
//解析RGB数据
void RGB_cai_1(int RGB){
//清零
for(int i;i<sizeof(RGB_buffur);i++){
RGB_buffur[i]=0;
}
//G 第一次拆分:处理最高8位
for (int i = 0; i < 8; ++i) {
RGB_buffur[i] = ((RGB >> (16 - i)) & 1) ? Hight_Data : Low_Data; // 从最高位开始,向右移动并取最低位
}
//R 第二次拆分:处理中间8位
for (int i = 0; i < 8; ++i) {
RGB_buffur[8 + i] = ((RGB >> (24 - i)) & 1) ? Hight_Data : Low_Data; // 从第17位开始,向右移动并取最低位
}
//B 第三次拆分:处理最低8位
for (int i = 0; i < 8; ++i) {
RGB_buffur[16 + i] = ((RGB >> (8 - i)) & 1) ? Hight_Data : Low_Data; // 从第9位开始,向右移动并取最低位
}
}
将数据写入 ws2812中
RGB_cai_1(0x24D400); //将颜色数据写入
HAL_TIM_PWM_Start_DMA(&htim1,TIM_CHANNEL_1,(uint32_t*)RGB_buffur,324);//实现点亮一个LED
main.c文件
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
double Hight_Data = 100; //1 (166.4~96.5)
double Low_Data = 50; //0 (36.6~63.2)
int RGB_buffur[300+24]={0};//颜色数据缓冲区 总值324 其中300为复位(280μs以上)
//解析RGB数据
void RGB_cai_1(int RGB){
//清零
for(int i;i<sizeof(RGB_buffur);i++){
RGB_buffur[i]=0;
}
//G 第一次拆分:处理最高8位
for (int i = 0; i < 8; ++i) {
RGB_buffur[i] = ((RGB >> (16 - i)) & 1) ? Hight_Data : Low_Data; // 从最高位开始,向右移动并取最低位
}
//R 第二次拆分:处理中间8位
for (int i = 0; i < 8; ++i) {
RGB_buffur[8 + i] = ((RGB >> (24 - i)) & 1) ? Hight_Data : Low_Data; // 从第17位开始,向右移动并取最低位
}
//B 第三次拆分:处理最低8位
for (int i = 0; i < 8; ++i) {
RGB_buffur[16 + i] = ((RGB >> (8 - i)) & 1) ? Hight_Data : Low_Data; // 从第9位开始,向右移动并取最低位
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
RGB_cai_1(0x24D400); //将颜色数据写入
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_TIM_PWM_Start_DMA(&htim1,TIM_CHANNEL_1,(uint32_t*)RGB_buffur,324);//实现点亮一个LED
HAL_Delay(2000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
注意
pwm的周期为1.25us 调节占空比使其符合 1 0 的时间周期即可
PWM周期为1.25us相当于频率为800kHz。
思路2
用定时器实现延迟
思路3
使用spi实现写数据
【经验分享】STM32 HAL库使用SPI+DMA驱动WS2812优化方案 - STM32团队 ST意法半导体中文论坛