-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_keypad.c
147 lines (122 loc) · 2.59 KB
/
matrix_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
/*
* File: matrix_keypad.c
* Author: shummu
*
* Created on 30 September, 2023, 4:27 PM
*/
#include <xc.h>
#include "matrix_keypad.h"
void init_matrix_keypad(void)
{
/* Setting the Columns as Inputs (RB2 - RB0)*/
//TRISB = TRISB | 0x07; xxxx xxxx | 0000 0111 = xxxx x111
MATRIX_KEYPAD_COLUMN_PORT_DDR = MATRIX_KEYPAD_COLUMN_PORT_DDR | 0x07;
/* Setting the Rows as Output (RD3 - RD0)*/
MATRIX_KEYPAD_ROW_PORT_DDR = MATRIX_KEYPAD_ROW_PORT_DDR & 0xF0; // TRISD = TRISD & 0xF0 -> (TRISD0 to TRISD3)
/* Enabling PORTB Pullups */
nRBPU = 0;
/* Making all the Rows HIGH to start with */
ROW1 = HI;
ROW2 = HI;
ROW3 = HI;
ROW4 = HI;
}
static unsigned char scan_keypad(void)
{
int i;
ROW1 = LOW;
ROW2 = HI;
ROW3 = HI;
ROW4 = HI;
for (i = 100; i--; );
if (COL1 == LOW)
{
return 1;
}
else if (COL2 == LOW)
{
return 2;
}
else if (COL3 == LOW)
{
return 3;
}
ROW1 = HI;
ROW2 = LOW;
ROW3 = HI;
ROW4 = HI;
for (i = 100; i > 0; i--)
{
;
}
if (COL1 == LOW)
{
return 4;
}
else if (COL2 == LOW)
{
return 5;
}
else if (COL3 == LOW)
{
return 6;
}
ROW1 = HI;
ROW2 = HI;
ROW3 = LOW;
ROW4 = HI;
for (i = 100; i > 0; i--);
if (COL1 == LOW)
{
return 7;
}
else if (COL2 == LOW)
{
return 8;
}
else if (COL3 == LOW)
{
return 9;
}
ROW1 = HI;
ROW2 = HI;
ROW3 = HI;
ROW4 = LOW;
for (i = 100; i > 0; i--);
if (COL1 == LOW)
{
return '*';
}
else if (COL2 == LOW)
{
return 0;
}
else if (COL3 == LOW)
{
return '#';
}
return ALL_RELEASED;
}
unsigned char read_matrix_keypad(unsigned char mode) // LEVEL STATE
{
static unsigned char once = 1;
unsigned char key;
key = scan_keypad(); // 1 2 3 4 5 6 7 8 9 0 '*' '#'
if (mode == LEVEL)
{
return key; //// 1 2 3 4 5 6 7 8 9 0 '*' '#' ALL_RELEASED
}
else // state change
{
if ((key != ALL_RELEASED) && once)
{
once = 0;
return key; // 1 2 3 4 5 6 7 8 9 0 '*' '#' ALL_RELEASED
}
else if (key == ALL_RELEASED)
{
once = 1;
}
}
return ALL_RELEASED;
}