verilog实现计数器

来源:互联网 发布:人防工程结构设计软件 编辑:程序博客网 时间:2024/05/10 00:05
/*********在闸门时间内对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;endalways @(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计数    endendendmodule

仿真结果
仿真结果
可以看出,在闸门时间内,脉冲个数为8,输出计数结果正确

1 0