我们所需要用到的时间标志点:
对应的code如下:
module led_ctrl_2(
input wire clk,
input wire rst_n,
output reg led
);
parameter CNT_025S_MAX = 31'd12_500_000 - 1; //0.25S
parameter CNT_075S_MAX = 31'd37_500_000 - 1; //0.75S
parameter CNT_1S5_MAX = 31'd75_500_000 - 1; //1.5S
parameter CNT_2S5_MAX = 31'd125_000_000 - 1;//2.5S
//为什么要减1,大家可以思考一下从0到9是几个数,如果认为是9个的那么可以放弃学习FPGA了
//完成1秒的计数周期
reg [30:0] counter;
always @(posedge clk or negedge rst_n)
if( rst_n == 1'b0 )
counter <= 31'b0;
else if( counter == CNT_2S5_MAX )
counter <= 31'b0;
else
counter <= counter + 1'b1;
//在这里建议能在对应的数值前加上位数的还是加上位数,我记得不加位数会被默认为32位,不知道是不是还是这样
//在1秒的周期内设定对应led翻转的时间节点并完成操作
always @(posedge clk or negedge rst_n)
if( rst_n == 1'b0 )
led <= 1'b1; //因起始位高电平
else if( counter == CNT_025S_MAX )
led <= ~led;
else if( counter == CNT_075S_MAX )
led <= ~led;
else if( counter == CNT_1S5_MAX )
led <= ~led;
else if( counter == CNT_2S5_MAX )
led <= ~led;
else
led <= led; //避免产生锁存器
endmodule
对应的测试code:
`timescale 1ns/1ns
module tb_led_ctrl_1();
//****************** Parameter and Internal Signal *******************//
//wire define
wire led;
//reg define
reg clk;
reg rst_n;
//***************************** Main Code ****************************//
initial begin
clk = 1'b1;
rst_n <= 1'b0;
#201
rst_n <= 1'b1;
#2_000_000;
$stop;
end
//参数重定义缩短仿真时间(大家的时间很宝贵,尽量不要浪费)
defparam led_ctrl_2_inst.CNT_025S_MAX = 31'd12_50 - 1;
defparam led_ctrl_2_inst.CNT_075S_MAX = 31'd37_50 - 1;
defparam led_ctrl_2_inst.CNT_1S5_MAX = 31'd75_50 - 1;
defparam led_ctrl_2_inst.CNT_2S5_MAX = 31'd125_00 - 1;
// creator clk
always #10 clk <= ~clk;
//*************************** Instantiation **************************//
led_ctrl_2 led_ctrl_2_inst
(
.clk ( clk ),
.rst_n ( rst_n ),
.led ( led )
);
endmodule
对应的仿真时序如下:
作者: 商风, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-451112.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
文章评论(0条评论)
登录后参与讨论