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修改代码,添加一个按键使流水灯能够反向运行
时间: 2024-01-19 17:05:03 浏览: 78
好的,下面是修改后的代码,添加了一个按键,使流水灯能够反向运行:
```
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信号的值,实现反向运行的效果。
阅读全文