FPGA练习:计数器

模块示意图如下:
FPGA练习:计数器

端口配置清单:
FPGA练习:计数器

verilog 代码:

module counter(
	clock,
	reset,
	enable,
	count);

	input clock;
	input reset;
	input enable;
	output [3:0] count;

	wire clock;
	wire reset;
	wire enable;
	reg [3:0] count;

	always @ (posedge clock) 
	begin 
		if (reset == 1) 
		begin 
			count <= 0;
		end
		else if (enable == 1) 
		begin 
			count <= count + 1; 
		end 
	end
	
endmodule 


在 always 语句块中,修改 reg 端口的值要用 <= 符号。

测试代码如下:

module counter_test();

	reg clock;
	reg reset;
	reg enable;
	wire [3:0] result;

	initial begin
		clock = 1;
		reset = 0;
		enable = 0;
		#5 reset = 1;
		#10 reset = 0;
		#10 enable = 1;
		#150 enable = 0;
		#5 $finish;
	end
	always begin
		#5 clock = ~clock;
	end
	
	initial begin
		$dumpfile ("... .../counter_test.vcd");
		$dumpvars;
	end

	counter counter1(
		.clock(clock),
		.reset(reset),
		.enable(enable),
		.count(result));

endmodule 


测试一下,看看波形图:

FPGA练习:计数器

比较一下代码,和预期一样呀,真不错!

上一篇:PAT乙级1026题解


下一篇:AttributeError: module 'time' has no attribute 'clock'