//让LED灯按照指定的亮灭模式亮灭,亮灭模式未知,由用户随机指定。以0.25秒为一个变换 //周期,8个周期为一个循环。8*0.25=2,2s➗20ns=100 000 000 源代码 module counter_led_3( clk, reset_n, ctrl, led ); input clk; input reset_n; input [7:0] ctrl; output reg led; parameter MCNT=100 000 000; reg [25:0] counter; always@(posedge clk or negedge reset_n) if(!reset_n) counter<=0; else if(counter==MCNT-1) counter<=0; else counter<=counter+1'b1; // always@(posedge clk or negedge reset_n)//这种是为了方便理解,实际用下面那一种写法。 // if(!reset_n) // led<=0; // else if(counter==MCNT/8-1) // led<=ctrl[0]; // else if(counter==MCNT*2/8-1) // led<=ctrl[1]; // else if(counter==MCNT*3/8-1) // led<=ctrl[2]; // else if(counter==MCNT*4/8-1) // led<=ctrl[3]; // else if(counter==MCNT*5/8-1) // led<=ctrl[4]; // else if(counter==MCNT*6/8-1) // led<=ctrl[5]; // else if(counter==MCNT*7/8-1) // led<=ctrl[6]; // else if(counter==MCNT-1) // led<=ctrl[7]; always@(posedge clk or negedge reset_n) if(!reset_n) led<=0; else case(counter) MCNT*1/8-1:led<=ctrl[0]; MCNT*2/8-1:led<=ctrl[1]; MCNT*3/8-1:led<=ctrl[2]; MCNT*4/8-1:led<=ctrl[3]; MCNT*5/8-1:led<=ctrl[4]; MCNT*6/8-1:led<=ctrl[5]; MCNT*7/8-1:led<=ctrl[6]; MCNT*8/8-1:led<=ctrl[7]; default led<=led; endcase endmodule 仿真代码 `timescale 1ns/1ns module counter_led_3_tb(); reg clk; reg reset_n; reg [7:0] ctrl; wire led; defparam counter_led_3_inst0.MCNT=100000; counter_led_3 counter_led_3_inst0( .clk(clk), .reset_n(reset_n), .ctrl(ctrl), .led(led) ); initial clk=1; always #10 clk=!clk; initial begin reset_n=0; ctrl=0; #200 ctrl=8'b1101_0110; reset_n=1; #2000000000; $stop; end endmodule
标签:reset,LED,clk,器到,000,input,led,小梅哥 From: https://www.cnblogs.com/wangwin4/p/17113366.html