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。同一个数字在不同的位置代表的值是不一样的。
参考资料来源:百度百科-二十四进制
举个简单点的例子,如下。
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 traffic(clk,urgency,east_west,south_north,led)input clk
input urgency
output [7:0]east_west,south_north
output [5:0]led
reg [7:0]east_west,south_north
reg [5:0]led
initial begin
east_west<=8'b0
south_north<=8'b0
led<=6'b100001end
always @(posedge clk)
begin if(urgency==1) led<=6'b100100
else if(east_west==8'b0 &&south_north==8'b0) begin
east_west<=8'b00101101
south_north<=8'b00101000
led<=6'b100001end
else if(east_west==8'b00000110 &&south_north==8'b1) begin
east_west<=8'b00000101
south_north<=8'b00000101
led<=6'b100010end
else if(east_west==8'b1 &&south_north==8'b1 &&led[5]==1'b1) begin
east_west<=8'b00101000
south_north<=8'b00101101
led<=6'b001100end
else if(east_west==8'b1 &&south_north==8'b00000110) begin
east_west<=8'b00000101
south_north<=8'b00000101
led<=6'b010100end
else if(east_west==8'b1 &&south_north==8'b1 &&led[2]==1'b1) begin
east_west<=8'b00101101
south_north<=8'b00101000
led<=6'b100001end
else if(east_west[3:0]==4'b0000) begin
east_west<=east_west-8'b111
south_north<=south_north-1'b1end
else if(south_north[3:0]==4'b0000) begin
east_west<=east_west-1'b1
south_north<=south_north-8'b111end
else begin
east_west<=east_west-1'b1
south_north<=south_north-1'b1
end
end
endmodule
上面是我前一段时间写的交通灯控制器设计代码,相应的英文字母对应相应的信号
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)