学习defparam和元件例化所遇到问题

该博客介绍了如何使用Verilog通过defparam改变模块参数,实现不同周期的LED闪烁效果。在LR8模块中,通过实例化LR模块三次并设置不同的ENDCNT参数,使得三个LED的亮灭周期分别为0.1s, 1s和0.2s。代码展示了如何在时钟上升沿触发计数器,当计数值达到预设的ENDCNT时,翻转LED的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在学习defparam和元件例化中,如果所写的模块中有parameter变量,我们可以在顶层模块中使用defparam将所调用模块的值进行改变,并且在顶层模块中,输入输出都是wire型的,因为在顶层模块中输入输出信号就是将各个模块连接起来,起到一个连接线的作用。

所实现的是不同小灯亮灭时间不同,周期为0.1s,1s,0.2s,代码如下:

`timescale 1ns / 1ps


module LR(
    clk,
    reset,
    out

    );
    input clk;
    input reset;
    output out;
    reg out;
    reg [32:0] cnt;
    parameter ENDCNT = 32'd2_499_999;

    always @( posedge clk) begin
            if (!reset) begin
                
                out <= 0;
                cnt <= 0;
            end
            else begin
                
                if(cnt <= ENDCNT) begin
                    
                    cnt <= cnt + 1;
                    out <= out;
                
                end
                else begin
                    cnt <= 0;
                    out <= !out;
                end
                    
            end
        
    end

endmodule
`timescale 1ns / 1ps


module LR8(
    clk,
    reset,
    out
    );
    input clk;
    input reset;
    output[2:0] out;
    wire [2:0] out;
 
    
    LR LR_0(
    .clk(clk),
    .reset(reset),
    .out(out[0])
    );
    
    
    
    LR LR_1(
    .clk(clk),
    .reset(reset),
    .out(out[1])
    );
    defparam LR_1.ENDCNT  = 32'd4_999_999;

    LR LR_2(
    .clk(clk),
    .reset(reset),
    .out(out[2])
    );
    defparam LR_2.ENDCNT = 32'd24_999_999;
    
endmodule

仿真图如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值