FPGA BCD计数器(多位)

将多个一位BCD计数器级联即可得到多位BCD计数器


一位BCD计数器模块代码:

module BCD_Count(Clk,Rst_n,Cin,Cout,q);
	
	input Clk;
	input Rst_n;
	input Cin;
	output reg Cout;
	output [3:0] q;
	
	reg [3:0] cnt;
	
	always@( posedge Clk or negedge Rst_n )
		begin
			if( Rst_n==0 )
				cnt<=0;
			else if( Cin==1 )
				begin	
					cnt=cnt+1;	
					
					if( cnt==10 )
						begin
							cnt<=0;
							Cout<=1;
						end					
				end	
			else if( Cin==0 )
				Cout<=0;
		end
		
	assign q=cnt;
	
endmodule 


级联代码:

module BCD_Count_top(Clk,Rst_n,Cin,Cout,q);

	input Clk;
	input Rst_n;
	input Cin;
	output Cout;
	output [11:0] q;
	
	wire cout0,cout1;
	
	wire [3:0] q0,q1,q2;

	assign q={q2,q1,q0};
	
	BCD_Count BCD_Count0
	(
	.Clk(Clk),
	.Rst_n(Rst_n),
	.Cin(Cin),
	.Cout(cout0),
	.q(q0)
	);
	
	BCD_Count BCD_Count1
	(
	.Clk(Clk),
	.Rst_n(Rst_n),
	.Cin(cout0),
	.Cout(cout1),
	.q(q1)
	);
	
	BCD_Count BCD_Count2
	(
	.Clk(Clk),
	.Rst_n(Rst_n),
	.Cin(cout1),
	.Cout(Cout),
	.q(q2)
	);

endmodule 


testbench代码:

`timescale 1ns/1ns
`define Time_Periord 20

module BCD_Count_top_tb;

	reg clk;
	reg rst;
	reg cin;
	
	wire cout;
	wire [11:0] q;
	
	BCD_Count_top BCD_Count_top0
	(
	.Clk(clk),
	.Rst_n(rst),
	.Cin(cin),
	.Cout(cout),
	.q(q)
	);
	
	initial 
		clk=1;
		always
			#(`Time_Periord/2)	clk=~clk;
		
	initial
		begin
			rst=0;
			#100;
			rst=1;
			
			repeat( 3000 )
				begin
					cin=0;		
					#(`Time_Periord*10); 
					cin=1;		
					#(`Time_Periord*1); 
				end
				
			$stop;
		end

endmodule 

RTL仿真结果:



还是存在一些小瑕疵,以后有熟练了再改完善

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值