基于PLD的矩陣鍵盤狀態(tài)機(jī)控制
reg [3:0] col_data_r;
reg [3:0] row_data_r;
reg key_flag_r0;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
row_data = 4'b0000;
Key_flag_r0 = 0;
end
else if(cnt == 20'hfffff)
begin
case(next_state)
SCAN_IDLE : begin
row_data = 4'b0000;
key_flag_r0 = 0;
end
//SCAN_JITTER:
SCAN_COL0 : row_data = 4'b1110;
SCAN_COL1 : row_data = 4'b1101;
SCAN_COL2 : row_data = 4'b1011;
SCAN_COL3 : row_data = 4'b0111;
SCAN_READ : begin
row_data_r = row_data;
col_data_r = col_data;
key_flag_r0 = 1;
end
//SCAN_JTTTER2:
default:; //default vaule
endcase
end
end
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
key_value = 0;
else if(cnt == 20'hfffff)
begin
if(key_flag_r0 == 1'b1) //the mark of key is pressed
begin
case ({row_data_r,col_data_r}) //row_data Row, col_data Col
8'b0111_0111: key_value = 4'h0;
8'b0111_1011: key_value = 4'h1;
8'b0111_1101: key_value = 4'h2;
8'b0111_1110: key_value = 4'h3;
8'b1011_0111: key_value = 4'h4;
8'b1011_1011: key_value = 4'h5;
8'b1011_1101: key_value = 4'h6;
8'b1011_1110: key_value = 4'h7;
8'b1101_0111: key_value = 4'h8;
8'b1101_1011: key_value = 4'h9;
8'b1101_1101: key_value = 4'hA;
8'b1101_1110: key_value = 4'hB;
8'b1110_0111: key_value = 4'hC;
8'b1110_1011: key_value = 4'hD;
8'b1110_1101: key_value = 4'hE;
8'b1110_1110: key_value = 4'hF;
default : key_value = key_value;
endcase
end
評論