用vivado设计用于FPGA板的代码,实现多种形式的流水灯(LD0~LD7)。可用6个开关切换模式。(灯的闪烁周期均为1秒) (1)按键1按下后灯从左到右按顺序轮流闪1次,循环执行; (2)按键2按下后所有灯闪烁3次; (3)按键3按下后灯从右到左按顺序轮流闪烁1次,循环执行; (4)按键4按下后奇数灯和偶数灯轮流各闪3次,; (5)按键5按下后前4盏灯从右到左闪一遍; (6)按键6按下后后4盏灯从左到右闪一遍。
时间: 2023-12-30 13:03:33 浏览: 130
以下是基于Vivado的FPGA实现代码,实现多种形式的流水灯:
```verilog
module led_controller(
input clk, // 时钟信号
input [5:0] sw, // 6个开关输入信号
output reg [7:0] leds // 8个LED灯输出信号
);
reg [7:0] led_pattern; // LED灯的模式
reg [2:0] mode; // 当前模式
reg [2:0] count; // 闪烁计数器
// 模式1:按键1按下后灯从左到右按顺序轮流闪1次,循环执行
reg [2:0] index1; // 灯的位置
always @ (posedge clk) begin
if (sw[0]) begin
mode <= 3'b001;
count <= 0;
index1 <= 3'b000;
end else if (mode == 3'b001) begin
if (count == 3'd0) begin
led_pattern <= {led_pattern[6:0], led_pattern[7]};
if (index1 == 3'b111) index1 <= 3'b000;
else index1 <= index1 + 3'd1;
count <= 3'd1;
end else begin
count <= count + 3'd1;
end
end
end
// 模式2:按键2按下后所有灯闪烁3次
always @ (posedge clk) begin
if (sw[1]) begin
mode <= 3'b010;
count <= 0;
end else if (mode == 3'b010) begin
if (count == 3'd0) begin
led_pattern <= ~led_pattern;
count <= 3'd1;
end else if (count == 3'd3) begin
led_pattern <= 8'h00;
count <= 3'd0;
end else begin
count <= count + 3'd1;
end
end
end
// 模式3:按键3按下后灯从右到左按顺序轮流闪烁1次,循环执行
reg [2:0] index3; // 灯的位置
always @ (posedge clk) begin
if (sw[2]) begin
mode <= 3'b011;
count <= 0;
index3 <= 3'b111;
end else if (mode == 3'b011) begin
if (count == 3'd0) begin
led_pattern <= {led_pattern[0], led_pattern[7:1]};
if (index3 == 3'b000) index3 <= 3'b111;
else index3 <= index3 - 3'd1;
count <= 3'd1;
end else begin
count <= count + 3'd1;
end
end
end
// 模式4:按键4按下后奇数灯和偶数灯轮流各闪3次
reg [2:0] index4; // 灯的位置
always @ (posedge clk) begin
if (sw[3]) begin
mode <= 3'b100;
count <= 0;
index4 <= 3'b000;
end else if (mode == 3'b100) begin
if (count == 3'd0) begin
if (index4[0]) led_pattern[index4] <= 1'b0;
else led_pattern[index4] <= 1'b1;
if (index4 == 3'b111) index4 <= 3'b000;
else index4 <= index4 + 3'd1;
count <= 3'd1;
end else if (count == 3'd3) begin
count <= 3'd0;
end else begin
count <= count + 3'd1;
end
end
end
// 模式5:按键5按下后前4盏灯从右到左闪一遍
reg [2:0] index5; // 灯的位置
always @ (posedge clk) begin
if (sw[4]) begin
mode <= 3'b101;
count <= 0;
index5 <= 3'b100;
end else if (mode == 3'b101) begin
if (count == 3'd0) begin
led_pattern[index5] <= 1'b1;
if (index5 == 3'b000) begin
count <= 3'd4;
end else begin
index5 <= index5 - 3'd1;
count <= 3'd1;
end
end else if (count == 3'd4) begin
led_pattern <= 8'h00;
count <= 3'd0;
end else begin
count <= count + 3'd1;
end
end
end
// 模式6:按键6按下后后4盏灯从左到右闪一遍
reg [2:0] index6; // 灯的位置
always @ (posedge clk) begin
if (sw[5]) begin
mode <= 3'b110;
count <= 0;
index6 <= 3'b011;
end else if (mode == 3'b110) begin
if (count == 3'd0) begin
led_pattern[index6] <= 1'b1;
if (index6 == 3'b111) begin
count <= 3'd4;
end else begin
index6 <= index6 + 3'd1;
count <= 3'd1;
end
end else if (count == 3'd4) begin
led_pattern <= 8'h00;
count <= 3'd0;
end else begin
count <= count + 3'd1;
end
end
end
assign leds = led_pattern;
endmodule
```
该代码实现了六种不同的模式,使用六个开关作为模式选择输入。在不同的模式下,LED灯的输出会发生变化,实现不同的流水灯效果。
阅读全文