2021-08-06

计数器

verilog

实现0-100计数

module counter(
	input clk,
	input rst_n,
	output reg [7:0] cnt
    );
	 
	 always @ (posedge clk,negedge rst_n)
	 begin 
	   if(rst_n == 1'b0)
			cnt <= 8'd0;
		else if(cnt == 8'd100)
			cnt <= 8'd0;
		else
			cnt <= cnt + 1'b1;
	end
		


endmodule

tb

module counter_tb;

	// Inputs
	reg clk;
	reg rst_n;

	// Outputs
	wire [7:0] cnt;

	// Instantiate the Unit Under Test (UUT)
	counter uut (
		.clk(clk), 
		.rst_n(rst_n), 
		.cnt(cnt)
	);

	initial begin
		// Initialize Inputs
		clk = 0;
		rst_n = 0;

		// Wait 100 ns for global reset to finish
		#100;
		rst_n = 1;
		#10000;
		$stop;
        
		// Add stimulus here

	end
    always #10 clk = ~clk;
endmodule

仿真波形

实现一毫秒的计时

module time_counter(
	input clk,
	input rst_n,
	output reg flag
	
    );
	parameter t = 50_000;
	
	reg [15:0] cnt;
	
	always @ (posedge clk,negedge rst_n)
	begin
	  if(rst_n == 1'b0)
	    cnt <= 16'd0;
	  else if (cnt == t-1)
	    cnt <= 16'd0;
	  else
		 cnt <= cnt + 1'b1;
	end
	
	always @ (posedge clk,negedge rst_n)
	begin
	  if(rst_n == 1'b0)
	    flag <= 1'b0;
	  else if (cnt == t-1)
	    flag <= 1'b1;
	  else 
	    flag <= 1'b0;
	end
endmodule

tb

module time_counter_tb;

	// Inputs
	reg clk;
	reg rst_n;

	// Outputs
	wire flag;

	// Instantiate the Unit Under Test (UUT)
	time_counter time_counter_inst(
		.clk(clk), 
		.rst_n(rst_n), 
		.flag(flag)
	);

	initial begin
		// Initialize Inputs
		clk = 0;
		rst_n = 0;

		// Wait 100 ns for global reset to finish
		#100;
        
		// Add stimulus here
		rst_n = 1;
		#10000;
		$stop;
	end
   always #10 clk = ~clk;   
endmodule

手动继续波形

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值