-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorlock.c
259 lines (236 loc) · 4.99 KB
/
doorlock.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
// Inspired from https://github.com/kmhmubin/Password-based-doorlock-system-in-8051-microprocessor
// password based door lock system in 8051 microprocessor
#include <reg51.h>
// connected pins
// keypad rows
sbit keyrow1 = P2 ^ 0;
sbit keyrow2 = P2 ^ 1;
sbit keyrow3 = P2 ^ 2;
sbit keyrow4 = P2 ^ 3;
// keypad column
sbit keycolumn1 = P3 ^ 0;
sbit keycolumn2 = P3 ^ 1;
sbit keycolumn3 = P3 ^ 2;
// motor pins
sbit motorpin1 = P3 ^ 3;
sbit motorpin2 = P3 ^ 4;
// led pins
sbit rs = P3 ^ 5;
sbit rw = P3 ^ 6;
sbit en = P3 ^ 7;
// functions
void lcdcmd(unsigned char);
void lcddat(unsigned char);
void lcddisplay(unsigned char *q);
char keypad();
void check();
void delay(unsigned int);
unsigned char pin[] = {"12345"};
unsigned char Epin[5];
// main function
void main()
{
lcdcmd(0x0F); // decimal value: 15
lcdcmd(0x38); // decimal value: 56
lcdcmd(0x01); // decimal value: 1
while (1)
{
unsigned int i = 0;
lcdcmd(0x80); // decimal value: 128
lcddisplay("ENTER PIN NUMBER");
delay(1000);
lcdcmd(0xc0); // decimal value: 192
while (pin[i] != '\0')
{
Epin[i] = keypad();
delay(1000);
i++;
}
check();
}
}
// delay function
void delay(unsigned int j)
{
int a, b;
for (a = 0; a < j; a++)
{
for (b = 0; b < 10; b++)
;
}
}
// lcd commands functions
void lcdcmd(unsigned char A)
{
P1 = A;
rs = 0;
rw = 0;
en = 1;
delay(1000);
en = 0;
}
// lcd data function
void lcddat(unsigned char i)
{
P1 = i;
rs = 1;
rw = 0;
en = 1;
delay(1000);
en = 0;
}
// lcd display charecters function
void lcddisplay(unsigned char *q)
{
int k;
for (k = 0; q[k] != '\0'; k++)
{
lcddat(q[k]);
}
delay(10000);
}
// assign keypad character value function
char keypad()
{
int x = 0;
while (x == 0)
{
// assign values for first row
keyrow1 = 0;
keyrow2 = 1;
keyrow3 = 1;
keyrow4 = 1;
if (keycolumn1 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '1';
}
if (keycolumn2 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '2';
}
if (keycolumn3 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '3';
}
// assign values for second row
keyrow1 = 1;
keyrow2 = 0;
keyrow3 = 1;
keyrow4 = 1;
if (keycolumn1 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '4';
}
if (keycolumn2 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '5';
}
if (keycolumn3 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '6';
}
// assign values for third row
keyrow1 = 1;
keyrow2 = 1;
keyrow3 = 0;
keyrow4 = 1;
if (keycolumn1 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '7';
}
if (keycolumn2 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '8';
}
if (keycolumn3 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '9';
}
// assign values for forth row
keyrow1 = 1;
keyrow2 = 1;
keyrow3 = 1;
keyrow4 = 0;
if (keycolumn1 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '*';
}
if (keycolumn2 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '0';
}
if (keycolumn3 == 0)
{
lcddat('*');
delay(1000);
x = 1;
return '#';
}
}
}
// password check function and run the door motor
void check()
{
// compare the input value with the assign password value
if (pin[0] == Epin[0] && pin[1] == Epin[1] && pin[2] == Epin[2] && pin[3] == Epin[3] && pin[4] == Epin[4])
{
delay(1000);
lcdcmd(0x01); // decimal value: 1
lcdcmd(0x81); // decimal value: 129
// show pin is correct
lcddisplay("PIN CORRECT");
delay(1000);
// door motor will run
motorpin1 = 1;
motorpin2 = 0;
lcdcmd(0xc1); // decimal value: 193
// show the door is unlocked
lcddisplay("DOOR OPENED");
delay(10000);
motorpin1 = 1;
motorpin2 = 0;
lcdcmd(0x01); // decimal value: 1
}
else
{
lcdcmd(0x01); // decimal value: 1
lcdcmd(0x80); // decimal value: 128
lcddisplay("WRONG PIN");
delay(1000);
lcdcmd(0x01); // decimal value: 1
}
}
// end