WS2812自带5050灯珠,只需要一个IO口就能够驱动LED灯带,十分方便。但是,由于需要800K的PWM信号,对大部分单片机来说,压力非常大,通常单片机的硬件PWM只支持到100K左右。
好不容易在网上找了个例子,为了满足处理速度,预先将字节转化为比特,需要24字节的缓冲区,而且只能适用于1灯珠的情况。
http://www.ndiy.cn/forum.php?mod=viewthread&tid=36302&page=1#pid730724
本着DIY折腾的精神,用嵌入汇编的方式重写了驱动,最后发现更笨,需要发三次,有机会再改进。
/*发送24位字符(包含RGB信息各8位)*/
// 24M
void WS2812_SendRGB(uchar r, uchar g, uchar b) {
#pragma ASM
CLR WS5050_DI
; // [email protected]
MOV R6,#160
NEXT:
DJNZ R6,NEXT
;---- green
MOV R6,#8
MOV A,R5
R_LOOP_PHASE0:
SETB WS5050_DI
NOP
NOP
RLC A
JC R_BIT_1
CLR WS5050_DI
NOP
NOP
NOP
SJMP R_PHASE3
R_BIT_1:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
CLR WS5050_DI
R_PHASE3:
NOP
NOP
DJNZ R6,R_LOOP_PHASE0
;---- red
MOV R6,#8
MOV A,R7
G_LOOP_PHASE0:
SETB WS5050_DI
NOP
NOP
RLC A
JC G_BIT_1
CLR WS5050_DI
NOP
NOP
NOP
SJMP G_PHASE3
G_BIT_1:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
CLR WS5050_DI
G_PHASE3:
NOP
NOP
DJNZ R6,G_LOOP_PHASE0
;---- blue
MOV R6,#8
MOV A,R3
B_LOOP_PHASE0:
SETB WS5050_DI
NOP
NOP
RLC A
JC B_BIT_1
CLR WS5050_DI
NOP
NOP
NOP
SJMP B_PHASE3
B_BIT_1:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
CLR WS5050_DI
B_PHASE3:
NOP
NOP
DJNZ R6,B_LOOP_PHASE0
NOP
SETB WS5050_DI
#pragma ENDASM
r = 0;
g = 0;
b = 0;
}