怎么用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进制计数器。

module counter_24 ( input clk, input rst, input cnt_in ,output reg cnt_out )

reg [4:0] cnt

always @ (posedge clk or posedge rst_n) begin

if (rst) cnt <= 5'b0

else if (~cnt_in) cnt <= cnt

else if (cnt == 5'b10110) cnt <= 5'b0

else cnt <= cnt + 1'b1

end

always @ (posedge clk or posedge rst) begin

if (rst) cnt_out <= 1'b0

else if (cnt_in &&cnt == 5'b10110) cnt_out <= 1'b1

else cnt_out <= 1'b0

end

endmodule

input add//为1时加 *** 作

input dec//为1时减 *** 作

output [5:0] counter

reg [5:0] counter

always @(add and dec) begin

if(add &&!dec) begin

if(counter == 6'd38) begin

counter <= 6'd0

扩展资料:

有一种记数系统便是24进制的,其中1~24有专门的符号来表示,大于24的数便可以像24进制那样写成多位数,如tokaputokapuŋgayepoko代表24进制中的茄粗启P0(552)。malapu talusupuŋga talu代表24进制中的凳乎H2G(9856)。

为了避免混淆1和I,0和O,故跳过字母I、O,18~~23分别计作J、K、L、M、N、P。比如:16计作G、22计作N。

等于或大于24的数字计作:24→10、25→11、26→12??25→11中标粗体的1代表24。同一个数字在不颤如同的位置代表的值是不一样的。

参考资料来源:百度百科-二十四进制

这是39进制计数器,纤绝可进行加减 *** 作,为0时减 *** 作输出38,为38是加 *** 作输出0.你改一下就成9进制了

module counter_39{

add,

dec,

counter

}

input add/碧竖弯/为1时加悔闷 *** 作

input dec//为1时减 *** 作

output [5:0] counter

reg [5:0] counter

always @(add and dec) begin

if(add &&!dec) begin

if(counter == 6'd38) begin

counter end

else begin

counter end

end

if(!add and dec) begin

if(counter == 6'd0) begin

counter end

else begin

counter end

end

end

endmodule


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

原文地址: https://outofmemory.cn/yw/12455061.html

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

发表评论

登录后才能评论

评论列表(0条)

保存