-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_keyboard.c
246 lines (225 loc) · 7.2 KB
/
matrix_keyboard.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
//==================================================================================
//|| PROYECTO FINAL DE MICROCONTROLADORES ||
//|| ||
//|| Proyecto: Clave SHAVO ||
//|| Clase: Teclado Matricial ||
//|| Profesor: Dr. Rodolfo Castelló ||
//|| Integrantes: Jonathan Torres, Dilan Coss, Yamil Elías ||
//|| Escuela: Instituto Tecnológico de Estudios Superiores de Monterrey ||
//==================================================================================
#include <xc.h>
#include "functions.h" // Include our functions header file
unsigned char oneShotFlag = 0; // One-shot flag.
unsigned char DDRAMAddress = 0; // This is too watch out for pointer position
//rows of the keyboard matrix
#define row1 PORTBbits.RB0
#define row2 PORTBbits.RB1
#define row3 PORTBbits.RB2
#define row4 PORTBbits.RB3
//columns of the keyboard matrix
#define col1 PORTBbits.RB4
#define col2 PORTBbits.RB5
#define col3 PORTBbits.RB6
#define col4 PORTBbits.RB7
#define LED1 PORTDbits.RD0
#define LED2 PORTDbits.RD1
#define LED3 PORTDbits.RD2
#define LED4 PORTDbits.RD3
#define LED5 PORTDbits.RD4
#define LED6 PORTDbits.RD5
#define LED7 PORTDbits.RD6
#define LED8 PORTDbits.RD7
unsigned char columnSelected;
unsigned char rowSelected;
unsigned char keyCode;
void groundAllRows(){
row1 = 0;
row2 = 0;
row3 = 0;
row4 = 0;
}
void oneShot(){
if (oneShotFlag == 1) //IF One-Shot has been triggered THEN EXIT.
return;
delay(); //Eliminate Bouncing.
if (PORTB != 0xF0){ //IF a button is still pressed
pressedKey(); //a key was pressed, determine which key and act accordingly
oneShotFlag = 1; //Turn-ON one-shot flag.
}
}
void resetOneShot(){
if (PORTB == 0xF0) //IF RB0 OFF THEN Eliminate Bouncing
delay();
else //ELSE EXIT
return;
if (PORTB == 0XF0){ //IF RB0 OFF THEN reset one-shot flag
//turnOffAllLeds();
oneShotFlag = 0;
}
}
//10 ms delay
void delay(){
unsigned char L1REG = 0;
unsigned char L2REG;
while(L1REG++ < 13){
L2REG = 0;
while(L2REG++ < 255);
}
}
void turnOnAllRows(){
row1 = 1;
row2 = 1;
row3 = 1;
row4 = 1;
}
void determinePressedRow(){
char i;
for(i=0; i<4; i++){
turnOnAllRows();
switch(i){
case 0: {
row1 = 0;
if( (columnSelected == 1) && (col1 == 0)){
rowSelected = 1;
return;
}
if( (columnSelected == 2) && (col2 == 0)){
rowSelected = 1;
return;
}
if( (columnSelected == 3) && (col3 == 0)){
rowSelected = 1;
return;
}
if( (columnSelected == 4) && (col4 == 0)){
rowSelected = 1;
return;
}
}break;
case 1: {
row2 = 0;
if( (columnSelected == 1) && (col1 == 0)){
rowSelected = 2;
return;
}
if( (columnSelected == 2) && (col2 == 0)){
rowSelected = 2;
return;
}
if( (columnSelected == 3) && (col3 == 0)){
rowSelected = 2;
return;
}
if( (columnSelected == 4) && (col4 == 0)){
rowSelected = 2;
return;
}
}break;
case 2: {
row3 = 0;
if( (columnSelected == 1) && (col1 == 0)){
rowSelected = 3;
return;
}
if( (columnSelected == 2) && (col2 == 0)){
rowSelected = 3;
return;
}
if( (columnSelected == 3) && (col3 == 0)){
rowSelected = 3;
return;
}
if( (columnSelected == 4) && (col4 == 0)){
rowSelected = 3;
return;
}
}break;
case 3: {
row4 = 0;
if( (columnSelected == 1) && (col1 == 0)){
rowSelected = 4;
return;
}
if( (columnSelected == 2) && (col2 == 0)){
rowSelected = 4;
return;
}
if( (columnSelected == 3) && (col3 == 0)){
rowSelected = 4;
return;
}
if( (columnSelected == 4) && (col4 == 0)){
rowSelected = 4;
return;
}
}break;
}
}
}
unsigned char getKeyCode(){
if(rowSelected == 1){
switch(columnSelected){
case 1: return '1';
case 2: return '2';
case 3: return '3';
case 4: return 'A';
}
}
else if(rowSelected == 2){
switch(columnSelected){
case 1: return '4';
case 2: return '5';
case 3: return '6';
case 4: return 'B';
}
}
else if(rowSelected == 3){
switch(columnSelected){
case 1: return '7';
case 2: return '8';
case 3: return '9';
case 4: return 'C';
}
}
else if(rowSelected == 4){
switch(columnSelected){
case 1: return '*';
case 2: return '0';
case 3: return '#';
case 4: return 'D';
}
}
return -1;
}
void pressedKeyAction(unsigned char keyCode){
switch(keyCode){
case '1': displayOne(); break;
case '2': displayTwo(); break;
case '3': displayThree(); break;
case '4': displayFour(); break;
case '5': displayFive(); break;
case '6': displaySix(); break;
case '7': displaySeven(); break;
case '8': displayEight(); break;
case '9': displayNine(); break;
case '0': displayZero(); break;
case '*': command(0x01); isReady(); DDRAMAddress=0; break;
case '#': displaySOS(); break;
//case '*': displayLineOnLCD(GREETING,11); break;
//case '#': displayLineOnLCD(CASTELLO,11); break;
default: displayCharOnLCD(keyCode);
}
}
void pressedKey(){
switch(PORTB){ //obtain the selected column
case 0xE0: columnSelected = 1; break;
case 0xD0: columnSelected = 2; break;
case 0xB0: columnSelected = 3; break;
case 0x70: columnSelected = 4; break;
default: columnSelected = 100;
}
determinePressedRow();
keyCode = getKeyCode();
pressedKeyAction(keyCode);
//pressedKeyAction(columnSelected);
}