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;
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;
}
Previous article:Several traps that can easily cause microcontroller memory overflow
Next article:Description of MCU memory (RAM), flash, data storage and boot process
- Popular Resources
- Popular amplifiers
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Microcontroller Principles and Application Project Tutorial - Based on STC15W4K32S4 Series Microcontroller
- STC15 series emulation MCU project application tutorial (C language)
- MCU Principles and Interface Technology C51 Programming (Edited by Zhang Yigang)
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- 8N60MOS field effect tube product details, original supply, high practical value
- Award-winning live broadcast | Rochester Electronics explains in detail: Challenges and solutions after semiconductor production shutdown
- C language structure pointer definition problem help
- Newbie help, how to modify the contents of a file with the suffix bin
- From fail to pass, what does DDR debugging go through?
- Understand 20 types of analog circuits in one article
- Human life status monitoring controller based on GD32E231
- 【TI recommended course】#2015 Power Supply Design Seminar: Control Problems of Low-Power AC/DC Converters (2)#
- A bluetooth printer implementation solution
- Principle and application of single-chip switching power supply VIPer53