Electronic module|External control integrated LED light source WS2812 module---hardware introduction and stm32 driver

Module Introduction

WS2812 is an intelligent external control LED light source integrating control circuit and light emitting circuit. Its appearance is the same as a 5050LED lamp bead, and each element is a pixel. The pixel contains an intelligent digital interface data latch signal shaping and amplifying drive circuit, and also includes a high-precision internal oscillator and a 12V high-voltage programmable constant current control part, which effectively ensures that the color of the pixel light is highly consistent.

The data protocol adopts the single-line return-to-zero code communication method. After the pixel is powered on and reset, the DIN terminal receives the data transmitted from the controller. The 24bit data sent first is extracted by the first pixel and sent to the inside of the pixel. The remaining data is reshaped and amplified by the internal shaping processing circuit, and then forwarded and output to the next cascaded pixel through the DO port. Every time a pixel is transmitted, the signal is reduced by 24 bits. Pixels adopt automatic shaping and forwarding technology, so that the number of cascaded pixels is not limited by signal transmission, but only limited by signal transmission speed requirements.

LED has the advantages of low voltage drive, environmental protection and energy saving, high brightness, large scattering angle, good consistency, ultra-low power, and ultra-long life. The control circuit is integrated on the LED, the circuit becomes simpler, the volume is smaller, and the installation is easier.

Physical map:
insert image description here

The main application areas include :

  • LED full-color luminous string lights, LED full-color modules, LED full-color soft light strips and hard light strips, LED guardrail tubes.
  • LED point light source, LED pixel screen, LED special-shaped screen, various electronic products, electrical equipment marquee.

Module Features

  • The control circuit and the RGB chip are integrated in a 5050-package component to form a complete external control pixel.
  • Built-in signal shaping circuit, after any pixel receives the signal, it will be output after waveform shaping to ensure that the line waveform distortion will not accumulate.
  • Built-in power-on reset and power-off reset circuits
  • The three primary colors of each pixel can realize 256 levels of brightness display, complete the full true color display of 16,777,216 colors, and the scanning frequency is not lower than
    400Hz/s
  • Serial cascading interface, which can complete data receiving and decoding through one signal line
  • There is no need to add any circuit when the transmission distance between any two points does not exceed 5 meters.
  • When the refresh rate is 30 frames per second, the cascaded number of low-speed mode is not less than 512 points, and the high-speed mode is not less than 1024 points
  • Data sending speed up to 800Kbps
  • Highly consistent light color, high cost performance

Mechanical Dimensions

insert image description here
Hardware pin definition:

insert image description here

Terminal function
insert image description here
Maximum rating
insert image description here
LED characteristic parameters
insert image description here
Typical application circuit
insert image description here
The principle is as follows: the physical diagram above
a WS2812B schematic image is a module composed of four WS2812B, then the schematic diagram is as follows
insert image description here

insert image description here

Single-line return-to-zero code communication method

Data transfer time ( TH+TL=1.25 µs ±600ns )
insert image description here

Timing Waveform

insert image description here

connection method:

insert image description here
Data transmission method:
insert image description here
where D1 is the data sent by the MCU, and D2, D3, D4 are the data automatically reshaped and forwarded by the cascade circuit.

24bit data structure

insert image description here
High bit first, send data in the order of GRB

stm32 driver

The time accuracy required for WS2812B encoding is ns level, and the timer interrupt is not suitable; the clock cycle of stm32 is 13.89ns under the 72mhz system clock, and it can also be achieved by using systemtick as delay, but the time will not be very accurate and needs to be considered The impact of different instruction cycles on coding is rough and cumbersome.

The 0/1 code of WS2812B is very similar to the waveform of different duty ratios in one cycle, PWM control can be considered

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_GPIOA, ENABLE);
    /* GPIOA Configuration: TIM2 Channel 1 as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    /* Compute the prescaler value */
    //PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 89; // 800kHz
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &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(TIM2, &TIM_OCInitStructure);

    /* configure DMA */
    /* DMA clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    /* DMA1 Channel6 Config */
    DMA_DeInit(DMA1_Channel2);

    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM2->CCR1;	// physical address of Timer 3 CCR1
    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_Channel2, &DMA_InitStructure);

    /* TIM3 CC1 DMA Request enable */
	/* 只能使用通道1 TIMx_UP */
    TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
}
void send_Data(uint32_t rgb)
{
    
    

	uint8_t r = (rgb&0xff0000)>>16;
	uint8_t g = (rgb&0x00ff00)>>8;
	uint8_t b = (rgb&0xff);
	for(uint16_t i=0;i<8;i++){
    
    
		LED_BYTE_Buffer[i] = (0x80&g)>0?TIMING_ONE:TIMING_ZERO;g <<= 1;
	}
	for(uint16_t i=0;i<8;i++){
    
    
		LED_BYTE_Buffer[8 + i] = (0x80&r)>0?TIMING_ONE:TIMING_ZERO;r <<= 1;
	}
	for(uint16_t i=0;i<8;i++){
    
    
		LED_BYTE_Buffer[16 + i] = (0x80&b)>0?TIMING_ONE:TIMING_ZERO;b <<= 1;
	}
	DMA_SetCurrDataCounter(DMA1_Channel2, 24); 	// load number of bytes to be transferred
    DMA_Cmd(DMA1_Channel2, ENABLE); 			// enable DMA channel 6
    TIM_Cmd(TIM2, ENABLE); 						// enable Timer 3
    while(!DMA_GetFlagStatus(DMA1_FLAG_TC2)) ; 	// wait until transfer complete
	TIM_Cmd(TIM2, DISABLE); 					// disable Timer 3
    DMA_Cmd(DMA1_Channel2, DISABLE); 			// disable DMA channel 6
    DMA_ClearFlag(DMA1_FLAG_TC2); 				// clear DMA1 Channel 6 transfer complete flag
}

After that, if you want the light to be send_Dataon, you can use the function directly.
The generated data is the brightness value of GRB.
For example, if you want bright green, it is 0xff0000,
and white is 0xffffff, and it is
not bright, it is 0x000000.

If several lamps are connected in series, send them continuously,
send_Data(0xff0000)
send_Data(0x000000)
send_Data(0x000000)
send_Data(0x000000)
The next command of the four lamps requires an interval greater than 24us.

For example, the result of calling the light on like this
insert image description here
:
insert image description here
too bright.

Guess you like

Origin blog.csdn.net/qq_32761549/article/details/130129577