当前位置:   article > 正文

WS2812全彩RGB驱动方法_ws2812驱动程序详解

ws2812驱动程序详解

一. 简介

买了一个圆形的WS2812模块玩玩,特来总结一下驱动方法,感觉对比于普通的RGB灯来说,还是有点不一样的。
踩了一些坑,也在此列出。

二. ws2812驱动

驱动方法其实很简单,就是发送一个24bit的数据即可,数据0和1的定义分别如下。
在这里插入图片描述

三. 特别提示

  1. 它没有所谓的空闲态,如果两个24bit的数据传输时间间隔相差过大,那个第二个24bit数据,不会传递到后面的ws2812灯上,而是会更新当前ws2812灯的状态。

四. FPGA实现

整个模块的实现方式如下,欢迎关注,写的比较随便了。


module ws2812_driver(
	
	input				sys_clk_50M,
	input				rst_n,
	
	output			ws2812_o,
	
	
	
	//外部控制
	input				ws2812_req,			//显示请求
	output			ws2812_ack,		   //显示完成应答
	input				ws2812_reset,	   //显示复位
	output			ws2812_reset_ack, //显示复位完成
	input[7:0]		ws2812_r,			//显示的数据
	input[7:0]		ws2812_g,
	input[7:0]		ws2812_b
);



//逻辑0高低电平持续周期数
localparam	T0_H		=	6'd16;  //320ns
localparam	T0_L		=	6'd42;  //840ns


//逻辑1高低电平持续周期数
localparam	T1_H		=	6'd42;  //840ns
localparam	T1_L		=	6'd16;  //320ns

//复位持续周期数
localparam	T_RESET	=	14'd15000;//300us


localparam	S_IDLE		=		3'd0;
localparam	S_DATA		=		3'd1;
localparam	S_RESET		=		3'd2;
localparam	S_ACK			=		3'd3;



localparam	S_Sub_IDLE		=		4'd0;
localparam	S_Sub_T0_L		=		4'd1;
localparam	S_Sub_T0_H		=		4'd2;
localparam	S_Sub_T1_L		=		4'd3;
localparam	S_Sub_T1_H		=		4'd4;
localparam	S_Sub_ACK		=		4'd5;







reg[2:0]		state , next_state;
reg[3:0]		sub_state , sub_next_state;


reg[13:0]	t_cnt;				//时间计数
reg[5:0]		bit_cnt;				//发送比特位计数

reg[23:0]	color_rgb;




assign	ws2812_o = (sub_state == S_Sub_T0_L || sub_state == S_Sub_T1_L || state == S_RESET) ? 1'b0 : 1'b1;
assign	ws2812_ack = ( state == S_ACK ) ? 1'b1 : 1'b0;
always@(posedge sys_clk_50M or negedge rst_n)
begin
	if( rst_n == 1'b0 )
		state <= S_IDLE;
	else
		state <= next_state;
end


always@(*)
begin
	case(state)
	S_IDLE:
		if( ws2812_reset == 1'b1)
			next_state <= S_RESET;
		else if( ws2812_req == 1'b1)
			next_state <= S_DATA;
		else
			next_state <= S_IDLE;
	S_DATA:
		if( bit_cnt == 'd23)
			next_state <= S_ACK;
		else
			next_state <= S_DATA;
	S_RESET:
		if( t_cnt == T_RESET)
			next_state <= S_ACK;
		else
			next_state <= S_RESET;
	S_ACK:
		next_state <= S_IDLE;
	default: next_state <= S_IDLE;
	endcase 
end




always@(posedge sys_clk_50M or negedge rst_n)
begin
	if( rst_n == 1'b0 )
		t_cnt <= 8'd0;
	else if( state != next_state )
		t_cnt <= 8'd0;
		
	else if( state == S_DATA )
		if( sub_state == S_Sub_T0_L && t_cnt == T0_L)
			t_cnt <= 8'd0;
		else if( sub_state == S_Sub_T0_H && t_cnt == T0_H)
			t_cnt <= 8'd0;
		else if( sub_state == S_Sub_T1_L && t_cnt == T1_L)
			t_cnt <= 8'd0;
		else if( sub_state == S_Sub_T1_H && t_cnt == T1_H)
			t_cnt <= 8'd0;
		else
			t_cnt <= t_cnt + 1'b1;
	else if( state == S_RESET )
		t_cnt <= t_cnt + 1'b1;
	else
		t_cnt <= 'd0;

end




always@(posedge sys_clk_50M or negedge rst_n)
begin
	if( rst_n == 1'b0 )
		sub_state <= S_Sub_IDLE;
	else
		sub_state <= sub_next_state;

end


always@(*)
begin
	case(sub_state)
	S_Sub_IDLE:
		if( state == S_DATA && color_rgb[23] == 1'b1)
			sub_next_state <= S_Sub_T1_H;
		else if( state == S_DATA && color_rgb[23] == 1'b0)
			sub_next_state <= S_Sub_T0_H;
		else
			sub_next_state <= S_Sub_IDLE;
	S_Sub_T0_H:
		if( t_cnt == T0_H)
			sub_next_state <= S_Sub_T0_L;
		else if( state == S_ACK )
			sub_next_state <= S_Sub_IDLE;
		else
			sub_next_state <= S_Sub_T0_H;
	
	S_Sub_T0_L:
		if( t_cnt == T0_L && color_rgb[23] == 1'b0)
			sub_next_state <= S_Sub_T0_H;
		else if( t_cnt == T0_L && color_rgb[23] == 1'b1)
			sub_next_state <= S_Sub_T1_H;
		else
			sub_next_state <= S_Sub_T0_L;
				
	S_Sub_T1_H:
		if( t_cnt == T1_H)
			sub_next_state <= S_Sub_T1_L;
		else if( state == S_ACK )
			sub_next_state <= S_Sub_IDLE;
		else
			sub_next_state <= S_Sub_T1_H;
			
	S_Sub_T1_L:
		if( t_cnt == T1_L && color_rgb[23] == 1'b0)
			sub_next_state <= S_Sub_T0_H;
		else if( t_cnt == T1_L && color_rgb[23] == 1'b1)
			sub_next_state <= S_Sub_T1_H;
		else
			sub_next_state <= S_Sub_T1_L;
		
	default: sub_next_state <= S_Sub_IDLE;
	endcase

end





always@(posedge sys_clk_50M or negedge rst_n)
begin
	if( rst_n == 1'b0)
		color_rgb <= 24'd0;
	
	else if(state == S_DATA && sub_state == S_Sub_IDLE)
		color_rgb <= color_rgb << 1;
	else if( sub_state == S_Sub_T0_L && t_cnt == T0_L)
		color_rgb <= color_rgb << 1;
	else if( sub_state == S_Sub_T1_L && t_cnt == T1_L)
		color_rgb <= color_rgb << 1;
	else if( state == S_DATA)
		color_rgb <= color_rgb;
	else
		color_rgb <= {ws2812_g,ws2812_r,ws2812_b};
end



always@(posedge sys_clk_50M or negedge rst_n)
begin
	if( rst_n == 1'b0 )
		bit_cnt <= 6'd0;
	else if( sub_state == S_Sub_T0_L && t_cnt == T0_L )
		bit_cnt <= bit_cnt + 1'b1;
	else if( sub_state == S_Sub_T1_L && t_cnt == T1_L)
		bit_cnt <= bit_cnt + 1'b1;
	else if( state == S_ACK)
		bit_cnt <= 6'd0;
	else
		bit_cnt <= bit_cnt;

end


endmodule 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/537437
推荐阅读
相关标签
  

闽ICP备14008679号