1 //让多个LED灯按照设置的模式,各自在一个变化值循环独立亮灭。
 2 //源代码
 3 module counter_led_5(
 4     clk,
 5     reset_n,
 6     Time,
 7     ctrlA,
 8     ctrlB,
 9     led
10 );
11     input clk;
12     input reset_n;
13     input [31:0]Time;
14     input [7:0]ctrlA;
15     input [7:0]ctrlB;
16     output reg [1:0] led;
17     reg [31:0]counter;
18     always @(posedge clk or negedge reset_n)
19     if(!reset_n)
20         counter<=0;
21     else if(counter==Time-1)
22         counter<=0;
23     else
24         counter<=counter+1'b1;
25     reg [2:0] counter2;
26     always@(posedge clk or negedge reset_n)
27     if(!reset_n)
28         counter2<=0;
29     else if(counter==Time-1)
30         counter2<=counter2+1'b1;
31     else
32         counter2<=counter2;
33     always@(posedge clk or negedge reset_n)
34     if(!reset_n)
35         led<=0;
36     else case(counter2)
37         0:begin led[0]<=ctrlA[0];led[1]<=ctrlB[0];end
38         1:begin led[0]<=ctrlA[1];led[1]<=ctrlB[1];end 
39         2:begin led[0]<=ctrlA[2];led[1]<=ctrlB[2];end 
40         3:begin led[0]<=ctrlA[3];led[1]<=ctrlB[3];end 
41         4:begin led[0]<=ctrlA[4];led[1]<=ctrlB[4];end
42         5:begin led[0]<=ctrlA[5];led[1]<=ctrlB[5];end
43         6:begin led[0]<=ctrlA[6];led[1]<=ctrlB[6];end
44         7:begin led[0]<=ctrlA[7];led[1]<=ctrlB[7];end
45         default:led<=led;
46     endcase
47  
48 endmodule
49 仿真源代码
50 `timescale 1ns/1ns
51 module counter_led_5_tb();
52     reg clk;
53     reg reset_n;
54     reg [7:0]ctrlA;
55     reg [7:0]ctrlB;
56     reg [31:0]Time;//这里面两个的,位宽容易忘记写
57     wire [1:0] led;
58     counter_led_5 counter_led_5_inst0(
59     .clk(clk),
60     .reset_n(reset_n),
61     .ctrlA(ctrlA),
62     .ctrlB(ctrlB),
63     .Time(Time),
64     .led(led)
65     );
66     initial clk=1;
67     always #10 clk=~clk;
68     
69     initial begin
70     reset_n=0;
71     ctrlA=0;//这个容易忘记给初值,
72     ctrlB=0;
73     Time=0;//这个容易忘记给初值,
74     
75     #201;
76     reset_n=1;
77     ctrlA=8'b1000_0110;
78     ctrlB=8'b1001_0110;
79     Time=2500;//方便看波形
80     #2000;
81     
82     $stop;
83     end
84 endmodule
85  

 

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