设计以下两个模块的顶层模块 module lsd(clk,rst_n,led); input clk,rst_n; output reg [3:0] led; reg [25:0] cnt; reg clk_2s; always @ (posedge clk or negedge rst_n) if (!rst_n) begin cnt <='d0; clk_2s <= 0; end else if (cnt == 26'd24_999_999) begin cnt <= 0; clk_2s <= ~clk_2s; end else cnt <= cnt + 1'b1; always @ (posedge clk_2s or negedge rst_n) if (!rst_n) led <= 4'b0111; else led <= {led[0],led[3:1]}; endmodule module aa(clk,sel,rst_n,seg,Q); input clk,rst_n,Q; output reg [2:0]sel; output reg [6:0]seg; reg [25:0]cnt; reg clk_n; always @(posedge clk_n) begin if(rst_n==0) sel <= 3'd0; else begin if(sel < 3'd6) sel <= sel+1'b1; else sel <= 3'd0; end end always @(posedge clk) begin if(cnt==26'd99_999) begin cnt <= 0; clk_n <= ~clk_n; end else cnt <= cnt+1'b1; end always @(Q or sel) begin if(Q==0) seg <= 7'b1000000; else case(sel) 3'b000 : seg <= 7'b1111001; 3'b001 : seg <= 7'b0100100; 3'b010 : seg <= 7'b0110000; 3'b011 : seg <= 7'b0011001; 3'b100 : seg <= 7'b0010010; 3'b101 : seg <= 7'b0000010; default seg <=7'b1111111; endcase end endmodule
时间: 2024-03-11 19:43:56 浏览: 110
这里有两个模块的顶层模块,其中一个是 lsd 模块,另一个是 aa 模块。lsd 模块是一个计数器,每秒钟会将 led 显示的值向左移动一位,而 aa 模块则是根据输入的 Q 和 sel 信号来控制七段数码管的显示。其中 sel 信号是控制七段数码管显示哪一位数字的信号,而 Q 信号是控制显示的数字的信号。在每秒钟的计数到 99_999 时,会将 clk_n 信号取反,从而控制 lsd 模块中的 led 显示向左移动一位。
相关问题
module top_module( input clk, input rst_n, output reg [3:0] led, output reg [2:0] sel, output reg [6:0] seg ); wire clk_2s; wire [3:0] lsd_led; lsd lsd_inst( .clk(clk), .rst_n(rst_n), .led(lsd_led) ); aa aa_inst( .clk(clk_2s), .sel(sel), .rst_n(rst_n), .seg(seg), .Q(lsd_led[3]) ); always @ (posedge clk) begin if (rst_n == 0) begin led <= 4'b0111; end else begin led <= {led[0], led[3:1]}; end end endmodule
这是一个顶层模块,包含了之前提到的 lsd 模块和 aa 模块。在这个模块中,将 lsd 模块的 led 信号连接到了一个名为 lsd_led 的 wire 信号上,而将 lsd 模块的 clk_2s 信号连接到了 aa 模块的 clk 输入上。同时,将 lsd_led[3] 信号连接到了 aa 模块的 Q 输入上,用来控制七段数码管的显示数字。最后,在 always 块中,根据时钟信号来控制 led 的显示向左移动一位。
module lsd(clk, rst, led,ctrl); input clk; input rst; input ctrl; output [7 : 0] led; parameter WIDTH =27; parameter COUNT = 99_999_999; reg [WIDTH - 1 : 0] cnt; reg [7 : 0] light; reg flag; always @(posedge clk) begin if (!rst) cnt <= 0; else if (cnt == COUNT) cnt <= 0; else if (ctrl) cnt <= cnt; else cnt <= cnt + 1; end always @(posedge clk) begin if (light == 8'h01) flag <= 1; //left move else if (light == 8'h80) flag <= 0; //rihgt move else ; end always @(posedge clk) begin if (!rst) light <= 8'h01; else if (cnt == COUNT) begin if (ctrl) light <= light; else if (flag) light <= light << 1; else light <= light >> 1; end else ; end assign led = light; endmodule修改代码,添加一个按键使流水灯能够反向运行
好的,下面是修改后的代码,添加了一个按键,使流水灯能够反向运行:
```
module lsd(clk, rst, led, ctrl, key);
input clk;
input rst;
input ctrl;
input key;
output [7:0] led;
parameter WIDTH = 27;
parameter COUNT = 99_999_999;
reg [WIDTH-1:0] cnt;
reg [7:0] light;
reg flag;
always @(posedge clk) begin
if (!rst)
cnt <= 0;
else if (cnt == COUNT)
cnt <= 0;
else if (ctrl)
cnt <= cnt;
else
cnt <= cnt + 1;
end
always @(posedge clk) begin
if (light == 8'h01)
flag <= 1; //left move
else if (light == 8'h80)
flag <= 0; //right move
else
flag <= flag;
end
always @(posedge clk) begin
if (!rst)
light <= 8'h01;
else if (cnt == COUNT) begin
if (ctrl)
light <= light;
else if (flag)
light <= light << 1; //reverse left move
else
light <= light >> 1; //reverse right move
end
else
light <= light;
end
always @(posedge clk) begin
if (key == 1'b0) // detect falling edge of key
flag <= ~flag; // toggle flag
end
assign led = light;
endmodule
```
修改的主要部分是添加了一个按键信号key,并在第四个always块中检测key信号的下降沿,从而切换flag信号的值,实现反向运行的效果。
阅读全文