-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.v
More file actions
207 lines (185 loc) · 5.17 KB
/
Copy pathDecoder.v
File metadata and controls
207 lines (185 loc) · 5.17 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Company: Digilent Inc 2011
// Engineer: Michelle Yu
// Josh Sackos
// Create Date: 07/23/2012
//
// Module Name: Decoder
// Project Name: PmodKYPD_Demo
// Target Devices: Nexys3
// Tool versions: Xilinx ISE 14.1
// Description: This file defines a component Decoder for the demo project PmodKYPD. The Decoder scans
// each column by asserting a low to the pin corresponding to the column at 1KHz. After a
// column is asserted low, each row pin is checked. When a row pin is detected to be low,
// the key that was pressed could be determined.
//
// Revision History:
// Revision 0.01 - File Created (Michelle Yu)
// Revision 0.02 - Converted from VHDL to Verilog (Josh Sackos)
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// ==============================================================================================
// Define Module
// ==============================================================================================
module Decoder(
input clk,// 100MHz onboard clock
input [3:0] Row,// Rows on KYPD
output reg[3:0] Col,// Columns on KYPD
output reg [3:0] DecodeOut,// Output data
output wire pressSignal
);
// ==============================================================================================
// Parameters, Regsiters, and Wires
// ==============================================================================================
// Output wires and registers
// Count register
reg [19:0] sclk;
// ==============================================================================================
// Implementation
// ==============================================================================================
reg [4:0] col1Debouncer;
reg [4:0] col2Debouncer;
reg [4:0] col3Debouncer;
reg [4:0] col4Debouncer;
initial begin
col1Debouncer <= 5'b00000;
col2Debouncer <= 5'b00000;
col3Debouncer <= 5'b00000;
col4Debouncer <= 5'b00000;
end
assign pressSignal = col1Debouncer[0] | col2Debouncer[0] | col3Debouncer[0] | col4Debouncer[0];
always @(posedge clk) begin
// 1ms
if (sclk == 20'b00011000011010100000) begin
//C1
Col <= 4'b0111;
sclk <= sclk + 1'b1;
end
// check row pins
else if(sclk == 20'b00011000011010101000) begin
//R1
if (Row == 4'b0111) begin
DecodeOut <= 4'b0001; //1
col1Debouncer <= 5'b11111;
end
//R2
else if(Row == 4'b1011) begin
DecodeOut <= 4'b0100; //4
col1Debouncer <=5'b11111;
end
//R3
else if(Row == 4'b1101) begin
DecodeOut <= 4'b0111; //7
col1Debouncer <= 5'b11111;
end
//R4
else if(Row == 4'b1110) begin
DecodeOut <= 4'b0000; //0
col1Debouncer <= 5'b11111;
end else begin
col1Debouncer <= {1'b0, col1Debouncer[4:1]};
end
sclk <= sclk + 1'b1;
end
// 2ms
else if(sclk == 20'b00110000110101000000) begin
//C2
Col<= 4'b1011;
sclk <= sclk + 1'b1;
end
// check row pins
else if(sclk == 20'b00110000110101001000) begin
//R1
if (Row == 4'b0111) begin
DecodeOut <= 4'b0010; //2
col2Debouncer <=5'b11111;
end
//R2
else if(Row == 4'b1011) begin
DecodeOut <= 4'b0101; //5.
col2Debouncer <=5'b11111;
end
//R3
else if(Row == 4'b1101) begin
DecodeOut <= 4'b1000; //8
col2Debouncer <=5'b11111;
end
//R4
else if(Row == 4'b1110) begin
DecodeOut <= 4'b1111; //F
col2Debouncer <=5'b11111;
end else begin
col2Debouncer <= {1'b0, col2Debouncer[4:1]};
end
sclk <= sclk + 1'b1;
end
//3ms
else if(sclk == 20'b01001001001111100000) begin
//C3
Col<= 4'b1101;
sclk <= sclk + 1'b1;
end
// check row pins
else if(sclk == 20'b01001001001111101000) begin
//R1
if(Row == 4'b0111) begin
DecodeOut <= 4'b0011; //3
col3Debouncer <=5'b11111;
end
//R2
else if(Row == 4'b1011) begin
DecodeOut <= 4'b0110; //6
col3Debouncer <=5'b11111;
end
//R3
else if(Row == 4'b1101) begin
DecodeOut <= 4'b1001; //9
col3Debouncer <=5'b11111;
end
//R4
else if(Row == 4'b1110) begin
DecodeOut <= 4'b1110; //E
col3Debouncer <=5'b11111;
end else begin
col3Debouncer <= {1'b0, col3Debouncer[4:1]};
end
sclk <= sclk + 1'b1;
end
//4ms
else if(sclk == 20'b01100001101010000000) begin
//C4
Col<= 4'b1110;
sclk <= sclk + 1'b1;
end
// Check row pins
else if(sclk == 20'b01100001101010001000) begin
//R1
if(Row == 4'b0111) begin
DecodeOut <= 4'b1010; //A
col4Debouncer <=5'b11111;
end
//R2
else if(Row == 4'b1011) begin
DecodeOut <= 4'b1011; //B
col4Debouncer <=5'b11111;
end
//R3
else if(Row == 4'b1101) begin
DecodeOut <= 4'b1100; //C
col4Debouncer <=5'b11111;
end
//R4
else if(Row == 4'b1110) begin
DecodeOut <= 4'b1101; //D
col4Debouncer <=5'b11111;
end else begin
col4Debouncer <= {1'b0, col4Debouncer[4:1]};
end
sclk <= 20'b00000000000000000000;
end
// Otherwise increment
else begin
sclk <= sclk + 1'b1;
end
end
endmodule