前言
环境:
1、Quartus18.0
2、vscode
3、板子型号:EP4CE6F17C8
要求:
按键1按下,流水灯从右开始向左开始流动,按键2按下,流水灯从左开始向右开始流动,按键3按下LED每隔1s进行亮灭,按键4按下LED常亮。
一、按键
“自锁”是指开关能通过锁定机构保持某种状态(通或断),“轻触”是说明操作开关使用的力量大小。cyclone IV开发板上的按键属于轻触式按键。
二、系统设计
1、模块框图
2、RTL视图
三、源码
module key_led #(parameter MAX_NUM = 24'd9_999_999)(
input clk,
input rst_n,
input [3:0] key,
output reg [3:0] led
);
//parameter MAX_NUM = 24'd9_999_999;//0.2s
reg [1:0] state;
reg [23:0] cnt;
reg [3:0] key_r;
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
cnt <= 24'd0;
end
else if (cnt == MAX_NUM) begin
cnt <= 24'd0;
end
else begin
cnt <= cnt + 1'd1;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
state <= 24'd0;
end
else if (cnt == MAX_NUM) begin
state <= state + 2'd1;
end
else begin
state <= state;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
led <= 4'b0000;
end
else if (key[0] == 0) begin
case (state)
2'd0 : led <= 4'b0001;
2'd1 : led <= 4'b0010;
2'd2 : led <= 4'b0100;
2'd3 : led <= 4'b1000;
default: ;
endcase
end
else if (key[1] == 0) begin
case (state)
2'd0 : led <= 4'b1000;
2'd1 : led <= 4'b0100;
2'd2 : led <= 4'b0010;
2'd3 : led <= 4'b0001;
default: ;
endcase
end
else if (key[2] == 0) begin
case (state)
2'd0 : led <= 4'b1111;
2'd1 : led <= 4'b0000;
2'd2 : led <= 4'b1111;
2'd3 : led <= 4'b0000;
default: ;
endcase
end
else if (key[3] == 0) begin
case (state)
2'd0 : led <= 4'b1111;
2'd1 : led <= 4'b1111;
2'd2 : led <= 4'b1111;
2'd3 : led <= 4'b1111;
default: ;
endcase
end
else
led <= led;
end
endmodule
四、效果
按键控制流水灯
五、总结
这里的实现并不难,加强了按键与LED的互动性,使用了状态控制LED,槽点就是按键没有进行消抖。文章来源:https://www.toymoban.com/news/detail-734506.html
六、参考资料
按键控制led灯文章来源地址https://www.toymoban.com/news/detail-734506.html
到了这里,关于按键控制流水灯方向——FPGA的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!