数电实验 可逆计数器设计

数电实验 可逆计数器设计

在这里插入图片描述

module yyc2018113559_3(clk,clr,x,Q,co,codeout);
input clk,clr,x;  //clk时钟,clr低电平Q清零,x转换加计数与减计数
output reg[6:0] codeout;
output co;  //进位信号
output reg[3:0] Q;

always @(posedge clk,negedge clr)  //敏感信号为clk上升沿,clr下降沿
	if(!clr)  //如果clk为低电平,Q清零
		Q<=4'd0;
	else if(x) // 如果x为高电平,加计数
	begin
		if(Q==4'd9) //Q为9则清零
			Q<=4'd0;
		else
			Q<=Q+4'd1;
	end
	else    //如果x为低电平,减计数
	begin
		if(Q==4'd0) //Q为0则变为9重新开始减计数
			Q<=4'd9;
		else
			Q<=Q-4'd1;
	end

	assign co=(clr & ~x &Q==4'd0)|(x & Q==4'd9); //实现进位

always @(clk) //译码器
begin 
	case(Q)
	4'b0000:codeout=7'b1111110;
	4'b0001:codeout=7'b0110000;
	4'b0010:codeout=7'b1101101;
	4'b0011:codeout=7'b1111001;
	4'b0100:codeout=7'b0110011;
	4'b0101:codeout=7'b1011011;
	4'b0110:codeout=7'b1011111;
	4'b0111:codeout=7'b1110000;
	4'b1000:codeout=7'b1111111;
	4'b1001:codeout=7'b1111011;
	endcase
end


endmodule

猜你喜欢

转载自blog.csdn.net/weixin_44026026/article/details/109301660