流水灯示例 本示例将较为详细从基础流水灯——按键控制流水灯方向——按键控制流水灯频率——呼吸灯进行逐步讲解。
基础流水灯 模块文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 module loopled( input wire sys_clk , input wire rst_n , output reg [7 :0 ]led ); parameter fre = 32'd5000 ; reg clk_1Hz; reg [31 :0 ] cnt; always @(posedge sys_clk or posedge rst_n) begin if (rst_n == 1 )begin cnt <= 0 ; clk_1Hz <= 0 ; end else begin cnt <= cnt + 32'd1 ; if (cnt == fre/2 )begin clk_1Hz = ~clk_1Hz; cnt<=0 ; end end end always @(posedge clk_1Hz or posedge rst_n)begin if (rst_n == 1 ) begin led = 8'b0000_0001 ; end else begin led = {led[0 ],led[7 :1 ]}; end end endmodule
测试文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 module loopLed_tb(); reg sys_clk ; reg rst_n ; wire [7 :0 ]led ; initial begin sys_clk <= 0 ; res_n <= 0 ; #50 res_n <= 1 ; #100 res_n <= 0 ; end always #1 sys_clk <= ~sys_clk; loopled led (.sys_clk (sys_clk), .rst_n (rst_n), .led (led)); endmodule