【PFGA】--计数器的几种写法

module counter#(parameter cnt_begin =0 , cnt_max =24999_999 ,width =25 )(

	input clk,
	input rst_n,
	input enable,
	
	output  [width-1:0] cnt_out

);
	reg [width-1:0] cnt;
	wire cnt_set;
	
	always @(posedge clk or negedge rst_n)begin 
		if(!rst_n)
			cnt <= cnt_begin;
		else if(enable)begin
			if(cnt_set)begin
				cnt <= cnt_begin;
			end
			else begin
				cnt <= cnt + 1'b1;
			end
		end
	end 
	
	assign cnt_set = (cnt == cnt_max);
	assign cnt_out = cnt;
	
	endmodule 

可作为计数器模板使用。只需要改cnt_begin、cnt_max、width的值,就能得到自己所需要的计数器。

另一种计数0.5s时间的最简单的写法

module counter1(
	input clk,
	input rst_n,
	
	output [24:0]cnt_out


);
	reg [24:0] cnt ;

	always @(posedge clk or negedge rst_n)
		if(!rst_n)
			cnt <= 1'd0;
		else if(cnt == 24999_999)
			cnt <= 1'd0;
		else 	
			cnt <= cnt + 1'd1;
	
		
			
	assign cnt_out = cnt ;
	endmodule 

 这种写法在级联中更有优势,只写了中间主要的部分逻辑

always@(posegdge clk or negedge rst_n)
	if(!rst_n)
		cnt <= 'd0;
	else if (add_cnt)begin
		if(end_cnt)
			cnt <= 'd0;
	end
	else 
		cnt <= cnt + 1'b1;
	else 
		cnt <= 'd0;
		
		assign add =   ;
		assign end_cnt = add_cnt&&cnt == cnt_max -1

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值