-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicKeypadScan.cpp
More file actions
354 lines (309 loc) · 8.85 KB
/
Copy pathbasicKeypadScan.cpp
File metadata and controls
354 lines (309 loc) · 8.85 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// ARDUINO MEGA, NOT UNO!
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
byte c = 0;
char curChar = 0;
bool validISR = false;
bool buttonReleased = false;
unsigned long lastRegisteredPress, durationBetweenPress;
#define _IO_Mem_reg(address) *((volatile byte*) (address))
#define DataDirReg_D _IO_Mem_reg(0x2A)
#define DataDirReg_H _IO_Mem_reg(0x101)
#define DataDirReg_J _IO_Mem_reg(0x104)
// Pins 18-21
#define PORT_D_REG _IO_Mem_reg(0x2B)
// PORT is for outputs. PIN is for reading inputs!
#define PIN_D_REG _IO_Mem_reg(0x29)
// Pins 16-17
#define PORT_H_REG _IO_Mem_reg(0x102)
#define PIN_H_REG _IO_Mem_reg(0x100)
// Pins 14-15
#define PORT_J_REG _IO_Mem_reg(0x105)
#define PIN_J_REG _IO_Mem_reg(0x103)
// The compiler actually doesn't allow these addresses. Interesting
// Must use the predefined macros
// #define Extern_Inp_Ctrl_Reg(0x69)
// #define Extern_Inp_Mask_Reg(0x3D)
char keyMap[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
/*
MEGA2560 Interrupt pins:
18–21
*/
void setup() {
/*
Since columns are interrupts, set them to be INPUT_PULLUP
*/
/*
We want to set pins 18–21 to INPUT_PULLUP;
INPUT_PULLUP is 0x2, and this is because the DDR should
be set to 0, and the PORT should be set to 1
X &= ~0x0F <=> X & 0xF0 = left four bits are preserved, right
four bits are set to 0
*/
DataDirReg_D &= ~0x0F;
// Set PD0-3 as 1
PORT_D_REG |= 0x0F;
// Set DDR[H/J] bits 0-1 to 1
DataDirReg_H |= 0x03;
DataDirReg_J |= 0x03;
// Set ports to 0 for Output LOW
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
// // We want rising edge, so both ISC bits should be 1 for each interrupt pin.
// // Since we're using INT0-4, set all bits to 1
// EICRA = 0xFF;
/*
Wrong! The colums are PULLUP, so they are default HIGH. If there is a key
press, they switch from HIGH to LOW, which is FALLING edge, not RISING!
*/
EICRA = 0xAA; // 0b10101010 -> Falling edge for INT0-INT3
// We only want to enable INT0-4, not all 8
EIMSK |= 0x0F;
lastRegisteredPress = micros();
Serial.begin(9600);
}
ISR(INT0_vect) {
// Serial.println("Something happened!" + (String)c + (String)(3-c));
/*
Set ports to 1 for Output HIGH. Now, the pressed column is
also high, and will only be LOW when the row physically connected
to the column (i.e. where the press/contact is) is driven low
(i.e. we should still see HIGH if we drive a row not connected
to the column to LOW)
*/
lastRegisteredPress = micros();
c = 0;
curChar = 0;
PORT_H_REG |= 0x03;
PORT_J_REG |= 0x03;
// PORT_J_REG &= ~(1 << 1); // Same thing
PORT_J_REG &= ~0x2;
/*
This was the trickiest part. The logic, without this for
loop, is correct. However, without the for loop, the CPU
actually runs faster than the pin has time to discharge.
The pin seems to discharge at approximately the time it takes
to get to the next row, so it thinks that the preceeding row
was pressed!
This for loop is a busy-wait, but necessary for correct row detection
The reason this works is because PORT_J_REG is a volatile variable.
If any variable that is not marked volatile is used, this loop will
be optimized out by the compiler because the state is determined
and unchanged
*/
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
// 3-c because I want to keep the array mapped with how the
// physical keypad looks; changing the mapping thru software
curChar = keyMap[0][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
// 2. Clear any pending interrupt flags caused by dropping the rows to LOW!
// Writing 1 to INTF0, INTF1, INTF2, and INTF3 clears them.
// Also, this MUST happen right after driving the lines low; doing it after
// the very next line of code didn't make it work
EIFR = 0x0F;
validISR = true;
return;
}
PORT_J_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[1][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_H_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[2][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_H_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[3][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
// If we're here, somehow we didn't find a valid key. Don't give one
curChar = 0;
}
ISR(INT1_vect) {
// Serial.println("Something happened!" + (String)c + (String)(3-c));
lastRegisteredPress = micros();
c = 1;
curChar = 0;
PORT_H_REG |= 0x03;
PORT_J_REG |= 0x03;
PORT_J_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[0][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_J_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[1][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_H_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[2][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_H_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[3][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
curChar = 0;
}
ISR(INT2_vect) {
// Serial.println("Something happened!" + (String)c + (String)(3-c));
lastRegisteredPress = micros();
c = 2;
curChar = 0;
PORT_H_REG |= 0x03;
PORT_J_REG |= 0x03;
PORT_J_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[0][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_J_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[1][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_H_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[2][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_H_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[3][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
curChar = 0;
}
ISR(INT3_vect) {
// Serial.println("Something happened!" + (String)c + (String)(3-c));
lastRegisteredPress = micros();
c = 3;
curChar = 0;
PORT_H_REG |= 0x03;
PORT_J_REG |= 0x03;
PORT_J_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[0][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_J_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[1][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_H_REG &= ~0x2;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[2][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
PORT_H_REG &= ~0x1;
for (int i = 0; i < 1000; i++) { PORT_J_REG = PORT_J_REG; }
if ( !((PIN_D_REG >> c) & 0x1) ) {
curChar = keyMap[3][3-c];
PORT_H_REG &= ~0x03;
PORT_J_REG &= ~0x03;
EIFR = 0x0F;
validISR = true;
return;
}
}
void loop() {
// prevChar = curChar;
// curChar = listenForKey();
// If the printed character changed OR there was a release of the key
if (validISR && buttonReleased && curChar) {
cli();
durationBetweenPress = micros()-lastRegisteredPress;
sei();
if (durationBetweenPress > 50) {
// Serial.println("Duration: " + (String)durationBetweenPress);
Serial.println(curChar);
}
validISR = buttonReleased = false;
} else {
buttonReleased = true;
}
}