-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreading-4x4-matrix-keypad-pic16f877a.c
136 lines (125 loc) · 3.52 KB
/
reading-4x4-matrix-keypad-pic16f877a.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
#define LED1 RB0_bit
#define LED2 RB1_bit
#define LED3 RB2_bit
#define LED4 RB3_bit
#define LED5 RB4_bit
#define LED6 RB5_bit
#define LED7 RB6_bit
#define LED8 RB7_bit
#define KEYPAD_R1 RD0_bit
#define KEYPAD_R2 RD1_bit
#define KEYPAD_R3 RD2_bit
#define KEYPAD_R4 RD3_bit
#define KEYPAD_C1 RD4_bit
#define KEYPAD_C2 RD5_bit
#define KEYPAD_C3 RD6_bit
#define KEYPAD_C4 RD7_bit
#define KEYPAD_R1_DIR TRISD0_bit
#define KEYPAD_R2_DIR TRISD1_bit
#define KEYPAD_R3_DIR TRISD2_bit
#define KEYPAD_R4_DIR TRISD3_bit
unsigned char Read_Key(unsigned char row, unsigned char column);
void main()
{
TRISB=0x00;
TRISD=0xFF;
LED1=0;
LED2=0;
LED3=0;
LED4=0;
LED5=0;
LED6=0;
LED7=0;
LED8=0;
while(1)
{
if(Read_Key(1,1)==0)
{
LED1=1; //turn on LED1 if pressed
}
else if(Read_Key(1,2)==0)
{
LED2=1; //turn on LED2 if pressed
}
else if(Read_Key(1,3)==0)
{
LED3=1; //turn on LED3 if pressed
}
else if(Read_Key(2,1)==0)
{
LED4=1; //turn on LED4 if pressed
}
else if(Read_Key(2,2)==0)
{
LED5=1; //turn on LED5 if pressed
}
else if(Read_Key(2,3)==0)
{
LED6=1; //turn on LED6 if pressed
}
else if(Read_Key(3,1)==0)
{
LED7=1; //turn on LED7 if pressed
}
else if(Read_Key(3,2)==0)
{
LED8=1; //turn on LED8 if pressed
}
else
{
LED1=0; //turn off all LED
LED2=0;
LED3=0;
LED4=0;
LED5=0;
LED6=0;
LED7=0;
LED8=0;
}
}
}
unsigned char Read_Key(unsigned char row, unsigned char column)
{
KEYPAD_R1_DIR=1;
KEYPAD_R2_DIR=1;
KEYPAD_R3_DIR=1;
KEYPAD_R4_DIR=1;
switch(row)
{
case 1: //Row 1 selected
KEYPAD_R1_DIR=0; //set Row 1 Pin as output
KEYPAD_R1=0; //set Row 1 Pin output 0
break;
case 2: //Row 2 selected
KEYPAD_R2_DIR=0; //set Row 2 Pin as output
KEYPAD_R2=0; //set Row 2 Pin output 0
break;
case 3: //Row 3 selected
KEYPAD_R3_DIR=0; //set Row 3 Pin as output
KEYPAD_R3=0; //set Row 3 Pin output 0
break;
case 4: //Row 4 selected
KEYPAD_R4_DIR=0; //set Row 4 Pin as output
KEYPAD_R4=0; //set Row 4 pin output 0
break;
}
switch(column)
{
case 1: //column 1 selected
if(KEYPAD_C1==0)return 0; //return 0 if pin is zero
else return 1;
break;
case 2: //column 2 selected
if(KEYPAD_C2==0)return 0; //return 0 if pin is zero
else return 1;
break;
case 3: //column 3 selected
if(KEYPAD_C3==0)return 0; //return 0 if pin is zero
else return 1;
break;
case 4: //column 4 selected
if(KEYPAD_C4==0)return 0; //return 0 if pin is zero
else return 1;
break;
}
}