CH32幻彩灯控 - WS2812及同等类型灯珠的四种驱动方式
当下ARGB(Addressable RGB)盛行,用CH32也可轻松施展灯光魔法;以CH32X035为例分析驱动WS2812幻彩LED灯珠的四种方式。
WS2812时序
WS2812是集控制电路与发光电路于一体的LED,采用单线数据协议,每24bit数据控制总线上的一颗LED。但此处的bit不等同于二进制中的位,它由自己的一段时序代表最小的信息量单位,分别为0码和1码,还有Reset码,典型时序如下图:
24bit 的数据结构为每 8bit 控制一种颜色, bit[23:16] 代表 Green 绿色,灰度为0~255, bit[15:8] 代表 Red 红色, bit[7:0] 代表 Blue 蓝色,同理灰度都为0~255;在发送数据时按照GRB的顺序高位先发。
方式一,IO翻转驱动
1、IO初始化,配置GPIO为推挽输出,初始化为低电平;
2、IO翻转的时间,通过写寄存器的方式来翻转IO电平,逻辑分析仪测量CH32X035在48M主频下的IO翻转的时间如下,高电平执行时间45ns,低电平执行时间40ns;
3、控制输出时序;
void WS2812_0(void) // 输出0码
{
/* T0H - 300ns , T0L - 600ns */
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF); // 高电平时间 45ns
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BCR = GPIO_Pin_0; // 低电平时间 40ns
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
}
void WS2812_1(void) // 输出1码
{
/* T1H - 600ns , T1L - 600ns */
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF); // 高电平时间 45ns
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BSHR = (GPIO_Pin_0 & (uint32_t)0x0000FFFF);
GPIOA->BCR = GPIO_Pin_0; // 低电平时间 40ns
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
GPIOA->BCR = GPIO_Pin_0;
}
void WS2812_Reset(void) // 输出Reset码
{
/* RES 低电平280us以上 */
GPIOA->BCR = GPIO_Pin_0;
Delay_Us(300);
}
4、调用以上函数组成IO输出的时序,控制RGB色彩;
方式二,定时器PWM+DMA驱动
1、定时器初始化,重装载值(10-1);预分频值(6-1);可得PWM频率 800K Hz,PWM选择模式1,输出极性配置为高,即低于比较值时输出有效电平,且有效电平为高;
void TIM1_PWMOut_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
TIM_OCInitTypeDef TIM_OCInitStructure={0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure );
TIM_TimeBaseInitStructure.TIM_Period = 10-1;
TIM_TimeBaseInitStructure.TIM_Prescaler = (SystemCoreClock/8000000)-1; // 800K Hz
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // PWM1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // 输出极性高
TIM_OC1Init( TIM1, &TIM_OCInitStructure );
TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Enable ); ///通道1
TIM_ARRPreloadConfig( TIM1, ENABLE );
TIM_Cmd( TIM1, ENABLE );
}
2、DMA初始化,DMA方向为内存到外设,即将比较值从内存搬运至定时器的比较寄存器;DMA传输数据宽度为半字,且不开启循环模式,
3、使能PWM输出;
4、使能DMA传输修改定时器比较值来控制PWM占空比,PWM周期为1250ns,根据每bit数据决定PWM输出的占空比,来控制输出0码(高电平30%,低电平70%)或1码(高电平50%,低电平50%);
方式三,SPI驱动
1、SPI初始化,主机模式,数据宽度8位,每字节控制WS2812的一个bit(最小时序码),即24字节控制一颗LED;
2、设置RGB的缓存,每24字节控制一颗灯珠,以输出三颗灯珠依此为为绿红蓝为例:
uint8_t SPI_Tx_Buffer[] = {
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x0F, // G - 0x01
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // R - 0x00
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // B - 0x00
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // G - 0x00
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x0F, // R - 0x01
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // B - 0x00
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // G - 0x00
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // R - 0x00
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x0F, // B - 0x01
};
3、SPI发送,输出RGB
for(uint8_t i=0;i<(24*3);i++) // 每24字节控制一颗灯珠,共3颗
{
SPI_I2S_SendData(SPI1, SPI_Tx_Buffer[i]);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) != SET);
}
Delay_Us(300); // Reset
方式四,PIOC驱动
CH32X035内嵌了一个可编程协议IO微控制器,可实现单线输出;
1、设置RGB缓存,以三颗灯珠为例;
u8 RGBpbuf[] = {
0x01, 0x00, 0x00, // G-R-B
0x00, 0x01, 0x00,
0x00, 0x00, 0x01,
};
2、输出通道初始化,若选择PC18或PC19为输出通道,则须先关闭PC18和PC19默认的 SWD 功能;
3、根据缓存输出RGB
uint16_t total_bytes;
u8* RGB_RAM;
RGB1W_Init( );
stat = 0x80;
while(1)
{
total_bytes = rgb_data_bytes;
Delay_Ms(200);
stat = 0x80;
if ( stat != 0xFF )
{
if ( stat < 0x80 )
{
if ( stat == RGB1W_ERR_OK ) printf("1-wire finished\r\n");
else printf("1-wire error %02x\r\n", stat);
stat = 0x80; // free
}
if ( stat == 0x80 && total_bytes )
{ //RAM mode for 1~3072 bytes data
stat = 0xFF; // wait
RGB_RAM = RGBpbuf;
RGB1W_SendRAM( total_bytes, RGB_RAM ,0);
total_bytes = 0;
}
}
if ( PIOC->D8_SYS_CFG & RB_INT_REQ ) { // query if disable interrupt
stat = PIOC->D8_CTRL_RD; // auto remove interrupt request after reading
}
}
总结
四种方式均可驱动WS2812及其同等类型的LED灯珠,可根据引脚资源和具体应用环境自由选择,以CH32X035为例参考程序如下:
CH32X035_WS2812.zip