-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassword based EVM 8051 code.c
558 lines (502 loc) · 12.3 KB
/
Password based EVM 8051 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*18BEC0042 - ELECTRONIC VOTING MACHINE USING 8051 - MICROCONTROLLERS PROJECT - RAW CODE */
#include<reg51.h> /*Importing Libraries for all 51 microcontroller Families*/
#define msec 50
#define lcd_data_str_pin P2
/*Declaring the Pins for Control Lines of LCD*/
sbit rs = P3^0; //Register select (RS) pin with 3.0
sbit rw = P3^1; //Read write(RW) pin with 3.1
sbit en = P3^6; //Enable(EN) pin with 3.6
sbit ini_pin = P1^0; // Start voting pin
sbit stop_pin = P1^5; // Stop voting pin
int i;
/*Keypad Connections */
sbit r1=P0^0;
sbit r2=P0^1;
sbit r3=P0^2;
sbit r4=P0^3;
sbit c1=P0^4;
sbit c2=P0^5;
sbit c3=P0^6;
void lcdcmd(unsigned char);
void lcddat(unsigned char);
void lcddis(unsigned char *,unsigned char);
void kdelay();
void check();
unsigned char pwd[10],x;
/*Declaring Pins for Candidate Buttons*/
sbit candidate_1=P1^1; //Candidate1 is assigned to Pin 1.1
sbit candidate_2=P1^2; //Candidate2 is assigned to Pin 1.2
sbit candidate_3=P1^3; //Candidate3 is assigned to Pin 1.3
sbit candidate_4=P1^4; //Candidate4 is assigned to Pin 1.4
/*Declaring the necessary variables*/
int max = 0;
int carry = 0;
int arr[4];
int vote_amt[3],j;
unsigned int vote_1,vote_2,vote_3,vote_4; /*Using Unsigned as votes are always positive*/
void delay(int delay_time) /*User Defined Time delay function. We have to maintain some delay for passing each value to screen between logic high and low*/
{
int j,k;
for(j=0;j<=delay_time;j++)
for(k=0;k<=1000;k++);
}
void lcd_cmd(unsigned char cmd_addr) //User Defined Function to send command to LCD
{
lcd_data_str_pin = cmd_addr;
en = 1;
rs = 0;
rw = 0;
delay(1);
en = 0;
return;
}
void lcd_data_str(char str[50]) //Function to send string
{
int p;
for (p=0;str[p]!='\0';p++)
{
lcd_data_str_pin = str[p];
rw = 0;
rs = 1;
en = 1;
delay(1);
en = 0;
}
return;
}
void lcd_data_int(unsigned int vote) //Function to send 0-9 character values
{
char dig_ctrl_var;
int p;
for (j=2;j>=0;j--)
{
vote_amt[j]=vote%10;
vote=vote/10;
}
for (p=0;p<=2;p++)
{
dig_ctrl_var = vote_amt[p]+48;
lcd_data_str_pin = dig_ctrl_var;
rw = 0;
rs = 1;
en = 1;
delay(1);
en = 0;
}
return;
}
void vote_count() // Function to count votes
{
while (candidate_1==0 && candidate_2==0 && candidate_3==0 && candidate_4==0);
if (candidate_1==1)
{
while (candidate_1 == 1);
{
vote_1 = vote_1 + 1;
}
}
if (candidate_2==1)
{
while (candidate_2 == 1);
{
vote_2 = vote_2 + 1;
}
}
if (candidate_3==1)
{
while (candidate_3 == 1);
{
vote_3 = vote_3 + 1;
}
}
if (candidate_4==1)
{
while (candidate_4 == 1);
{
vote_4 = vote_4 + 1;
}
}
}
void lcd_ini()
{
lcd_cmd(0x38); /*Command for 8bit 2 row configuration or 5x7 Matrix Crystal Activation*/
delay(msec); /*Delay between each command so that the machine/lcd can execute*/
lcd_cmd(0x0E);/*Command for turning on Display with Cursor Blinking*/
delay(msec);
lcd_cmd(0x01); /*Command for Clearing the Screen*/
delay(msec);
lcd_cmd(0x81);/*Command for Force Cursor to Begin in 1st Line*/
delay(msec);
lcd_data_str("Welcome!!!"); /*Displays Welcome Message*/
delay(100);
lcd_cmd(0x01); /*Command for clearing the Screen*/
delay(msec);
lcd_cmd(0x80); /*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str( "Press" );/*Displays "Press" */
delay(msec);
lcd_cmd(0x14);/*Command for shifting cursor position to right*/
delay(msec);
lcd_data_str("button");/*Displays "BUTTON" */
delay(msec);
delay(msec);
lcd_cmd(0xC0);/* Command for forcing cursor to begin in 2nd Line*/
delay(msec);
lcd_data_str("to");/*Displays TO */
delay(msec);
lcd_cmd(0x14);/*Command for shifting cursor position to right*/
delay(msec);
lcd_data_str("vote");/*Displays "VOTE" */
delay(100);
lcd_cmd(0x01);/*Command for clearing the Screen*/
delay(msec);
lcd_cmd(0x80);/*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("P1");/*Displays "P1" */
delay(msec);
lcd_cmd(0x84); /*Force cursor to jump to next bit giving one bit gap*/
delay(msec);
lcd_data_str("P2");/*Displays "P2" */
delay(msec);
lcd_cmd(0x88);/*Force cursor to jump to next bit giving one bit gap*/
delay(msec);
lcd_data_str("P3");/*Displays "P3" */
delay(msec);
lcd_cmd(0x8C);/*Force cursor to jump to next bit giving one bit gap*/
delay(msec);
lcd_data_str("P4");/*Displays "P4" */
delay(msec);
vote_count();
lcd_cmd(0x01);/*Command for clearing the Screen*/
delay(msec);
lcd_cmd(0x85);/*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("Thank");/*Displays 'THANK' */
delay(msec);
lcd_cmd(0x14);/*Command for shifting cursor position to right */
delay(msec);
lcd_data_str("You!!");/*Displays You */
delay(100);
}
void check(){
int i=1;
if(pwd[0]=='9'&&pwd[1]=='6'&&pwd[2]=='7'&&pwd[3]=='6'&&pwd[4]=='3'&&pwd[5]=='2'&&pwd[6]=='7'&&pwd[7]=='1'&&pwd[8]=='1'&&pwd[9]=='8')
{
lcd_cmd(0x01);
lcd_data_str("PASSWORD MATCHED");
delay(msec);
carry = 0;
lcd_cmd(0x01); /*Command for clearing the screen */
delay(msec);
lcd_cmd(0x80);/*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("Results");/*Displays 'RESULTS' */
delay(msec);
lcd_cmd(0x14);/*Command for shifting cursor position to right */
delay(msec);
lcd_data_str("Are");/*Displays 'ARE' */
delay(msec);
lcd_cmd(0x14);/*Command for shifting cursor position to right */
delay(msec);
lcd_data_str("Out");/* Displays 'OUT' */
delay(msec);
lcd_cmd(0x01); /*Command for clearing the screen */
delay(msec);
lcd_cmd(0x80);/*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("P1");/*Displays P1*/
delay(msec);
lcd_cmd(0x84);/*Force cursor to jump to next bit giving one bit gap*/
delay(msec);
lcd_data_str("P2");/*Displays P2*/
delay(msec);
lcd_cmd(0x88);/*Force cursor to jump to next bit giving one bit gap*/
delay(msec);
lcd_data_str("P3");/*Displays P3*/
delay(msec);
lcd_cmd(0x8C);/*Force cursor to jump to next bit giving one bit gap*/
delay(msec);
lcd_data_str("P4");/*Displays P4 */
delay(msec);
lcd_cmd(0xC0);/* Command for forcing cursor to begin in 2nd Line*/
delay(100);
lcd_data_int(vote_1);/*Displays how many votes are casted for P1 */
delay(msec);
lcd_cmd(0xC4);/*Force cursor to jump to next bit giving one bit gap in 2nd Line*/
delay(msec);
lcd_data_int(vote_2);/*Displays how many votes are casted for P2 */
delay(msec);
lcd_cmd(0xC8);/*Force cursor to jump to next bit giving one bit gap in 2nd Line*/
delay(msec);
lcd_data_int(vote_3);/*Displays how many votes are casted for P3 */
delay(msec);
lcd_cmd(0xCC);/*Force cursor to jump to next bit giving one bit gap in 2nd Line*/
delay(msec);
lcd_data_int(vote_4);/*Displays how many votes are casted for P4 */
delay(300);
/*Creating Numerical Arrays for storing no:of votes */
arr[0] = vote_1;
arr[1] = vote_2;
arr[2] = vote_3;
arr[3] = vote_4;
for(i=0; i<4; i++)
{
if(arr[i]>=max)
max = arr[i];
}
if ( (vote_1 == max) && ( vote_2 != max) && (vote_3 != max)&& (vote_4 != max) )
{
carry = 1;
lcd_cmd(0x01); /*Command for clearing the screen */
delay(msec);
lcd_cmd(0x82);/*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("Hurray!!!");/* Displays 'HURRAY' */
delay(50);
lcd_cmd(0xC4);/*Command for forcing the cursor to begin in 2nd Line*/
delay(msec);
lcd_data_str("P1");/* Displays 'P1' */
delay(msec);
lcd_cmd(0x14);/* Command for shifting cursor position to right */
delay(msec);
lcd_data_str("wins");/*Displays 'WINS' */
delay(msec);
}
if ( (vote_2 == max) && ( vote_1 != max) && (vote_3 != max)&& (vote_4 != max) )
{
carry = 1;
lcd_cmd(0x01);/*Command for clearing the screen */
delay(msec);
lcd_cmd(0x82);/*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("Hurray!!!");/* Displays 'HURRAY' */
delay(50);
lcd_cmd(0xC4);/*Command for forcing the cursor to begin in 2nd Line*/
delay(msec);
lcd_data_str("P2");/* Displays 'P2' */
delay(msec);
lcd_cmd(0x14);/* Command for shifting cursor position to right */
delay(msec);
lcd_data_str("wins");/*Displays 'WINS' */
delay(msec);
}
if ( (vote_3 == max) && ( vote_2 != max) && (vote_1 != max)&& (vote_4 != max) )
{
carry = 1;
lcd_cmd(0x01); /*Command for clearing the screen */
delay(msec);
lcd_cmd(0x82); /*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("Hurray!!!"); /* Displays 'HURRAY' */
delay(50);
lcd_cmd(0xC4); /*Command for forcing the cursor to begin in 2nd Line*/
delay(msec);
lcd_data_str("P3"); /* Displays 'P3' */
delay(msec);
lcd_cmd(0x14); /* Command for shifting cursor position to right */
delay(msec);
lcd_data_str("wins"); /*Displays 'WINS' */
delay(msec);
}
if ( (vote_4 == max) && ( vote_2 != max) && (vote_3 != max)&& (vote_1 != max) )
{
carry = 1;
lcd_cmd(0x01); /*Command for clearing the screen */
delay(msec);
lcd_cmd(0x82); /*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("Hurray!!!"); /* Displays 'HURRAY' */
delay(50);
lcd_cmd(0xC4); /*Command for forcing the cursor to begin in 2nd Line*/
delay(msec);
lcd_data_str("P4"); /* Displays 'P4' */
delay(msec);
lcd_cmd(0x14); /* Command for shifting cursor position to right */
delay(msec);
lcd_data_str("wins"); /*Displays 'WINS' */
delay(msec);
}
/* LCD Code for displaying message when tie occurs between two or more particiants */
if (carry==0)
{
/* Code for Displaying String 'CLASH BETWEEN' */
lcd_cmd(0x01); /*Command for clearing the screen */
delay(msec);
lcd_cmd(0x82); /*Command for forcing the cursor to begin in 1st Line*/
delay(msec);
lcd_data_str("clash");/* Displays CLASH*/
delay(50);
lcd_cmd(0x14); /* Command for shifting cursor position to right */
delay(msec);
lcd_data_str("between!!!"); /*Displays 'BETWEEN' */
delay(50);
if(vote_2 == max)
{
lcd_cmd(0xC5); /*Command for forcing the cursor to begin in 2nd Line with a gap of a bit*/
lcd_data_str("P2"); /*Displays 'P2' */
delay(50);
}
if(vote_3 == max)
{
lcd_cmd(0xC9); /*Command for forcing the cursor to begin in 2nd Line with a gap of a bit*/
lcd_data_str("P3"); /*Displays 'P3' */
delay(50);
}
if(vote_4 == max)
{
lcd_cmd(0xCD); /*Command for forcing the cursor to begin in 2nd Line with a gap of a bit*/
lcd_data_str("P4"); /*Displays 'P4' */
delay(50);
}
}
}
else
{
lcd_cmd(0x01);
lcd_data_str("PASSWORD WRONG");
}
}
void main() /*Main Function*/
{
ini_pin = stop_pin = 1;
vote_1 = vote_2 = vote_3 = vote_4 = 0;
candidate_1 = candidate_2 = candidate_3 = candidate_4 = 0;
lcd_cmd(0x38); /*Command for 8bit 2 row configuration or 5x7 Matrix Crystal Activation*/
delay(msec);
lcd_cmd(0x0E); /*Command for turning on Display with Cursor Blinking*/
delay(msec);
lcd_cmd(0x01);/*Command for clearing the screen */
delay(msec);
lcd_cmd(0x80);/* Command for forcing the cursor to begin in first line */
delay(msec);
lcd_data_str( "Press" );/* Displays 'PRESS' */
delay(msec);
lcd_cmd(0x14); /* Command for shifting Cursor position to Right */
delay(msec);
lcd_data_str("init"); /*Displays INIT */
delay(msec);
delay(msec);
lcd_cmd(0xC0); /* Command for forcing the cursor to begin in 2nd line */
delay(msec);
lcd_data_str("to");/* Displays 'TO' */
delay(msec);
lcd_cmd(0x14); /* Command for shifting Cursor position to Right */
delay(msec);
lcd_data_str("begin"); /* DISPLAYS 'BEGIN' */
delay(100);
while(1)
{
while(ini_pin != 0)
{
if (stop_pin == 0)
break;
}
if (stop_pin == 0)
{
break;
}
lcd_ini();
}
while(1)
{
lcd_cmd(0x38);
lcd_cmd(0x01);
lcd_cmd(0x10);
lcd_cmd(0x0c);
lcd_cmd(0x80);
lcd_data_str("PASSWORD PROTECTION");
lcd_cmd(0xc0);
lcd_data_str("SYSTEM");
lcd_cmd(0x01);
lcd_data_str("ENTER PASSWORD");
lcd_cmd(0xc0);
r1=0;
if(c1==0)
{
pwd[x]='0';
delay(msec);
lcd_data_str('*');
x=x+1;
}
if(c2==0)
{
pwd[x]='2';
delay(msec);
lcd_data_str('*');
x=x+1;
}
if(c3==0)
{
pwd[x]='3';
delay(msec);
lcd_data_str('*');
x=x+1;
}
r2=0;r1=1;
if(c1==0)
{
pwd[x]='4';
delay(msec);
lcd_data_str('*');
x=x+1;
}
if(c2==0)
{
pwd[x]='5';
delay(msec);
lcd_data_str('*');
x=x+1;
}
if(c3==0)
{
pwd[x]='6';
delay(msec);
lcd_data_str('*');
x=x+1;
}
r3=0;r2=1;
if(c1==0)
{
pwd[x]='7';
delay(msec);
lcd_data_str('*');
x=x+1;
}
if(c2==0)
{
pwd[x]='8';
delay(msec);
lcd_data_str('*');
x=x+1;
}
if(c3==0)
{
pwd[x]='9';
delay(msec);
lcd_data_str('*');
x=x+1;
}
r4=0;r3=1;
if(c1==0)
{
pwd[x]='*';
delay(msec);
lcd_data_str('*');
x=x+1;
}
if(c2==0)
{
pwd[x]='0';
delay(msec);
lcd_data_str('*');
x=x+1;
}
if(c3==0)
{
check();
delay(msec);
delay(msec);
}
r4=1;
}
}