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