功能分析
设计四个不同频率闪烁的led灯
使用参数化设计及模块复用
设计输入
设置底层模块
module led_run8(
Clk,
Reset_n,
Led
);
input Clk;
input Reset_n;
output reg Led;
parameter MCNT = 24999999;
reg [31:0]counter;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
counter<=0;
else if (counter == MCNT)
counter <= 0;
else
counter <= counter + 1'b1;
always@(posedge Clk or negedge Reset_n)
if(!Reset_n)
Led <= 0;
else if(counter == MCNT)
Led<=!Led;
else
Led <= Led;
endmodule
设置顶层模块 ,使用模块复用和参数化设计
//四个led分别以不同的频率进行闪烁
module led_run8_test(
Clk,
Reset_n,
Led
);
input Clk;
input Reset_n;
output [3:0]Led;
led_run8
#(
.MCNT(2_500_000 - 1)
)
led_run8_inst1(
.Clk(Clk),
.Reset_n(Reset_n),
.Led(Led[0])
);
//defparam led_run8_inst1.MCNT = 2_500_000 - 1;
//0.1s频率闪烁,计数半个周期为0.05s,则计数次数为50_000_000ns/20ns = 2_500_000
led_run8
#(
.MCNT(5_000_000 - 1)
)
led_run8_inst2(
.Clk(Clk),
.Reset_n(Reset_n),
.Led(Led[1])
);
//defparam led_run8_inst1.MCNT = 5_000_000 - 1;
//0.2s频率闪烁,计数半个周期为0.1s,则计数次数为100_000_000ns/20ns = 5_000_000
led_run8
#(
.MCNT(7_500_000 - 1)
)
led_run8_inst3(
.Clk(Clk),
.Reset_n(Reset_n),
.Led(Led[2])
);
//defparam led_run8_inst1.MCNT = 7_500_000 - 1;
//0.3s频率闪烁,计数半个周期为0.15s,则计数次数为150_000_000ns/20ns = 7_500_000
led_run8
#(
.MCNT(10_000_000 - 1)
)
led_run8_inst4(
.Clk(Clk),
.Reset_n(Reset_n),
.Led(Led[3])
);
//defparam led_run8_inst1.MCNT = 10_000_000 - 1;
//0.4s频率闪烁,计数半个周期为0.2s,则计数次数为200_000_000ns/20ns = 10_000_000
endmodule
功能仿真代码
`timescale 1ns/1ns
module tb_led_run8;
reg Clk;
reg Reset_n;
wire [3:0] Led;
led_run8_test led_run8_test(
.Clk(Clk),
.Reset_n(Reset_n),
.Led(Led)
);
initial Clk=1;
always #10 Clk=!Clk;
initial begin
Reset_n=0;
#201;
Reset_n=1;
#2000000000;
$stop;
end
endmodule