求大神帮忙写个verilog HDL代码 设计一个具有异步清零功能的同步计数器

设计一个具有异步清零功能的同步计数器,要求计数器从1,2,3....计到12,1,2....不断循环。上电后计数器其初始状态为12,然后整计数从12,1,2....12,1不断循环,当有异步清零信号时,计数器被清零,在清零信号撤去后,从12开始计数。给出verilog HDL代码

`timescale 1ns / 1ps

module top(

input clk,

input rst_n,

output reg [3:0] cnt

);

always@(posedge clk or negedge rst_n)

begin

if(!rst_n)

cnt<=0;

else  if(cnt==0)

cnt<=12;

else if(cnt>=12)

cnt<=1;

 else

cnt<=cnt+1;

end 

endmodule 

代码贴在这格式就乱了,不过比较简单,应该不会有阅读障碍,应该是符合你的要求的。附件上也上传了.v文件!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-09
module(clk, aclr,cunt);
output reg [3:0] cunt;
input clk;
input aclr;

initial begin cunt<= 4'hc; end; //初始状态设为12
always@(posedge clk or posedge aclr)//把清零信号rst列在这里面就代表异步清零
if(rst) cunt<= 4'hc;
else begin
if(cunt==4'hc) cunt<= 1;
else cunt<= cunt+ 1; end

endmodule
第2个回答  2013-07-09
module(clk, aclr,cnt);
output [3:0] cnt;
input clk;
input aclr;

reg [3:0] cnt;

always@(posedge clk or posedge aclr)
begin
if(aclr)
cnt[3:0] <= 4'hc;
else
begin
if(cnt[3:0] == 4'hc)
cnt[3:0] <= 4'h1;
else
cnt[3:0] <= cnt[3:0]+ 1'b1;
end
end
endmodule追问

请问下 你这个和下面的答案 哪个比较好啊?

追答

上面的那位同学写错了一些,比如rst信号没有定义,代码不规范等,思想都是一样的

本回答被提问者采纳
相似回答