-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path29_key_scan.v
93 lines (85 loc) · 1.46 KB
/
29_key_scan.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//a 4*4 key scan
//author:WangFW
//date:2020-6-11
module key_scan(clk,rst_n,col,row,key_out);
input clk; //10KHz
input rst_n;
input [3:0] col;
output reg [3:0] row;
output reg [15:0] key_out;
parameter s1=2'b00,s2=2'b01,s3=2'b10,s4=2'b11;
reg [1:0] state;
reg clk_200hz;
reg [4:0] cnt;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
cnt<=5'd0;
clk_200hz=1'b0;
end
else
begin
if(cnt==25)
begin
cnt<=5'd0;
clk_200hz<=~clk_200hz;
end
else
begin
cnt<=cnt+1'b1;
clk_200hz<=clk_200hz;
end
end
end
always @(posedge clk_200hz or negedge rst_n)
begin
if(!rst_n)
begin
state<=s1;
row<=4'b1111;
end
else
begin
case(state)
s1:
begin
state<=s2;
row<=4'b1101;
end
s2:
begin
state<=s3;
row<=4'b1011;
end
s3:
begin
state<=s4;
row<=4'b0111;
end
s4:
begin
state<=s1;
row<=4'b1110;
end
default:state<=s2;
endcase
end
end
always @(posedge clk_200hz or negedge rst_n)
begin
if(!rst_n)
begin
key_out<=16'hffff;
end
else
begin
case(state)
s1:key_out[3:0]<=col^key_out[3:0]; //filter
s2:key_out[7:4]<=col^key_out[7:4];
s3:key_out[11:8]<=col^key_out[11:8];
s4:key_out[15:12]<=col^key_out[15:12];
endcase
end
end
endmodule