-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decoder.v
177 lines (158 loc) · 4.2 KB
/
Decoder.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
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
`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: Basys3
// 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(
clk,
Row,
Col,
DecodeOut
);
// ==============================================================================================
// Port Declarations
// ==============================================================================================
input clk; // 100MHz onboard clock
input [3:0] Row; // Rows on KYPD
output [3:0] Col; // Columns on KYPD
output [3:0] DecodeOut; // Output data
// ==============================================================================================
// Parameters, Regsiters, and Wires
// ==============================================================================================
// Output wires and registers
reg [3:0] Col;
reg [3:0] DecodeOut;
// Count register
reg [19:0] sclk;
// ==============================================================================================
// Implementation
// ==============================================================================================
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
end
//R2
else if(Row == 4'b1011) begin
DecodeOut <= 4'b0100; //4
end
//R3
else if(Row == 4'b1101) begin
DecodeOut <= 4'b0111; //7
end
//R4
else if(Row == 4'b1110) begin
DecodeOut <= 4'b0000; //0
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
end
//R2
else if(Row == 4'b1011) begin
DecodeOut <= 4'b0101; //5
end
//R3
else if(Row == 4'b1101) begin
DecodeOut <= 4'b1000; //8
end
//R4
else if(Row == 4'b1110) begin
DecodeOut <= 4'b1111; //F
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
end
//R2
else if(Row == 4'b1011) begin
DecodeOut <= 4'b0110; //6
end
//R3
else if(Row == 4'b1101) begin
DecodeOut <= 4'b1001; //9
end
//R4
else if(Row == 4'b1110) begin
DecodeOut <= 4'b1110; //E
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
end
//R2
else if(Row == 4'b1011) begin
DecodeOut <= 4'b1011; //B
end
//R3
else if(Row == 4'b1101) begin
DecodeOut <= 4'b1100; //C
end
//R4
else if(Row == 4'b1110) begin
DecodeOut <= 4'b1101; //D
end
sclk <= 20'b00000000000000000000;
end
// Otherwise increment
else begin
sclk <= sclk + 1'b1;
end
end
endmodule