用Verilog HDL语言设计一个模值可变的计数器?怎样做?

如题所述

其实很简单的,这个和可以设置初始值的计数器实现方法是一样的。如果你能看懂下面这段代码,相信你肯定能写出一个模值可变的计数器了。
module counter7(clk,rst,load,data,cout);
input clk,rst,load;
input [2:0] data;
output reg [2:0] cout;

always@(posedge clk)
begin
if(!rst)
cout<=3’d0;
else if(load)
cout<=data;
else if(cout>=3’d6)
cout<=3’d0;
else
cout<=cout+3’d1;
end
endmodule
这段代码是设计一个可预置初值的7进制循环计数器。按照你的需要,稍微改一下就可以了,这下你应该会了吧?
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-27
module param_counter(
clk_in, reset, cnt_out
);
input clk_in;
input reset;
output [15:0] cnt_out;
//参数化调用,利用#符号将计数器的模值 10 传入被调用模块
cnt #(10) inst_cnt(
.clk_in(clk_in),
.reset(reset),
83
.cnt_out(cnt_out)
);
endmodule
被例化的参数化计数器的代码如下:
module cnt(
clk_in, reset, cnt_out
);
//定义参数化变量
parameter [15:0]Cmax = 1024;
input clk_in;
input reset;
output [15:0] cnt_out;
reg [15:0] cnt_out;
//完成模值可控的计数器
always @(posedge clk_in) begin
if(!reset)
cnt_out <= 0;
else
if(cnt_out == Cmax)
cnt_out <= 0;
else
cnt_out <= cnt_out + 1;
end
endmodule