/
Keypad.c
630 lines (570 loc) · 17.3 KB
/
Keypad.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
* File: Keypad.c
* Author: Shamnad
*
* Created on 12 October 2014, 03:00
*/
// include the header for Keypad library:
#include "Keypad.h"
#include "lcd.h"
/*******************************************************************************
* This file provides the functions for the Custom Matrix Keypad
*******************************************************************************/
/*
Matrix Keypad Library for PIC16F887
================================================================================
* Usages examples
* ----------------------------------------------------------------------------
* Keypad_begin(WITH_ISR) - Initialise the Keypad with Keypad Interrupt Enabled(WITH_ISR or WITHOUT_ISR)
* Key = Keypad_waitForKey() - Wait until a Key is pressed(It blocks the execution)
* Key = Keypad_getKey() - Get the Key if pressed(Non-blocking)
* Stat = Keypad_getKeyState(2,3) - Get status of Key positioned at ROW 2 and COL 3(PRESSED or RELEASED)
*
* Key = Keypad_ISR() - ISR for Keypad to call from MAIN Interrupt routine
This code is written utilizing the "Interrupt on Pin Change" feature of PORTB
in PICmicro devices when working with Keypad INTERRUPT Enabled. Keep the
COLUMN connections to PORTB4-7 to maintain compatibility with all PICmicro series.
The circuit:
* Keypad Row 1 pin any available port define as KP_ROW1 in Keypad.h
* Keypad Row 2 pin any available port define as KP_ROW2 in Keypad.h
* Keypad Row 3 pin any available port define as KP_ROW3 in Keypad.h
* Keypad Row 4 pin any available port define as KP_ROW4 in Keypad.h
* Keypad Column 1 pin connected to PORTB4 define as KP_COL1 in Keypad.h
* Keypad Column 2 pin connected to PORTB5 define as KP_COL2 in Keypad.h
* Keypad Column 3 pin connected to PORTB6 define as KP_COL3 in Keypad.h
* Keypad Column 4 pin connected to PORTB7 define as KP_COL4 in Keypad.h
* 4 x 10K resistor: Pullup Resistors need to be connected between Keypad Column pins and VCC.
* --Only needed if Keypad Column pins connected to ports other than PORTB--
*
* Direction Controls defined in Keypad.h
* eg:-
* #define KP_ROW1_dir TRISB0
* #define KP_ROW2_dir TRISB1
* #define KP_ROW3_dir TRISB2
* #define KP_ROW4_dir TRISB3
* #define KP_COL1_dir TRISB4
* #define KP_COL2_dir TRISB5
* #define KP_COL3_dir TRISB6
* #define KP_COL4_dir TRISB7
*/
/*******************************************************************************
* PRIVATE GLOBAL VARIABLES *
*******************************************************************************/
/*******************************************************************************
* PUBLIC FUNCTION: Keypad_busEnable
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the Matrix Keypad bus.
*
*******************************************************************************/
void Keypad_busEnable()
{
// This is for 4x4 Matrix keypad. Customize this code for any other size
KP_COL1 = 1;
KP_COL2 = 1;
KP_COL3 = 1;
KP_COL4 = 1;
KP_ROW1 = 1;
KP_ROW2 = 1;
KP_ROW3 = 1;
KP_ROW4 = 1;
KP_ROW1_dir = OUTPUT;
KP_ROW2_dir = OUTPUT;
KP_ROW3_dir = OUTPUT;
KP_ROW4_dir = OUTPUT;
KP_COL1_dir = INPUT;
KP_COL2_dir = INPUT;
KP_COL3_dir = INPUT;
KP_COL4_dir = INPUT;
KP_ROW1 = 1;
KP_ROW2 = 1;
KP_ROW3 = 1;
KP_ROW4 = 1;
KP_COL1 = 1;
KP_COL2 = 1;
KP_COL3 = 1;
KP_COL4 = 1;
PULLUP_ENABLE;
// Enable all individual pullups. Ports configured as OUTPUT get it disabled
//automatically. Change this if you want to disable any port pullups.
WPUB = 0xFF;
}
/*******************************************************************************
* PUBLIC FUNCTION: LCD_busEnable
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the LCD Keypad bus.
*
*******************************************************************************/
void LCD_busEnable()
{
KP_ROW1 = 1;
KP_ROW2 = 1;
KP_ROW3 = 1;
KP_ROW4 = 1;
LCD_4 = 1; // Data bus = 0
LCD_5 = 1;
LCD_6 = 1;
LCD_7 = 1;
LCD_RS_dir = OUTPUT; // Set as Output
LCD_E_dir = OUTPUT;
LCD_4_dir = OUTPUT;
LCD_5_dir = OUTPUT;
LCD_6_dir = OUTPUT;
LCD_7_dir = OUTPUT;
LCD_RS = 1; // RS = 0
LCD_E = 0; // E = 0
LCD_4 = 1; // Data bus = 0
LCD_5 = 1;
LCD_6 = 1;
LCD_7 = 1;
}
/*******************************************************************************
* PUBLIC FUNCTION: Keypad_ALTgetKey
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the Matrix Keypad.
*
*******************************************************************************/
unsigned char Keypad_ALTgetKey()
{
unsigned char Key = 0;
Keypad_busEnable();
KP_ROW1 = LOW;
KP_ROW2 = LOW;
KP_ROW3 = LOW;
KP_ROW4 = LOW;
if (KP_COL1==1 && KP_COL2==1 && KP_COL3==1 && KP_COL4==1)
{
return Key;
}
else
{
// Start Row Scaning
for (unsigned char row=1; row<=ROWS; row++)
{
Select_ROW(row);
Key = findKey_inRow(row);
if (Key)
break; // Break the scan if any Key found
}
LCD_busEnable();
__delay_ms(KEYHOLD_DELAY); //Apply a Typematic/Hold delay
return Key;
}
}
/*******************************************************************************
* PUBLIC FUNCTION: Keypad_ALTgetKey
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the Matrix Keypad.
*
*******************************************************************************/
unsigned char Keypad_ALTwaitForKey()
{
unsigned char Key = 0;
Keypad_busEnable();
while(Key==0) // Loop and wait for Key
{
// Start Row Scaning
for (unsigned char row=1; row<=ROWS; row++)
{
Select_ROW(row);
Key = findKey_inRow(row);
if (Key)
break; // Break the scan if any Key found
}
}
LCD_busEnable();
__delay_ms(KEYHOLD_DELAY); //Apply a Typematic/Hold delay
return Key;
}
/*******************************************************************************
* PUBLIC FUNCTION: Keypad_begin
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the Matrix Keypad.
*
*******************************************************************************/
void Keypad_begin(unsigned char Keypad_ISR_Enable)
{
// This is for 4x4 Matrix keypad. Customize this code for any other size
KP_ROW1_dir = OUTPUT;
KP_ROW2_dir = OUTPUT;
KP_ROW3_dir = OUTPUT;
KP_ROW4_dir = OUTPUT;
KP_COL1_dir = INPUT;
KP_COL2_dir = INPUT;
KP_COL3_dir = INPUT;
KP_COL4_dir = INPUT;
KP_ROW1 = 0;
KP_ROW2 = 0;
KP_ROW3 = 0;
KP_ROW4 = 0;
KP_COL1 = 1;
KP_COL2 = 1;
KP_COL3 = 1;
KP_COL4 = 1;
PULLUP_ENABLE;
// Enable all individual pullups. Ports configured as OUTPUT get it disabled
//automatically. Change this if you want to disable any port pullups.
WPUB = 0xFF;
if (Keypad_ISR_Enable)
{
// Enable Interrupt-on-change feature
KP_COL1_interrupt = ENABLE;
KP_COL2_interrupt = ENABLE;
KP_COL3_interrupt = ENABLE;
KP_COL4_interrupt = ENABLE;
RBIF = 0; // Initialize interrupt flag
RBIE = ENABLE; // Enable Keypad Interrupt
GIE = ENABLE; // Enable Global Interrupt
}
}
/*******************************************************************************
* PUBLIC FUNCTION: Keypad_waitForKey
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ unsigned char - Return pressed KEY
*
* DESCRIPTIONS:
* This function will wait forever until someone presses a key.
* Warning: It blocks all other code until a key is pressed.
* That means no blinking LED's, no LCD screen updates,
* no nothing with the exception of interrupt routines.
*
*******************************************************************************/
unsigned char Keypad_waitForKey(void)
{
unsigned char Key = 0;
while(Key==0)
Key = Keypad_getKey();
return Key;
}
/*******************************************************************************
* PUBLIC FUNCTION: Keypad_getKey
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ unsigned char - Return pressed KEY
*
* DESCRIPTIONS:
* Returns the key that is pressed, if any. This function is non-blocking.
*
*******************************************************************************/
unsigned char Keypad_getKey()
{
unsigned char Key = 0;
if (KP_COL1==1 && KP_COL2==1 && KP_COL3==1 && KP_COL4==1)
{
return Key;
}
else
{
// Start Row Scaning
for (unsigned char row=1; row<=ROWS; row++)
{
Select_ROW(row);
Key = findKey_inRow(row);
if (Key)
break; // Break the scan if any Key found
}
// Back to normal
KP_ROW1 = LOW;
KP_ROW2 = LOW;
KP_ROW3 = LOW;
KP_ROW4 = LOW;
__delay_ms(KEYHOLD_DELAY); //Apply a Typematic/Hold delay
return Key;
}
}
/*******************************************************************************
* PUBLIC FUNCTION: Select_ROW
*
* PARAMETERS:
* ~ row - Row position of the Key
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Activates the selected Row of Keypad
*
*******************************************************************************/
void Select_ROW(unsigned char row)
{
//De-activate all Rows
KP_ROW1 = HIGH;
KP_ROW2 = HIGH;
KP_ROW3 = HIGH;
KP_ROW4 = HIGH;
switch (row)
{
case 1:
// Enable first row
KP_ROW1 = LOW;
break;
case 2:
// Enable second row
KP_ROW2 = LOW;
break;
case 3:
// Enable third row
KP_ROW3 = LOW;
break;
case 4:
// Enable fourth row
KP_ROW4 = LOW;
break;
}
}
/*******************************************************************************
* PUBLIC FUNCTION: findKey_inRow
*
* PARAMETERS:
* ~ row - Row position of the Key
*
* RETURN:
* ~ unsigned char - Return Key Value
*
* DESCRIPTIONS:
* Finds the Key Value for a given Row
*
*******************************************************************************/
unsigned char findKey_inRow(unsigned char row)
{
unsigned char Key = 0;
static bit Key_Confirmed = FALSE;
// Check each column
for (unsigned char col=1; col<=COLS; col++)
{
//Check selected column
Key_Confirmed = Confirm_COL(col);
if (Key_Confirmed)
{
Key = keys[row-1][col-1]; //Retrieve Key Value
break;
}
}
return Key;
}
/*******************************************************************************
* PUBLIC FUNCTION: Keypad_getKeyState
*
* PARAMETERS:
* ~ row - Row position of the Key
* ~ Column - Column position of the Key
*
* RETURN:
* ~ unsigned char - Return present status of the Key
*
* DESCRIPTIONS:
* Returns the current state of any of the keys.
* The states are PRESSED and RELEASED.
*
*******************************************************************************/
unsigned char Keypad_getKeyState(unsigned char row, unsigned char col)
{
static bit Key_pressed = FALSE;
Select_ROW(row);
Key_pressed = Confirm_COL(col);
// Back to normal
KP_ROW1 = LOW;
KP_ROW2 = LOW;
KP_ROW3 = LOW;
KP_ROW4 = LOW;
if(Key_pressed)
return PRESSED;
else
return RELEASED;
}
/*******************************************************************************
* PUBLIC FUNCTION: Confirm_COL
*
* PARAMETERS:
* ~ Column - Column position of the Key
*
* RETURN:
* ~ bit - Confirm a key status of a selected Column
*
* DESCRIPTIONS:
* Returns the current state of any of the keys.
* Confirms the Key is Pressed or NOT(TRUE/FALSE).
*******************************************************************************/
bit Confirm_COL(unsigned char col)
{
static bit Key_pressed = 0;
switch (col)
{
case 1:
//Check first column
if (KP_COL1==0 && KP_COL2==1 && KP_COL3==1 && KP_COL4==1)
{
__delay_ms(DEBOUNCE_DELAY);
//Confirm key
if (KP_COL1==0 && KP_COL2==1 && KP_COL3==1 && KP_COL4==1)
Key_pressed = TRUE; //Confirm Key is PRESSED
else
Key_pressed = FALSE; //Confirm Key is RELEASED
}
else
Key_pressed = FALSE; //Confirm Key is RELEASED
break;
case 2:
//Check second column
if (KP_COL1==1 && KP_COL2==0 && KP_COL3==1 && KP_COL4==1)
{
__delay_ms(DEBOUNCE_DELAY);
//Confirm key
if (KP_COL1==1 && KP_COL2==0 && KP_COL3==1 && KP_COL4==1)
Key_pressed = TRUE; //Confirm Key is PRESSED
else
Key_pressed = FALSE; //Confirm Key is RELEASED
}
else
Key_pressed = FALSE; //Confirm Key is RELEASED
break;
case 3:
//Check third column
if (KP_COL1==1 && KP_COL2==1 && KP_COL3==0 && KP_COL4==1)
{
__delay_ms(DEBOUNCE_DELAY);
//Confirm key
if (KP_COL1==1 && KP_COL2==1 && KP_COL3==0 && KP_COL4==1)
Key_pressed = TRUE; //Confirm Key is PRESSED
else
Key_pressed = FALSE; //Confirm Key is RELEASED
}
else
Key_pressed = FALSE; //Confirm Key is RELEASED
break;
case 4:
//Check fourth column
if (KP_COL1==1 && KP_COL2==1 && KP_COL3==1 && KP_COL4==0)
{
__delay_ms(DEBOUNCE_DELAY);
//Confirm key
if (KP_COL1==1 && KP_COL2==1 && KP_COL3==1 && KP_COL4==0)
Key_pressed = TRUE; //Confirm Key is PRESSED
else
Key_pressed = FALSE; //Confirm Key is RELEASED
}
else
Key_pressed = FALSE; //Confirm Key is RELEASED
break;
}
return Key_pressed;
}
/*******************************************************************************
* PUBLIC FUNCTION: Keypad_ISR
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ unsigned char - Return pressed KEY
*
* DESCRIPTIONS:
* This function will detect immediately someone presses a key.
* Since this is called from Interrupt, this wont block the main code
*
*******************************************************************************/
unsigned char Keypad_ISR(void)
{
RBIE = DISABLE; // Disable Keypad interrupt for a while
unsigned char Key = 0;
static bit Key_pressed = FALSE;
// Start Row Scaning
for (unsigned char row=1; row<=ROWS; row++)
{
Select_ROW(row);
// Check each column
for (unsigned char col=1; col<=COLS; col++)
{
//Check selected column
switch (col)
{
case 1:
//Check first column
if (KP_COL1==0 && KP_COL2==1 && KP_COL3==1 && KP_COL4==1)
Key_pressed = TRUE; //Confirm Key is PRESSED
else
Key_pressed = FALSE; //Confirm Key is RELEASED
break;
case 2:
//Check second column
if (KP_COL1==1 && KP_COL2==0 && KP_COL3==1 && KP_COL4==1)
Key_pressed = TRUE; //Confirm Key is PRESSED
else
Key_pressed = FALSE; //Confirm Key is RELEASED
break;
case 3:
//Check third column
if (KP_COL1==1 && KP_COL2==1 && KP_COL3==0 && KP_COL4==1)
Key_pressed = TRUE; //Confirm Key is PRESSED
else
Key_pressed = FALSE; //Confirm Key is RELEASED
break;
case 4:
//Check fourth column
if (KP_COL1==1 && KP_COL2==1 && KP_COL3==1 && KP_COL4==0)
Key_pressed = TRUE; //Confirm Key is PRESSED
else
Key_pressed = FALSE; //Confirm Key is RELEASED
break;
default:
Key_pressed = FALSE; //Nothing found
}
if (Key_pressed)
{
Key = keys[row-1][col-1]; //Retrieve Key Value
break;
}
}
if (Key)
break; // Break the scan if any Key found
}
// Back to normal
KP_ROW1 = LOW;
KP_ROW2 = LOW;
KP_ROW3 = LOW;
KP_ROW4 = LOW;
RBIF = 0; // Clear the Keypad interrupt Flag
RBIE = ENABLE; // Re-enable Keypad interrupt
return Key;
}