(DMA 每一个定时器周期就搬运一个0到定时器CCR中,定时器将产生一个1.25us的全低电平,40个为50us,这个50us的低电平作为ws2812的复位信号。)
#include "WS2812.h"
uint16_t RGB_buffur[RESET_PULSE + WS2812_DATA_LEN] = { 0 };
void ws2812_set_RGB(uint8_t R, uint8_t G, uint8_t B, uint16_t num)
{
//指针偏移:需要跳过复位信号的N个0
uint16_t* p = (RGB_buffur + RESET_PULSE) + (num * LED_DATA_LEN);
for (uint16_t i = 0;i < 8;i++)
{
//填充数组
p[i] = (G << i) & (0x80)?ONE_PULSE:ZERO_PULSE;
p[i + 8] = (R << i) & (0x80)?ONE_PULSE:ZERO_PULSE;
p[i + 16] = (B << i) & (0x80)?ONE_PULSE:ZERO_PULSE;
}
}
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
// HAL_TIM_PWM_Stop_DMA(&htim4,TIM_CHANNEL_4);
// HAL_TIM_PWM_Stop_DMA(&htim1,TIM_CHANNEL_1);
HAL_TIM_PWM_Stop_DMA(&htim4,TIM_CHANNEL_3);//PA8
}
void ws2812_example(void)
{
//#1.填充数组
ws2812_set_RGB(0x22, 0x00, 0x00, 0);
ws2812_set_RGB(0x00, 0x22, 0x00, 1);
ws2812_set_RGB(0x00, 0x00, 0x22, 2);
ws2812_set_RGB(0x22, 0x22, 0x22, 3);
//#2.传输数据
// HAL_TIM_PWM_Start_DMA(&htim4,TIM_CHANNEL_4,(uint32_t *)RGB_buffur,(176));
// HAL_TIM_PWM_Start_DMA(&htim1,TIM_CHANNEL_1,(uint32_t *)RGB_buffur,(176));
HAL_TIM_PWM_Start_DMA(&htim4,TIM_CHANNEL_3,(uint32_t *)RGB_buffur,(176));
//#3.延时:使效果可以被观察
HAL_Delay(500);
ws2812_set_RGB(0x22, 0x00, 0x00, 1);
ws2812_set_RGB(0x00, 0x22, 0x00, 2);
ws2812_set_RGB(0x00, 0x00, 0x22, 3);
ws2812_set_RGB(0x22, 0x22, 0x22, 0);
// HAL_TIM_PWM_Start_DMA(&htim4,TIM_CHANNEL_4,(uint32_t *)RGB_buffur,(176));
// HAL_TIM_PWM_Start_DMA(&htim1,TIM_CHANNEL_1,(uint32_t *)RGB_buffur,(176));
HAL_TIM_PWM_Start_DMA(&htim4,TIM_CHANNEL_3,(uint32_t *)RGB_buffur,(176));
HAL_Delay(500);
} |