STC15 MCU drives WS2812B colorful LED (assembly mixed programming)

Publisher:数字奇迹Latest update time:2021-07-02 Source: eefocusKeywords:STC15 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

WS2812B colorful LED has the characteristics of high integration and easy use, but the data rate of 800K (IO change rate is up to 2.4M) puts forward higher requirements on the microcontroller, which is usually driven by SPI+DMA.  


Some people also use assembly to implement it, but the data must be converted into bit storage in advance. When there are a large number of LED cascades, it will take up a lot of storage space.


I spent too much time and took many detours on driving WS2812B. I recorded them here:

  • The maximum sampling frequency of the simple digital analyzer is only 4M, that is, the measurement accuracy is 250ns, while the signal width requirement is 400ns and 850ns. The measurement result has a large error, which makes people mistakenly think that the signal jumps irregularly and
    lose their way. The solution is to use the internal frequency division function of the STC microcontroller main clock, the statement: CLK_DIV |= 0x07; to reduce the main frequency by 128 times, so that the measured waveform is accurate enough.

  • The STC15 series can use a built-in oscillation circuit with a main frequency of up to 27M. The high speed has enough time for data processing, and the final choice is a main frequency of 24M.

  • Mixed assembly and C programming, I thought it was very complicated at first, but I found it was not difficult after doing it, mainly because of the understanding of assembly itself.
    1. In terms of form, in C language, #pragma ASM and #pragma ENDASM are used to embed assembly statements;
    2. In the right-click menu of the file using the embedded statement, select Option, select Generate assembler SRC file and assemble SRC file;
    3. When calling a subroutine, the parameters are generally passed using R7, R5, and R3. You can see the SRC file generated by the compiler for details;

  • Initially, a two-layer loop nesting was used in a high-level language. The inner layer achieved 8-bit output by shifting, and the outer layer processed each bit in turn. However, the delay between the two layers of loops was long, resulting in irregular output waveforms.
    Finally, the framework of structured programming was broken, and the long signal of 850ns output in each bit was used for data processing, achieving perfect waveform output.

  • When there are a large number of LEDs, flickering is found. After a long search, it is finally determined that a hardware interrupt occurs during the signal output, causing the output waveform to jump, causing the LED at an unspecified position to flicker;

s1

Output waveform under 128 frequency division

source code:

void WS2812_SendArray(uchar xdata *pSource, uchar length) {
    EA = 0;
#pragma ASM
    CLR WS5050_DI
    MOV R1,#160
SA_DELAY25:
    DJNZ R1, SA_DELAY25
    MOV R1,#160
SA_DELAY25_2:
    DJNZ R1, SA_DELAY25_2

; R5:length, R1:8bit, R4:temporary storage A
    MOV DPH, R6
    MOV DPL, R7
    MOVX A, @DPTR
    MOV R1,#8 ;2
SA_BIT_PHASE0:
    SETB WS5050_DI ;4
    NOP ;1
    RLC A ;1
    JC SA_BIT_1 ;3
    NOP ;1
    CLR WS5050_DI ;4
    CJNE R1, #1, SA_BIT0_NOT8 ;4
; When register R1=1, it means it is the last bit, then prepare the next byte
    INC DPTR ;1
    MOVX A, @DPTR ;2
    MOV R1,#9 ;2
    DJNZ R5, SA_PHASE3 ;4
; R5=0,end
    SJMP SA_END ;3
SA_BIT0_NOT8:
; R1>1,first seven bits
    XCH A, R4 ;2
    MOVX A, @DPTR ;2
    XCH A, R4 ;2
    NOP
    SJMP SA_PHASE3 ;3
SA_BIT_1:
    CJNE R1, #1, SA_BIT1_NOT8 ;4
; When register R1=1, it means it is the last bit, then prepare the next byte
    INC DPTR ;1
    MOVX A, @DPTR ;2
    MOV R1,#9 ;2
    NOP
    DJNZ R5, SA_BIT1_9TH ;4
; R5=0, end
    SJMP SA_END ;3
SA_BIT1_NOT8:
    XCH A, R4 ;2
    MOVX A, @DPTR ;2
    XCH A, R4 ;2
    NOP
    NOP
    NOP
SA_BIT1_9TH:
    CLR WS5050_DI
    NOP ​​;1
    NOP ;1
SA_PHASE3:
    DJNZ R1, SA_BIT_PHASE0 ;4

SA_END:
    ​​SETB WS5050_DI
#pragma ENDASM
    EA = 1;
    pSource = 0;
    length = 0;
}


Keywords:STC15 Reference address:STC15 MCU drives WS2812B colorful LED (assembly mixed programming)

Previous article:Several traps that can easily cause microcontroller memory overflow
Next article:Description of MCU memory (RAM), flash, data storage and boot process

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号