【Verilog】FPGA控制RGB灯WS2812B

【Verilog】FPGA控制RGB灯WS2812B

最近学业繁忙,因此好久没有更新博客,今日闲来无事,准备开始整理一下最近写的东西
今天先更新一下短学期做的东西吧——FPGA控制RGB灯WS2812B
好久没接触Verilog和FPGA了,所以做的可能有点烂,望读者勿怪,小弟上传只是为了记录和分享一下最近做的东西。

设计目标

在这里插入图片描述

代码

老师提供了灯的驱动代码:

  1. TOP.v
module TOP(
	input	clk,
	input	rst_n,
	output 	RZ_data
);
//-------------------------------//
wire 	tx_en;
wire 	tx_done;
wire 	[23:0]	RGB;
//-------------------------------//

//-------------------------------//
RGB_Control		RGB_Control_inst0(
	.clk		(clk),
	.rst_n		(rst_n),
	.tx_done	(tx_done),
	.tx_en		(tx_en),	
	.RGB		(RGB)
);
//-------------------------------//
RZ_Code			RZ_Code_inst1(
	.clk		(clk),
	.rst_n		(rst_n),
	.RGB		(RGB),
	.tx_en		(tx_en),	
	.tx_done	(tx_done),
	.RZ_data	(RZ_data)
);

endmodule

  1. RGB_Control.v
module RGB_Control(
	input	clk,
	input	rst_n,
	input	tx_done,		//一帧24bit)数据结束标志?	output	tx_en,			//发送数据使能?	output	reg	[23:0]	RGB
);

//-------------接口信号------------//
reg    [31:0]	cnt;
reg 	[23:0]	RGB_reg	[4:0];	//存RGB数据的数据?
reg 	[3:0]	k;				
reg 	tx_en_r;
//--------------------------------//
reg 	tx_done_r0;
always @(posedge clk or negedge rst_n) begin
	if(!rst_n)	begin
		tx_done_r0 <= 0;
	end
	else	begin
		tx_done_r0 <= tx_done;
	end
end
//--------------------------------//

always @(posedge clk or negedge rst_n) begin
	if(!rst_n)	begin
		k <= 0;
		RGB <= 0;
		RGB_reg	[0]	<= 24'b11111111_00000000_11111111;
		RGB_reg	[1]	<= 24'b00000000_11111111_00000000;
		RGB_reg	[2]	<= 24'b10101010_01010101_10101010;
		RGB_reg	[3]	<= 24'b10100101_01000011_11010101;
		RGB_reg	[4]	<= 24'b10100101_01000011_11010101;
	end
	else if(tx_en_r)	begin   
		case (k)
			4'd0,4'd1,4'd2,4'd3:
				if(tx_done_r0)	begin
					RGB <= RGB_reg[k];
					k <= k + 1;
				end
			4'd4:
				if(tx_done)	begin
					RGB <= RGB_reg[0];
					k <= 0;
				end
		  	default: k <= 0;
		endcase
	end
	else ;
end
//--------------------------------//

//-------------计数�?------------//
always @(posedge clk) begin
	if((!rst_n) || (tx_en_r))	begin
		cnt <= 0;
	end
	else if(cnt == 32'd14999)	begin	//RESET时间(300us*50M=15000)
		cnt <= cnt;
	end
	else	begin
		cnt <= cnt + 1;
	end
end
//--------------------------------//

//--------------------------------//
always @(posedge clk or negedge rst_n) begin
	if(!rst_n)	begin
		tx_en_r <= 0;
	end
	else if((k == 4'd4) && (tx_done))	begin
		tx_en_r <= 0;			//一组数据发送结束,RESTE
	end
	else if((cnt == 32'd14999) && tx_done)	begin
		tx_en_r <= 1;			//重新开始发送
	else	begin
		tx_en_r <= tx_en_r;
	end
end

assign	tx_en = tx_en_r;
//--------------------------------//

endmodule

  1. RZ_Code.v
/
//	输入数据为RGB数据(24bit)
//	输出数据为单极性归零码(Return Zero Code)
/

module RZ_Code(
	input	clk,
	input	rst_n,
	input	[23:0]	RGB,	//按照GRB的顺序排列
	input	tx_en,			//发送数据使能
	output	tx_done,		//一帧(24bit)数据结束标志
	output	RZ_data
);

//-----------------接口信号----------------//
reg 	[31:0]	cnt;		//计数一个码元周期
reg 	[4:0]	i;			//计数24bit
reg 	symbol;				//1bit数据结束标志
reg 	RGB_RZ;				//需要发送的RGB数据
reg		data_out;			//转换后的单极性归零码

//reg 	tx_done_sig;
//----------------------------------------//

//-----------------计数器-----------------//
always @(posedge clk or negedge rst_n) begin
	if(!rst_n)	begin
		cnt <= 0;
	end
	else if(cnt == 32'd62)	begin		//计数一个码元周期(62.5=1.25us * 50M)
		cnt <= 32'd0;
	end
	else	begin
		cnt <= cnt + 1'b1;
	end
end
always @(posedge clk or negedge rst_n) begin
	if(!rst_n)	begin
		symbol <= 0;
	end
	else if(cnt == 32'd61)	begin		//码元周期结束
		symbol <= 1;
	end
	else	begin
		symbol <= 0;
	end
end
//---------------------------------------//
assign	tx_done = symbol && (i == 5'd23);


//assign	tx_done = tx_done_sig;


//--------------循环发送一帧--------------//
always @(posedge clk or negedge rst_n) begin
	if(!rst_n)	begin
		i <= 0;
		RGB_RZ <= 0;
	end
	else	begin
		case (i)
			5'd0,5'd1,5'd2,5'd3,5'd4,5'd5,5'd6,5'd7,5'd8,5'd9,5'd10,5'd11,5'd12,5'd13,5'd14,5'd15,5'd16,5'd17,5'd18,5'd19,5'd20,5'd21,5'd22,5'd23:
				if(symbol)	begin
					i <= i + 1;
					RGB_RZ <= RGB[23 - i];		//从高位到低位赋值
				end
				else	begin
					i <= i;
					RGB_RZ <= RGB_RZ;
				end
		  	default:	i <= 0;

		endcase
	end
end
//---------------------------------------//

//-------------单极性归零码---------------//
always @(posedge clk ) begin
	if((!rst_n) || (!tx_en))	begin
		data_out <= 0;
	end
	else if((RGB_RZ == 0) && (tx_en == 1))	begin
		if(cnt <= 32'd15)	begin		//零码高电平,0.3us*50M=15
			data_out <= 1;
		end
		else	begin
			data_out <= 0;
		end
	end
	else if((RGB_RZ == 1) && (tx_en == 1))	begin
		if(cnt <= 32'd45)	begin		//一码高电平,0.9us*50M=45
			data_out <= 1;
		end
		else	begin
			data_out <= 0;
		end
	end
	else	begin
		data_out <= data_out;
	end
end
//-----------------------------------------//
assign	RZ_data = data_out;

endmodule

把以上部分烧录进FPGA并分配好引脚,便可以简单地让彩灯带亮起来
接下来分享一下我通过按键控制彩灯带。
因为时间以及能力有限,故效果不是十分理想,比如没有消抖导致需要长按按键才能让灯带进行闪烁等等

  1. RGB_Control.v
    这是我修改过的,我将按键的交互、状态的改变、时间的控制都揉合到了这个v文件里了
module RGB_Control(
	input	clk_RGB_Control,
	input	rst_n,
	input [1:0] sel_2 ,
	input [1:0] sel_1 ,
	input	tx_done,		//一帧(24bit)数据结束标志,
	output	tx_en,			//发送数据使能
	output	reg	[23:0]	RGB
);

//-------------接口信号------------//
reg	[31:0]	cnt;
reg 	[4:0]	k;				
reg 	tx_en_r;                   // tx_en_r发送数据控制使能
reg   [23:0]	RGB_reg	[8:0] ;  //存RGB数据的数组
reg   [1:0]  flag_smg ;
//--------------------------------//
//控制一帧(24bit)数据结束
//当复位时,清零
//否则,信号由RZ_Code模块输出tx_done提供
reg 	tx_done_r0;                
always @(posedge clk_RGB_Control or negedge rst_n) begin
	if(!rst_n)	begin
		tx_done_r0 <= 0;
	end
	else	begin
		tx_done_r0 <= tx_done;
	end
end

//--------------------------------//
//状态选择
reg [1:0]   mode_1 ;
reg [7:0]   mode_2 ;
reg         flag_1;
reg         flag_2;
reg [1:0]   time_flag_1 ;
reg [3:0]   time_flag_2 ;
reg [32:0]  Count1 ;
reg [32:0]  Count2 ; 
reg [32:0]  t ;
reg [9:0]   mode ;



//------------------------------------//


//----------------速度选择--------------------// t = 时间 * 5_000_0000
always @(*)  begin

      if (!sel_1[0])   begin  
		    
		                t <= 33'd4999_999 ; //  T = 0.1S  
							 										 
								
		end
		
		else if (!sel_1[1])   begin
		    
                      t <= 33'd24999_9999 ;  //  T = 5S
							 					 
		
		end
		
		else  begin
		
                      t = 33'd49999_999 ;   // T = 1S
							 					 
							 
		end
			
end


//-------------------------------//




//-----------模式控制模块-----------//
always @(*)  begin

      if (!sel_2[0])   begin
		    
		                flag_1 <= 0 ;
							 
							 flag_2 <= 1 ;							 
		
		end
		
		else if (!sel_2[1])   begin
		    
		                flag_1 <= 1 ;
							 
							 flag_2 <= 0 ;
		
		end
		
		else  begin     
                      flag_1 <= 0 ;	
							 
		                flag_2 <= 0 ;

		end
			
end


//-------------时间控制模块计数器----------------------//
always @(posedge clk_RGB_Control) begin 

      if(flag_1) begin
		
		    if(Count1 == t)      Count1 <= 0 ;
			 
			 else                 Count1 <= Count1 + 1 ;
			 
		end
		
		else                     Count1 <= 0 ;	

end

always @(posedge clk_RGB_Control) begin 

      if(flag_2) begin
		
		    if(Count2 == 8 * (t+1))    Count2 <= 0 ;
			 
			 else                       Count2 <= Count2 + 1 ;
			 
		end	
		
		else                           Count2 <= 0 ;
		

end

//--------------状态模块----------------------//
always @(posedge clk_RGB_Control) begin
     if     (Count1 == 0)                   time_flag_1 <= 2'd0 ;
  
	  else if(Count1 == 5)                   time_flag_1 <= 2'd1 ;
	  
	  else if(Count1 == (t+1)/2)             time_flag_1 <= 2'd2 ;
  
end

always @(posedge clk_RGB_Control) begin
     if     (Count2 == 0)                   time_flag_2 <= 16'd0 ;
  
	  else if(Count2 == 10)                  time_flag_2 <= 16'd1 ;
	  
	  else if(Count2 == t)                   time_flag_2 <= 16'd2 ;
	  
	  else if(Count2 == 2 * (t+1)-1)         time_flag_2 <= 16'd3 ; 
	  
     else if(Count2 == 3 * (t+1)-1)         time_flag_2 <= 16'd4 ; 
	  
	  else if(Count2 == 4 * (t+1)-1)         time_flag_2 <= 16'd5 ; 
	   
	  else if(Count2 == 5 * (t+1)-1)         time_flag_2 <= 16'd6 ;
	  
	  else if(Count2 == 6 * (t+1)-1)         time_flag_2 <= 16'd7 ; 
	  
	  else if(Count2 == 7 * (t+1)-1)         time_flag_2 <= 16'd8 ; 
  
end

//----------模式的状态变化----------//
always @(*) begin
     if     (time_flag_1 == 2'd0)    mode_1 <= 2'b00 ;

	  else if(time_flag_1 == 2'd1)    mode_1 <= 2'b01 ;
	  
	  else if(time_flag_1 == 2'd2)    mode_1 <= 2'b10 ;
 
end



always @(posedge clk_RGB_Control) begin
     if     (time_flag_2 == 16'd0)   mode_2 <= 8'b00000000 ;
   
	  else if(time_flag_2 == 16'd1)   mode_2 <= 8'b00000001 ;
	  
	  else if(time_flag_2 == 16'd2)   mode_2 <= 8'b00000010 ; 
	  
	  else if(time_flag_2 == 16'd3)   mode_2 <= 8'b00000100 ; 
	  
     else if(time_flag_2 == 16'd4)   mode_2 <= 8'b00001000 ; 
	  
	  else if(time_flag_2 == 16'd5)   mode_2 <= 8'b00010000 ; 
	  
	  else if(time_flag_2 == 16'd6)   mode_2 <= 8'b00100000 ;  
	  
	  else if(time_flag_2 == 16'd7)   mode_2 <= 8'b01000000 ; 
	  
	  else if(time_flag_2 == 16'd8)   mode_2 <= 8'b10000000 ; 
  
end

//------------------------------各个模式具体的状态--------------------------------//
//=================1. 闪烁+绿色单色移位循环================//
/*
always @(posedge clk_RGB_Control) begin

     mode <= {mode_2, mode_1} ;
	  
// 如果count1等于多少多少的时候,RGB_reg等于多少多少,就达到了固定时间切换状态的效果
     case(mode)    
	   10'b0000000001 :  begin
	   RGB_reg	[0]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[1]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[2]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[3]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[4]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[5]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[6]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[7]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[8]	<= 24'b11111111_11111111_11111111;
		end

      10'b0000000010 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		// 绿色单色移位循环
		10'b0000000100 :  begin
	  	RGB_reg	[0]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0000001000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0000010000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0000100000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0001000000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0010000000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0100000000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b1000000000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000; // 白
		end
		endcase
	
end
*/
//===============================================//

//=================2. 闪烁+红绿蓝白移位循环================//
/*
always @(posedge clk_RGB_Control) begin

     mode <= {mode_2, mode_1} ;
	  
// 如果count1等于多少多少的时候,RGB_reg等于多少多少,就达到了固定时间切换状态的效果
     case(mode)    
	   10'b0000000001 :  begin
	   RGB_reg	[0]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[1]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[2]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[3]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[4]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[5]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[6]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[7]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[8]	<= 24'b11111111_11111111_11111111;
		end

      10'b0000000010 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end

      //红绿蓝白红绿蓝白循环
		10'b0000000100 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_11111110_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0000001000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b11111110_00000000_00000000;  
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0000010000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_11111110;  
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0000100000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b11111110_11111110_11111110;  
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0001000000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_11111110_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0010000000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000;  
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b11111110_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0100000000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000;  
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_11111110; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b1000000000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000;  
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b11111110_11111110_11111110; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		endcase
		
end
*/
//===================================================//

//=================3. 闪烁+七彩色移位循环================//

always @(posedge clk_RGB_Control) begin

     mode <= {mode_2, mode_1} ;
	  
// 如果count1等于多少多少的时候,RGB_reg等于多少多少,就达到了固定时间切换状态的效果
     case(mode)    
	   10'b0000000001 :  begin
	   RGB_reg	[0]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[1]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[2]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[3]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[4]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[5]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[6]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[7]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[8]	<= 24'b11111111_11111111_11111111;
		end

      10'b0000000010 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[1]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[2]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[3]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[4]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[5]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[6]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[7]	<= 24'b00000000_00000000_00000000; 
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end

      //红绿蓝白红绿蓝白循环
		10'b0000000100 :  begin
	   RGB_reg	[0]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[1]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[2]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[3]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[4]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[5]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[6]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[7]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[8]	<= 24'b11111111_11111111_11111111;
		end
		
		10'b0000001000 :  begin
	  	RGB_reg	[0]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[1]	<= 24'b00000000_11111110_00000000; // 红 
		RGB_reg	[2]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[3]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[4]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[5]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[6]	<= 24'b00000000_00000000_11111110; // 蓝 
		RGB_reg	[7]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0000010000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[1]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[2]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[3]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[4]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[5]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[6]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[7]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0000100000 :  begin
	  	RGB_reg	[0]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[1]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[2]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[3]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[4]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[5]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[6]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[7]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0001000000 :  begin
	  	RGB_reg	[0]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[1]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[2]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[3]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[4]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[5]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[6]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[7]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0010000000 :  begin
	  	RGB_reg	[0]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[1]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[2]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[3]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[4]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[5]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[6]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[7]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b0100000000 :  begin
	  	RGB_reg	[0]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[1]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[2]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[3]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[4]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[5]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[6]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[7]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		
		10'b1000000000 :  begin
	  	RGB_reg	[0]	<= 24'b11001101_11111110_00000000; // 橙
		RGB_reg	[1]	<= 24'b11111110_11111110_00000000; // 黄
		RGB_reg	[2]	<= 24'b11111110_00000000_00000000; // 绿
		RGB_reg	[3]	<= 24'b11111110_00000000_11111110; // 青
		RGB_reg	[4]	<= 24'b00000000_00000000_11111110; // 蓝
		RGB_reg	[5]	<= 24'b00000000_11001100_11001100; // 紫
		RGB_reg	[6]	<= 24'b11111110_11111110_11111110; // 白
		RGB_reg	[7]	<= 24'b00000000_11111110_00000000; // 红
		RGB_reg	[8]	<= 24'b00000000_00000000_00000000;
		end
		endcase
		
end

//===================================================//


//===================================================//

always @(posedge clk_RGB_Control or negedge rst_n) begin
	if(!rst_n)	begin
		k <= 0;
		RGB <= 0;
	end
	else if(tx_en_r)	begin    //tx_en_r发送数据控制使能
		case (k)
			5'd0,5'd1,5'd2,5'd3,5'd4,5'd5,5'd6,5'd7:
				if(tx_done_r0)	begin
					RGB <= RGB_reg[k];
					k <= k + 1;
				end
			5'd8:
				if(tx_done)	begin
					RGB <= RGB_reg[0];
					k <= 0;
				end
		  	default: k <= 0;
		endcase
	end
	else ;
end
//--------------------------------//

//-------------计数器-------------//
always @(posedge clk_RGB_Control) begin
	if((!rst_n) || (tx_en_r))	begin  //tx_en_r发送数据控制使能
		cnt <= 0;
	end
	else if(cnt == 32'd14999)	begin	//RESET时间(300us*50M=15000)
		cnt <= cnt;
	end
	else	begin
		cnt <= cnt + 1;
	end
end
//--------------------------------//

//--------------------------------//
always @(posedge clk_RGB_Control or negedge rst_n) begin   //tx_en_r 发送数据控制使能
	if(!rst_n)	begin
		tx_en_r <= 0;
	end
	else if((k == 5'd8) && (tx_done))	begin
		tx_en_r <= 0;			//一组数据发送结束,RESTE
	end
	else if((cnt == 32'd14999) && tx_done)	begin
		tx_en_r <= 1;			//重新开始发送
	end
	else	begin
		tx_en_r <= tx_en_r;
	end
end

assign	tx_en = tx_en_r;
//--------------------------------//




endmodule
 
// tx_done_r0        控制一帧(24bit)数据结束  寄存器
// tx_done           一帧(24bit)数据结束标志    由RZ_Code输入

// tx_en             发送数据使能               输出
// tx_en_r           发送数据使能               寄存器
  1. 数码管显示模式的选择
module smg(
   input                  clk_smg ,
	input          [1:0]   sel_2 ,
	output 	      [5:0]	  sm_bit,	//数码管选择输出引脚
	output 	reg	[7:0]   sm_seg	//数码管段输出引脚
	
);		


reg [1:0]  flag ;

always @(*)  begin

      if (!sel_2[0])            flag <= 2'd1 ;
							 
		
		else if (!sel_2[1])       flag <= 2'd2 ;
							 

		else                      flag <= 2'd0 ;	
							 

end
	
	
always @(posedge clk_smg) 
begin

     case(flag) 

	   2'd0 :  sm_seg <= 8'hc0 ;
		
		2'd1 :  sm_seg <= 8'hf9;					//显示1
		
		2'd2 :  sm_seg <= 8'ha4;					//显示2
		
	  endcase

end
	
assign	sm_bit=6'b101111;//选通第1位数码管,其余5位数码管关闭
//assign	sm_seg=8'hc0;//数码管显示0的编码,如果要显示其它数字,改写编码即可



endmodule
/*
		sm_seg <= 8'hc0;					//显示0
		sm_seg <= 8'hf9;					//显示1
		sm_seg <= 8'ha4;					//显示2
		sm_seg <= 8'hb0;					//显示3
		sm_seg <= 8'h99;					//显示4
		sm_seg <= 8'h92;					//显示5
		sm_seg <= 8'h82;					//显示6
		sm_seg <= 8'hf8;					//显示7
		sm_seg <= 8'h80;					//显示8
		sm_seg <= 8'h90;					//显示9
		sm_seg <= 8'hbf;					//显示-



      _0 = 8'b1100_0000, 
		_1 = 8'b1111_1001, 
		_2 = 8'b1010_0100,              
		_3 = 8'b1011_0000, 
		_4 = 8'b1001_1001, 
		_5 = 8'b1001_0010,  
      _6 = 8'b1000_0010, 
		_7 = 8'b1111_1000, 
		_8 = 8'b1000_0000,       
		_9 = 8'b1001_0000; 
		
		*/
  • 13
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值