用Verilog编一个计数器的程序

用Verilog编一个计数器的程序,第1张

举个简单点的例子,如下。

设计一个4bit的计数器,在记到最大值时输出一个信号

module counter_16 ( input clk, input rst_n, input cnt_in ,output reg cnt_out )

reg [3:0] cnt

always @ (posedge clk or negedge rst_n) begin

if (~rst_n)  cnt <= 4'b0

else if (cnt_in) cnt <= cnt +1'b1

else cnt <= cnt

end

always @ (posedge clk or negedge rst_n) begin

if (~rst_n) cnt_out <= 1'b0

else if (cnt_in &&cnt == 4'b1111) cnt_out <= 1'b1

else cnt_out <= 1'b0

end

endmodule

这实际上设计了一个16进制计数器其中的一位,你可以例化多个相同模块,将低位的cnt_out连接到高位的cnt_in,级联成一个任意位数的16进制计数器。

这就是把ABC三个电台的启动信号变为输入,XY的启动信号为输出,列出真值表就行了吧。

真值表为:

a

b

c

x

y

0

0

0

0

0

0

0

1

0

1

0

1

0

1

0

0

1

1

0

1

1

0

0

1

0

1

0

1

0

1

1

1

0

0

1

1

1

1

1

1

module bijiaoqi(out,a,b,c,clk)

output reg[7:0] out

input[7:0] a,b,c//去掉了i,并将i设值为7位寄存器,用来计数clk次数,将i作为输入是不能赋值的

input clk

reg[6:0] i=0

always @(posedge clk)

begin

if(i<13)

begin

out<=0

i<=i+1

end

else if(13<i<26)//将12改成了13,按文字要求

begin

out<=(a>b)?a:0

i<=i+1

end

else if(27<i<57)//改了此处数据

begin

if(b>c) out<=(a>b)?a:0

else out<=(a>c)?a:0

i<=i+1

end

else if(57<i<77)//改了此处数据

begin

out<=(a>c)?a:0

end

end

endmodule

改了源程序中几个地方,需要注意这个程序当i计数到57以后不会再增加


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11944475.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存