-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.c
439 lines (371 loc) · 8.62 KB
/
code.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//Beni-seuf University(BSU) Projects Using 8051 microcontrller
//contacts:kordymahmoud00@gmail.com
#include <REGX51.H>
#include <stdio.h>
#include <string.h>
#define keypad P1 //for port 1
#define LCD P2
sbit mrs_sensor=P3^2; //defrning pins
sbit pir_sensor=P3^3;
sbit rs =P3^4;
sbit en =P3^5;
sbit door = P3^6;
sbit bzr_h = P3^7;
sbit alarm_led = P0^5;
sbit pir_led = P0^6;
sbit rs_led = P0^7;
sbit try = P0^4;
int count = 1; //integer value called count = 1
unsigned int lcd_cursor; // An unsigned integer variable named lcd_cursor
char a = '1'; // a,b,c,d are charcters with values 1,2,5,8 and that is the password
char b = '2';
char c = '5';
char d = '8';
unsigned char e; // e,f,g,,h are charcters without values and will be compared to the a.b.c.d to make the password correct or not
unsigned char f;
unsigned char g;
unsigned char h;
void string(char *); //declring some functions and definening thier kinds
void dely(unsigned int dly);
void lcd_cmd(unsigned char ch);
void lcd_data(unsigned char ch);
void lcd_str(unsigned char *str);
char get_keypad_key(void);
void main(void)
{
char KeyVal =0; //initialzing pins
alarm_led = 0;
pir_led = 0;
rs_led = 0;
door = 1;
bzr_h = 1;
lcd_cmd(0x38); // Configure LCD
lcd_cmd(0x01); // Clear LCD // Clear LCD
lcd_cmd(0x10); // Shift cursor to the left
lcd_cmd(0x0C); // Turn on display, hide cursor
lcd_cmd(0x80); // Set cursor to the beginning of the first line
lcd_cmd(0x38); // Configure LCD
lcd_cmd(0x01); // Clear LCD // Clear LCD
lcd_cmd(0x10); // Shift cursor to the left
lcd_cmd(0x0C); // Turn on display, hide cursor
lcd_cmd(0x80); // Set cursor to the beginning of the first line
string("Welcome '_' ");
dely(100);
lcd_cmd(0x01); // Clear LCD
string("How are you ");
lcd_cmd(0x01); // Clear LCD
string("Dr.Lila ");
lcd_cmd(0x01); // Clear LCD
string("Beni-Seuf");
lcd_cmd(0x01); // Clear LCD
string("University(BSU)");
lcd_cmd(0x01); // Clear LCD
//contacts:kordymahmoud00@gmail.com
while(1)
{
string("Enter Password: ");
lcd_cmd(0x01); // Clear LCD
while(1)
{
alarm_led = 0;
dely(100);
if(lcd_cursor < 4)
{
lcd_cmd(0x88+lcd_cursor);
if(lcd_cursor == 1)
e = KeyVal;
if(lcd_cursor == 2)
f = KeyVal;
if(lcd_cursor == 3)
g = KeyVal;
}
if(lcd_cursor == 4)
{
dely(100);
h = KeyVal;
lcd_cmd(0x88 + lcd_cursor);
KeyVal = get_keypad_key();
lcd_data(KeyVal);
dely(100);
if(KeyVal == '*')
{
dely(200);
if(a == e && b == f && c == g && d == h) // that means the password is the values of a,b,c,d then press *
{
lcd_cmd(0x01); // Clear LCD
string("Correct");
dely(200);
lcd_cmd(0x01); // Clear LCD
string("Door is open Dr");
while(count < 3) //loop for 3 iterations
{
alarm_led = 1;
door = 0;
dely(300);
try = 1;
door = 1; //open the door
alarm_led = 0;
dely(300);
count++; //if the loop is not comppleted add 1
}
e = 'x'; //make e,f,g,h in x varieble
f = 'x';
g = 'x';
h = 'x';
count = 0;
while(pir_sensor == 0 && mrs_sensor == 0)
{
alarm_led = 1;
pir_led = 1;
rs_led = 1;
dely(200);
}
lcd_cmd(0x01); // Clear LCD
string("SECURITY VIOLATION");
dely(500);
lcd_cmd(0x01); // Clear LCD
lcd_cursor = 0;
while(count < 1)
{
string("Enter Password: ");
dely(50);
lcd_cmd(0x01); // Clear LCD
while(1)
{
alarm_led = 0;
pir_led = 0;
rs_led = 0;
door = 1;
dely(50);
if(lcd_cursor < 4)
{
lcd_cmd(0x88+lcd_cursor);
if(lcd_cursor == 1)
e = KeyVal;
if(lcd_cursor == 2)
f = KeyVal;
if(lcd_cursor == 3)
g = KeyVal;
dely(50);
}
if(lcd_cursor == 4)
{
h = KeyVal;
lcd_cmd(0x88 + lcd_cursor);
KeyVal = get_keypad_key();
lcd_data(KeyVal);
if(KeyVal == '*')
{
dely(200);
if(a == e && b == f && c == g && d == h)
{
lcd_cmd(0x01); // Clear LCD
string("correct Password ");
dely(200);
lcd_cmd(0x01); // Clear LCD
string("ALARM DISABLED");
alarm_led = 0;
pir_led = 0;
rs_led = 0;
door = 1;
while(1)
{
}
}
else
{
count = 0;
lcd_cmd(0x01); // Clear LCD
string("SECURITY VIOLATION");
while(count < 300)
{
alarm_led = 1;
pir_led = 1;
rs_led = 1;
bzr_h = 1;
SCON = 0X50;
TMOD = 0X20;
TH1 = -3;
TR1 = 1;
SBUF = '1';
while(TI == 0);
TI = 0;
dely(300);
alarm_led = 0;
pir_led = 0;
rs_led = 0;
bzr_h = 1;
dely(300);
}
}
}
else
{
lcd_cmd(0x01); // Clear LCD
lcd_cursor = 0;
break;
}
}
alarm_led = 1;
pir_led = 1;
rs_led = 1;
door = 0;
KeyVal = get_keypad_key();
lcd_data(KeyVal);
count++;
if (lcd_cursor > 4 || KeyVal == '#')
{
lcd_cmd(0x01); // Clear LCD
lcd_cursor = 0;
break;
}
}
}
}
else
{
lcd_cmd(0x01); // Clear LCD
string("WRONG Password");
dely(300);
bzr_h = 1;
dely(300);
bzr_h = 0;
dely(300);
alarm_led = 1;
pir_led = 0;
rs_led = 0;
bzr_h = 1;
dely(300);
break;
}
}
else
{
lcd_cmd(0x01); // Clear LCD
lcd_cursor = 0;
count = 0;
break;
}
}
alarm_led = 1;
KeyVal = get_keypad_key();
lcd_data(KeyVal);
if (lcd_cursor > 4 || KeyVal == '#')
{
lcd_cmd(0x01); // Clear LCD
lcd_cursor = 0;
count = 0;
break;
}
}
}
}
char get_keypad_key(void)
{
char key_val = 0;
keypad = 0xFE;
if (keypad == 0xEE) // button 1
{
key_val = '*';
lcd_cursor++;
}
if (keypad == 0xDE) // button 2
{
key_val = '0';
lcd_cursor++;
}
if (keypad == 0xBE) // button 3
{
key_val = '#';
lcd_cursor++;
}
keypad = 0xFD;
if (keypad == 0xED) // button 4
{
key_val = '7';
lcd_cursor++;
}
if (keypad == 0xDD) // button 5
{
key_val = '8';
lcd_cursor++;
}
if (keypad == 0xBD) // button 6
{
key_val = '9';
lcd_cursor++;
}
keypad = 0xFB;
if (keypad == 0xEB) // button 7
{
key_val = '4';
lcd_cursor++;
}
if (keypad == 0xDB) // button 8
{
key_val = '5';
lcd_cursor++;
}
if (keypad == 0xBB) // button 9
{
key_val = '6';
lcd_cursor++;
}
keypad = 0xF7;
if (keypad == 0xE7) // button *
{
key_val = '1';
lcd_cursor++;
}
if (keypad == 0xD7) // button 0
{
key_val = '2';
lcd_cursor++;
}
if (keypad == 0xB7) // button #
{
key_val = '3';
lcd_cursor++;
}
return key_val;
}
//contacts:kordymahmoud00@gmail.com
void lcd_str(unsigned char *str)
{
unsigned int loop =0;
for(loop =0; str[loop]!= '\0'; loop++)
{
lcd_data(str[loop]);
}
}
void lcd_data(unsigned char ch)
{
LCD = ch;
rs = 1;
en = 1;
dely(10);
en = 0;
}
void lcd_cmd(unsigned char ch)
{
LCD = ch;
rs = 0;
en = 1;
dely(10);
en = 0;
}
void string(char *p)
{
while(*p)
{
lcd_data(*p++);
}
}
void dely(unsigned int dly)
{
unsigned int loop =0;
unsigned int delay_gen =0;
for(loop =0; loop < dly; loop++)
for(delay_gen =0; delay_gen < 1000; delay_gen++);
}
//contacts:kordymahmoud00@gmail.com