4*4矩阵键盘FPGA扫描实现

Posted xinshuwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4*4矩阵键盘FPGA扫描实现相关的知识,希望对你有一定的参考价值。

  1 module juzhen
  2 (
  3 input clk,
  4 input rst_n,
  5 input [3:0] col_data,
  6 output reg [3:0] row_data,
  7 output key_flag, //the mark of key is pressed
  8 output reg [3:0] key_value,
  9 
 10 output reg key_flag_r0
 11 );
 12 parameter SCAN_IDLE = 3b000;
 13 parameter SCAN_JITTER= 3b001;
 14 parameter SCAN_COL0 = 3b011;
 15 parameter SCAN_COL1 = 3b010;
 16 parameter SCAN_COL2 = 3b110;
 17 parameter SCAN_COL3 = 3b100;
 18 parameter SCAN_READ = 3b101;
 19 parameter SCAN_JTTTER2= 3b111;
 20 
 21 reg [19:0] cnt; 
 22 reg [2:0] current_state;
 23 reg [2:0] next_state;
 24 always @(posedge clk or negedge rst_n)begin 
 25     if(!rst_n) begin cnt<=0; end
 26 else
 27    begin
 28       if(cnt>=20h7ffff) begin cnt<=0;end
 29      else cnt<=cnt+1;end 
 30 end
 31 always@(posedge clk or negedge rst_n)
 32 begin
 33 if(!rst_n)
 34 current_state <= SCAN_IDLE;
 35 else if(cnt == 20h7ffff) current_state <= next_state;
 36 end
 37 always@*
 38 begin case(current_state)
 39 SCAN_IDLE : //init
 40      if(col_data != 4b0000) next_state = SCAN_JITTER; //初始化
 41    else next_state = SCAN_IDLE;
 42 SCAN_JITTER: //escape the jitter
 43      if(col_data != 4b0000) next_state = SCAN_COL0;//消除抖动
 44      else next_state = SCAN_IDLE;
 45 SCAN_COL0 : //1th row
 46     if(col_data != 4b0000) next_state = SCAN_READ;else next_state = SCAN_COL1;
 47 SCAN_COL1 : //2th row
 48    if(col_data != 4b0000) next_state = SCAN_READ;else next_state = SCAN_COL2;
 49 SCAN_COL2 : //3th row
 50     if(col_data != 4b0000) next_state = SCAN_READ;else next_state = SCAN_COL3;
 51 SCAN_COL3 : //4th row
 52     if(col_data != 4b0000) next_state = SCAN_READ;else next_state = SCAN_IDLE;
 53 SCAN_READ : //lock the vaule
 54     if(col_data != 4b0000) next_state = SCAN_JTTTER2;else next_state = SCAN_IDLE;
 55 SCAN_JTTTER2: //when your hand is gone
 56     if(col_data != 4b0000) next_state = SCAN_JTTTER2;else next_state = SCAN_IDLE;
 57 endcase
 58 end
 59 reg [3:0] col_data_r;
 60 reg [3:0] row_data_r;
 61 
 62 
 63 always@(posedge clk or negedge rst_n)
 64     
 65 begin if(!rst_n) begin row_data <= 4b1111;key_flag_r0 <= 0;end
 66      else if(cnt == 20h7ffff)begin
 67    case(next_state)
 68 SCAN_IDLE : begin row_data <= 4b1111;key_flag_r0 <= 0;end //SCAN_JITTER:
 69 SCAN_COL0 : row_data <= 4b0001;
 70 SCAN_COL1 : row_data <= 4b0010;
 71 SCAN_COL2 : row_data <= 4b0100;
 72 SCAN_COL3 : row_data <= 4b1000;
 73 SCAN_READ : begin
 74 row_data_r <= row_data;
 75 col_data_r <= col_data;
 76 key_flag_r0 <= 1;
 77 end
 78 //SCAN_JTTTER2:
 79 default:; //default vaule
 80 endcase
 81 end
 82 end
 83 always @(posedge clk or negedge rst_n)
 84 begin if(!rst_n)key_value <= 0;
 85       else if(cnt == 20h7fff)
 86   begin
 87 if(key_flag_r0 == 1b1) //the mark of key is pressed
 88    begin
 89   case ({row_data_r,col_data_r}) //row_data Row, col_data Col
 90 8b0001_0001: key_value <= 4d0;
 91 8b0010_0001: key_value <= 4d1;
 92 8b0100_0001: key_value <= 4d2;
 93 8b1000_0001: key_value <= 4d3;
 94 8b0001_0010: key_value <= 4d4;
 95 8b0010_0010: key_value <= 4d5;
 96 8b0100_0010: key_value <= 4d6;
 97 8b1000_0010: key_value <= 4d7;
 98 8b0001_0100: key_value <= 4d8;
 99 8b0010_0100: key_value <= 4d9;
100 8b0100_0100: key_value <= 4d10;
101 8b1000_0100: key_value <= 4d11;
102 8b0001_1000: key_value <= 4d12;
103 8b0010_1000: key_value <= 4d13;
104 8b0100_1000: key_value <= 4d14;
105 8b1000_1000: key_value <= 4d15;
106 default : key_value <= key_value;
107 endcase
108 end
109 else
110 key_value <= key_value;
111 end
112 end
113 //Capture the falling endge
114 reg key_flag_r2,key_flag_r1;
115 always@(posedge clk or negedge rst_n)
116 begin
117 if(!rst_n)
118 begin
119 key_flag_r1 <= 0;
120 key_flag_r2 <= 0;
121 end
122 else
123 begin
124 key_flag_r1 <= key_flag_r0;
125 key_flag_r2 <= key_flag_r1;
126 end
127 end
128 assign key_flag = key_flag_r2 & ~key_flag_r1; //when your hand is gone
129 endmodule

4*4矩阵经常用的到,实现的过程就是行扫描或者列扫描,画好状态机的状态,取得对应行列的值即确定具体哪个按键被按下。

以上是关于4*4矩阵键盘FPGA扫描实现的主要内容,如果未能解决你的问题,请参考以下文章

进阶项目(11) 矩阵键盘程序设计讲解

STM32 实现 4*4 矩阵键盘扫描(HAL库标准库 都适用)

设计一个3 行×4列的矩阵键盘,采用行扫描法编写程序,当有按键动作时,能够获得按键

疑问----单片机矩阵键盘行列反转扫描法

学习笔记之51单片机键盘篇(非编码键盘与编码键盘非编码键盘的扫描方式独立键盘矩阵键盘)

4x4矩阵键盘