module liushui(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修改代码,添加一个按键,使按键为1时流水灯能够单向运行
时间: 2024-02-10 16:06:18 浏览: 85
LED-liushui.rar_霍尔传感器_霍尔测速
可以添加一个输入端口 button,用来输入按键信号。当 button 为 1 时,流水灯单向循环运行;否则,流水灯双向循环运行。修改后的代码如下:
```
module liushui(clk, rst, led, button);
input clk;
input rst;
input button;
output [7:0] led;
parameter WIDTH = 27;
parameter COUNT = 99_999_999;
reg [WIDTH - 1:0] cnt;
reg [7:0] light;
reg [1:0] dir; // 0: right, 1: left
always @(posedge clk) begin
if (!rst)
cnt <= 0;
else if (cnt == COUNT)
cnt <= 0;
else if (button)
cnt <= cnt;
else
cnt <= cnt + 1;
end
always @(posedge clk) begin
if (light == 8'h01) // left move
dir <= 1;
else if (light == 8'h80) // right move
dir <= 0;
end
always @(posedge clk) begin
if (!rst)
light <= 8'h01;
else if (cnt == COUNT)
if (button)
if (dir)
light <= light << 1; // left move
else
light <= light >> 1; // right move
else
if (dir)
light <= light << 1; // left move
else if (light == 8'h80)
light <= 8'h01; // reset to 8'h01
else
light <= light >> 1; // right move
end
assign led = light;
endmodule
```
阅读全文