verilog实现计数器

/*********在闸门时间内对clk脉冲个数进行计数*************/
module cnt(clk,gate,cntout);

input clk;
input gate;
output [19:0] cntout ;

reg [19:0] cnt,cntout;
reg gatebuf;

always @(posedge clk)
begin
    gatebuf<=gate;
end

always @(posedge clk)
begin
    if((gate==1'b1)&&(gatebuf==1'b0))//门信号的上升沿
    begin
        cnt<=20'd1;//开始计数
    end
    else if((gate==1'b0)&&(gatebuf==1'b1))//门信号的下降沿
    begin
        cntout<=cnt;//输出计数结果
    end
    else if(gatebuf==1'b1)//门信号保持高电平期间
    begin
        cnt<=cnt+20'd1;//增1计数
    end
end
endmodule

仿真结果
verilog实现计数器_第1张图片
可以看出,在闸门时间内,脉冲个数为8,输出计数结果正确

你可能感兴趣的:(FPGA学习)