1. 对于写驱动来说WS2812B芯片手册主要就是看通讯。
简单来说就是给WS2812B发送数据,数据就是RGB对应的值,先发高位,按照GRB的顺序发送数据。通常我们用高电平表示数字信号“1”,低电平表示数字信号“0”,但是WS2812B由他自己特定的1码和0码。从手册中的“数据传送时间”表中可知,1码高电平和低电平持续时间都为580ns-1us,0码高电平时间为220ns-380ns低电平时间为580ns-1us。若低电平时间持续大于280us,则说明一个周期的数据发送完毕,再发送数据则重新更新WS2812B中寄存器的值。
根据特性采用PWM波模拟通讯数据,然后根据数据修改PWM的占空比。
2. 查看STM32手册我们使用TIM3通道4产生PWM。1码时占空比为50%,0码占空比为25%,PWM周期为800KHZ,即1.25us,1码高电平和低电平均为625ns,0码高电平为313ns,低电平为937ns.
3. 准备工作做完,编写程序。
void WS2812B_TIM_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* GPIOA Configuration: TIM2 Channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //中间
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Compute the prescaler value */
//PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 90-1; // 800kHz 90-1
TIM_TimeBaseStructure.TIM_Prescaler = 1-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
/* configure DMA */
/* DMA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA1 Channel6 Config */
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM3->CCR4; // physical address of Timer 3 CCR4
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer; // this is the buffer memory
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // data shifted from memory to peripheral
DMA_InitStructure.DMA_BufferSize = 24;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // automatically increase buffer index
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // stop DMA feed after buffer size is reached
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
/* 只能使用通道1 TIMx_UP TIM_DMA_CC2 */
TIM_DMACmd(TIM3, TIM_DMA_Update, ENABLE);
}
/*
注:本来是使用注释部分的代码合成发送数据的,但是会出现,发送数据完后,最有一个电平为高电平,导致通信
异常,于是我在数据前面和最后面各增加了一个字节使电平为低电平。
参数len 表示有各级LED灯, RGB对应的颜色值。
*/
void WS_sendData(u8 len,uint32_t rgb)
{
uint8_t b = (rgb&0xff0000)>>16;
uint8_t r = (rgb&0x00ff00)>>8;
uint8_t g = (rgb&0xff);
uint16_t i = 0;
//u8 len =4;
uint16_t sendLen =len*24+2;
// for(i=0;i<8;i++)
// {
// LED_BYTE_Buffer[i] = (0x80&g)>0?TIMING_ONE:TIMING_ZERO;g <<= 1;
// }
// for(i=0;i<8;i++)
// {
// LED_BYTE_Buffer[8 + i] = (0x80&r)>0?TIMING_ONE:TIMING_ZERO;r <<= 1;
// }
// for( i=0;i<8;i++)
// {
// LED_BYTE_Buffer[16 + i] = (0x80&b)>0?TIMING_ONE:TIMING_ZERO;b <<= 1;
// }
// //LED_BYTE_Buffer[24*len]=50;
//
// while(len){
// if(len-- < 1){break;}
// for(i=0;i<24;i++)
// {
// LED_BYTE_Buffer[(24*len)+i] = LED_BYTE_Buffer[i];
// }
// }
for(i=1;i<9;i++)
{
LED_BYTE_Buffer[i] = (0x80&g)>0?TIMING_ONE:TIMING_ZERO;g <<= 1;
}
for(i=1;i<9;i++)
{
LED_BYTE_Buffer[8 + i] = (0x80&r)>0?TIMING_ONE:TIMING_ZERO;r <<= 1;
}
for( i=1;i<9;i++)
{
LED_BYTE_Buffer[16 + i] = (0x80&b)>0?TIMING_ONE:TIMING_ZERO;b <<= 1;
}
LED_BYTE_Buffer[0]=0;
LED_BYTE_Buffer[sendLen-1]=0;
while(len){
if(len-- < 1){break;}
for(i=1;i<25;i++)
{
LED_BYTE_Buffer[(24*len)+i] = LED_BYTE_Buffer[i];
}
}
DMA_SetCurrDataCounter(DMA1_Channel3, sendLen); // load number of bytes to be transferred
DMA_Cmd(DMA1_Channel3, ENABLE); // enable DMA channel 3
TIM_Cmd(TIM3, ENABLE); // enable Timer 3
while(!DMA_GetFlagStatus(DMA1_FLAG_TC3)) ; // wait until transfer complete
TIM_Cmd(TIM3, DISABLE); // disable Timer 3
DMA_Cmd(DMA1_Channel3, DISABLE); // disable DMA channel 3
DMA_ClearFlag(DMA1_FLAG_TC3); // clear DMA1 Channel 3 transfer complete flag
}
void main()
{
WS2812B_TIM_init();
while(1)
{
WS_sendData(1,0x0000ff00);
OSTimeDly(500);
WS_sendData(2,0x0000ff00);
OSTimeDly(500);
WS_sendData(3,0x0000ff00);
OSTimeDly(300);
WS_sendData(4,0x0000ff00);
OSTimeDly(500);
WS_sendData(4,0x00FFFFff);
OSTimeDly(500);
}
}