1 //让LED灯按照指定的亮灭模式亮灭,亮灭模式未知,由用户随即指定。
 2 //8个变换状态为一个循环,每个变换状态的时间可根据不同场景选择。
 3 源代码
 4 module counter_led_4(
 5     clk,
 6     reset_n,
 7     Time,
 8     ctrl,
 9     led
10 );
11     input clk;
12     input reset_n;
13     input [31:0] Time;
14     input [7:0] ctrl;
15     output reg led;
16     reg [31:0] counter;
17     always@(posedge clk or negedge reset_n)
18         if(!reset_n)
19             counter<=0;
20         else if(counter==Time-1)
21             counter<=0;
22         else
23             counter=counter+1'b1;
24     reg [2:0] counter2;
25     always@(posedge clk or negedge reset_n)
26         if(!reset_n)
27             counter2<=0;
28         else if(counter==Time-1)
29             counter2<=counter2+1'b1;
30      
31         always@(posedge clk or negedge reset_n)
32         if(!reset_n)
33             led<=0;
34         else case(counter2)
35             0:led<=ctrl[0];
36             1:led<=ctrl[1];
37             2:led<=ctrl[2];
38             3:led<=ctrl[3];
39             4:led<=ctrl[4];
40             5:led<=ctrl[5];
41             6:led<=ctrl[6];
42             7:led<=ctrl[7];
43             default:led<=led;
44         endcase
45 endmodule
46 仿真代码
47 `timescale 1ns/1ns
48 module counter_led_4_tb();
49     reg clk;
50     reg reset_n;
51     reg [31:0] Time;
52     reg [7:0] ctrl;
53     wire led;
54     
55     counter_led_4 counter_led_4_inst0(
56     .clk(clk),
57     .reset_n(reset_n),
58     .ctrl(ctrl),
59     .Time(Time),
60     .led(led)
61     );
62     initial clk=1;
63     always #10 clk=!clk;
64     
65     initial begin
66     reset_n=0;
67     ctrl=0;
68     Time=0;
69     
70     #201;
71     reset_n=1;
72     #2000
73     Time=2500;
74     ctrl=8'b1000_0110;
75     Time=2499;
76     
77     #2000000000;
78     $stop;
79     end
80 
81 endmodule
82  

 

posted on 2023-02-12 10:15  无情的造轮子  阅读(25)  评论(0编辑  收藏  举报