矩阵键盘扫描
module matrix_keyboard
(
input clk_50m, // 50M时钟
input rst_n, // 复位
input [3:0] row, // 矩阵键盘行输入
output reg [3:0] col, // 矩阵键盘列输出
output reg key_flag, // 按键按下标志
output reg [3:0] key_val // 按键扫描值
);
parameter NO_KEY_PRESSED = 3’d0, // 没有按键按下状态
SCAN_COL0 = 3’d1, // 扫描第一列
SCAN_COL1 = 3’d2, // 扫描第二列
SCAN_COL2 = 3’d3, // 扫描第三列
SCAN_COL3 = 3’d4, // 扫描第四列
KEY_PRESSED = 3’d5; // 按键按下状态
reg clk_50hz; // 扫描频率50hz
reg [31:0] clk_cnt; // 分频计数器
reg [ 2:0] current_state, next_state; // 状态值
// 此always块分频出50hz的矩阵键盘扫描时钟
always @(posedge clk_50m, negedge rst_n) begin
if(!rst_n) begin
clk_cnt <= 32’d0;
clk_50hz <= 1’b0;
end
else if(clk_cnt == 32’d49_9999) begin
clk_cnt <= 32’d0;
clk_50hz <= ~clk_50hz;
end
else
clk_cnt <= clk_cnt + 1’b1;