//让LED灯按照指定的亮灭模式亮灭,亮灭模式未知,由用户随即指定。 //8个变换状态为一个循环,每个变换状态的时间可根据不同场景选择。 源代码 module counter_led_4( clk, reset_n, Time, ctrl, led ); input clk; input reset_n; input [31:0] Time; input [7:0] ctrl; output reg led; reg [31:0] counter; always@(posedge clk or negedge reset_n) if(!reset_n) counter<=0; else if(counter==Time-1) counter<=0; else counter=counter+1'b1; reg [2:0] counter2; always@(posedge clk or negedge reset_n) if(!reset_n) counter2<=0; else if(counter==Time-1) counter2<=counter2+1'b1; always@(posedge clk or negedge reset_n) if(!reset_n) led<=0; else case(counter2) 0:led<=ctrl[0]; 1:led<=ctrl[1]; 2:led<=ctrl[2]; 3:led<=ctrl[3]; 4:led<=ctrl[4]; 5:led<=ctrl[5]; 6:led<=ctrl[6]; 7:led<=ctrl[7]; default led<=led; endcase endmodule 仿真代码 `timescale 1ns/1ns module counter_led_4_tb(); reg clk; reg reset_n; reg [31:0] Time; reg [7:0] ctrl; wire led; counter_led_4 counter_led_4_inst0( .clk(clk), .reset_n(reset_n), .ctrl(ctrl), .Time(Time), .led(led) ); initial clk=1; always #10 clk=!clk; initial begin reset_n=0; ctrl=0; Time=0; #200 reset_n=1; #2000 Time=2500; ctrl=8'b1000_0110; Time=2499; #2000000000; $stop; end endmodule
标签:reset,LED,clk,器到,counter,input,led,小梅哥 From: https://www.cnblogs.com/wangwin4/p/17113349.html