-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboardtest.c
191 lines (173 loc) · 2.94 KB
/
keyboardtest.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
#include<reg52.h>
#include<intrins.h>
#define lcdData P3
#define keyport P0
#define clearDisplay 0x01
#define cursorBlink 0x0E
#define twoLines 0x38
#define G1 0x80
#define G2 0x88
#define G3 0xC0
#define G4 0xC8
sbit RS = P1^4;
sbit RW = P1^5;
sbit E = P1^6;
sbit Busy = P3^0;
void lcdInit(void);
void lcdCmdWrite(unsigned char );
void lcdDataWrite(unsigned char );
void lcdPrint(char *string);
void MSdelay(unsigned int);
unsigned char inputkey(void);
void main()
{
lcdInit();
while(1){
inputkey();
MSdelay(2000);
}
}
unsigned char inputkey(void)
{
unsigned char check;
unsigned char pressed = 0;
keyport = 0x0f;
check = keyport;
check &= 0x0f;
while(check != 0x0f){
lcdCmdWrite(clearDisplay);
lcdPrint("Waiting to release");
MSdelay(100);
check = keyport;
check &= 0x0f;
}
MSdelay(5);
lcdCmdWrite(clearDisplay);
lcdPrint("Press a key");
while(pressed == 0){
check = keyport;
check &= 0x0f;
if(check != 0x0f){
MSdelay(5);
check = keyport;
check &= 0xf;
if(check != 0x0f){
pressed = 1;
}
else{
pressed = 0;
}
}
}
/* checking col 1 */
keyport = 0xef;
check = keyport;
check &= 0x0f;
if(check != 0x0f){
lcdCmdWrite(clearDisplay);
if(check == 0x0e){
lcdPrint("Key : 1");
return;
}
if(check == 0x0d){
lcdPrint("Key : 4");
return;
}
if(check == 0x0b){
lcdPrint("Key : 7");
return;
}
if(check == 0x07){
lcdPrint("Key : Enter");
return;
}
}
/* checking col 2 */
keyport = 0xdf;
check = keyport;
check &= 0x0f;
if(check != 0x0f){
lcdCmdWrite(clearDisplay);
if(check == 0x0e){
lcdPrint("Key : 2");
return;
}
if(check == 0x0d){
lcdPrint("Key : 5");
return;
}
if(check == 0x0b){
lcdPrint("Key : 8");
return;
}
if(check == 0x07){
lcdPrint("Key : 0");
return;
}
}
/* checking col 3 */
keyport = 0xbf;
check = keyport;
check &= 0x0f;
if(check != 0x0f){
lcdCmdWrite(clearDisplay);
if(check == 0x0e){
lcdPrint("Key : 3");
return;
}
if(check == 0x0d){
lcdPrint("Key : 6");
return;
}
if(check == 0x0b){
lcdPrint("Key : 9");
return;
}
if(check == 0x07){
lcdPrint("Key : Return");
return;
}
}
}
void MSdelay(unsigned int time)
{
unsigned int i,j;
for(i = 0; i < time; i++)
{
for(j=0; j<165; j++);
}
}
void lcdInit()
{
lcdCmdWrite(clearDisplay);
lcdCmdWrite(cursorBlink);
lcdCmdWrite(twoLines);
lcdCmdWrite(G1);
}
void lcdCmdWrite(unsigned char command)
{
RS = 0;
RW = 0;
lcdData = command;
E = 1;
MSdelay(5);
E = 0;
MSdelay(10);
}
void lcdDataWrite(unsigned char Data)
{
RS = 1;
RW = 0;
lcdData = Data;
E = 1;
MSdelay(5);
E = 0;
MSdelay(10);
}
void lcdPrint(char *string)
{
while(*string > 0)
{
lcdDataWrite(*string++);
}
}