-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimon_EN.c
671 lines (575 loc) · 22.4 KB
/
Simon_EN.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //
#include <usart.h> //
#include <USART/ubaud.c> //
#include <USART/ubusy.c> //
#include <USART/uclose.c> //
#include <USART/udefs.c> // Serial Communication
#include <USART/udrdy.c> // Library (Used for the
#include <USART/ugets.c> // LCD communication)
#include <USART/uopen.c> //
#include <USART/uputrs.c> //
#include <USART/uputs.c> //
#include <USART/uread.c> //
#include <USART/uwrite.c> //
#define _XTAL_FREQ 4000000 // Frequency used for PIC18F4520;
#define BAUD_RATE 9600 // Defining the BAUD frequency rate as 9600,
// therefore 9600 bits of data transfer per second;
#pragma config WDT = OFF // Turning off Watchdog;
#pragma config MCLRE = OFF // Turning off Master clear;
#define Button_1x1 PORTCbits.RC3 //
#define Button_1x2 PORTCbits.RC4 //
#define Button_1x3 PORTCbits.RC5 //
#define Button_1x4 PORTBbits.RB7 //
#define Button_2x1 PORTCbits.RC7 //
#define Button_2x2 PORTDbits.RD0 //
#define Button_2x3 PORTDbits.RD1 //
#define Button_2x4 PORTDbits.RD2 //
// Defining input
#define Button_3x1 PORTDbits.RD3 // ports
#define Button_3x2 PORTDbits.RD4 // (user's input)
#define Button_3x3 PORTDbits.RD5 //
#define Button_3x4 PORTDbits.RD6 //
#define Button_4x1 PORTDbits.RD7 //
#define Button_4x2 PORTEbits.RE0 //
#define Button_4x3 PORTEbits.RE1 //
#define Button_4x4 PORTEbits.RE2 //
#define Led_1x1 PORTBbits.RB3 //
#define Led_1x2 PORTBbits.RB4 //
#define Led_1x3 PORTBbits.RB5 //
#define Led_1x4 PORTBbits.RB6 //
#define Led_2x1 PORTAbits.RA6 //
#define Led_2x2 PORTBbits.RB0 //
#define Led_2x3 PORTBbits.RB1 //
#define Led_2x4 PORTBbits.RB2 //
// Defining output
#define Led_3x1 PORTAbits.RA1 // ports (LED output);
#define Led_3x2 PORTAbits.RA2 //
#define Led_3x3 PORTAbits.RA3 //
#define Led_3x4 PORTAbits.RA5 //
#define Led_4x1 PORTCbits.RC2 //
#define Led_4x2 PORTCbits.RC1 //
#define Led_4x3 PORTCbits.RC0 //
#define Led_4x4 PORTAbits.RA0 //
#define flag_timer_0 INTCONbits.TMR0IF // Defining Timer0 flags;
#define flag_timer_1 PIR1bits.TMR1IF // Defining Timer1 flags;
#define flag_transmiter PIR1bits.TXIF // Defining flags for data transmission;
#define Level_Max 128 // Defining the initialization size of the array that
// will be used to store the sequences;
//*******************************[Global Scope]********************************
int sequence_array [Level_Max]; // Array which will store the sequence;
int active_button_array [Level_Max]; // Array which will store the user's
// input;
int size_sa = 1; // Pseudo array size. This is the size which will be
// incremented or decremented on each level;
int init = 0; // Variable which will check if that's the first time the code
// is being executed;
long int time_sec = 5; // Waiting time for user's input, initial time is 5
// seconds, it increments in 5 seconds each 2 levels;
long int repet_time = 1; // Repetition for time count;
long int count; // Variable to keep track of the loop iterations for the
// timing procedure;
char level[]; // Buffer to store the converted string (This string shows the
// current level of the game);
char level_cd; // Variable necessary to make the conversion of the current
// level of the game;
char time[]; //Buffer to store the converted string (This string will show
// the remaining time for the user's input);
char time_cd; // Variable necessary to make the conversion of the remaining
// time for user's input;
int multi = 450; // Multiplier for the operation of counting the remaining
// time for user's input. This variable is necessary for
// making the games' counting mechanic closer to real time.
//*******************************[Global Scope]********************************
int LCD_Initialize(void) // LCD booting up;
{
__delay_ms(500);
int config = USART_TX_INT_ON & USART_RX_INT_OFF & USART_BRGH_HIGH & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_ADDEN_ON;
int spbrg = 25;
int baud = BAUD_IDLE_CLK_HIGH & BAUD_8_BIT_RATE & BAUD_WAKEUP_OFF & BAUD_AUTO_OFF;
baudUSART(baud);
OpenUSART(config, spbrg);
__delay_ms(500);
}
void LCD_Clear(void) // Clear LCD screen;
{
__delay_ms(10);
WriteUSART(0B11111110);
WriteUSART(0B00000001);
__delay_ms(10);
}
void LCD_Second_Line(void) // Move cursor to the second line in the LCD;
{
__delay_ms(10);
WriteUSART(0B11111110);
WriteUSART(0B11000000);
__delay_ms(10);
}
void LCD_Third_Line(void) // Move cursor to the third line in the LCD;
{
__delay_ms(10);
WriteUSART(0B11111110);
WriteUSART(0B10010100);
__delay_ms(10);
}
void LCD_Fourth_Line(void) // Move cursor to the fourth line in the LCD;
{
WriteUSART(0B11111110);
WriteUSART(0B11010100);
}
int random_sequence(void) // Generating random numbers from 0 to 15;
{
srand(TMR0);
return rand() % 16;
}
void adding_to_array(int num) // Adding the random numbers to the array which
// contains the correct sequence;
{
if(((size_sa - 1) == 0) && (init == 0))
{
sequence_array[size_sa - 1] = first_seq();
init = 1;
__delay_ms(1500);
}
else
{
sequence_array[size_sa - 1] = num;
}
}
int level_time(void) // Counting the remaining time for user's input for each
// level;
{
count = (time_sec * multi);
flag_timer_1 = 0;
TMR1H = 0x0B;
TMR1L = 0xDB;
T1CONbits.TMR1ON = 1;
while(1)
{
if((repet_time % multi) == 0)
{
time_cd = itoa(time, (repet_time / multi), 10);
LCD_Fourth_Line();
putsUSART(time_cd);
putsUSART("s ");
putsUSART(itoa(time, time_sec, 10));
putsUSART("s");
}
if(flag_timer_0 == 1)
{
flag_timer_0 = 0;
}
else
{
TMR0++;
}
if(flag_timer_1 == 1);
{
if(repet_time == count)
{
T1CONbits.TMR1ON = 0;
return 1;
}
else
{
T1CONbits.TMR1ON = 0;
repet_time++;
return 0;
}
}
}
}
void led_act_desact (int button_act, int state) // Activate and Deactivate
// LEDs;
{
switch(button_act)
{
case 0:
{
Led_1x1 = state;
break;
}
case 1:
{
Led_1x2 = state;
break;
}
case 2:
{
Led_1x3 = state;
break;
}
case 3:
{
Led_1x4 = state;
break;
}
case 4:
{
Led_2x1 = state;
break;
}
case 5:
{
Led_2x2 = state;
break;
}
case 6:
{
Led_2x3 = state;
break;
}
case 7:
{
Led_2x4 = state;
break;
}
case 8:
{
Led_3x1 = state;
break;
}
case 9:
{
Led_3x2 = state;
break;
}
case 10:
{
Led_3x3 = state;
break;
}
case 11:
{
Led_3x4 = state;
break;
}
case 12:
{
Led_4x1 = state;
break;
}
case 13:
{
Led_4x2 = state;
break;
}
case 14:
{
Led_4x3 = state;
break;
}
case 15:
{
Led_4x4 = state;
break;
}
}
}
int first_seq(void) // Reading user's input to START the game;
{
int flag = 0, button = 0, button_led = 0, first_numb;
LCD_Second_Line();
putsUSART(" Press any Key to");
LCD_Third_Line();
putsUSART(" Start the Game");
do
{
if(flag_timer_0 == 1)
{
flag_timer_0 = 0;
}
else
{
TMR0++;
}
first_numb = random_sequence();
int active_button [] = {Button_1x1,Button_1x2,Button_1x3,Button_1x4,\
Button_2x1,Button_2x2,Button_2x3,Button_2x4,\
Button_3x1,Button_3x2,Button_3x3,Button_3x4,\
Button_4x1,Button_4x2,Button_4x3,Button_4x4};
if(active_button[button] == 1)
{
flag = 1;
}
else
{
if(button == 15)
{
button = 0;
}
else
{
button++;
}
}
}while(flag == 0);
LCD_Clear();
return first_numb;
}
void button_act (void) // Reading user's input DURING the game and storing
// the input into an array for later comparison of the
// sequences;
{
int size_aba = size_sa;
int current_button_array = 0, button = 0, flag = 0;
repet_time = 1;
for(int init = 0; init < Level_Max; init++)
{
active_button_array[init] = -1;
}
LCD_Fourth_Line();
putsUSART("0 s ");
putsUSART(itoa(time, time_sec, 10));
putsUSART("s");
do
{
if(flag_timer_0 == 1)
{
flag_timer_0 = 0;
}
else
{
TMR0++;
}
int active_button [] = {Button_1x1,Button_1x2,Button_1x3,Button_1x4,\
Button_2x1,Button_2x2,Button_2x3,Button_2x4,\
Button_3x1,Button_3x2,Button_3x3,Button_3x4,\
Button_4x1,Button_4x2,Button_4x3,Button_4x4};
if(active_button[button] == 1)
{
led_act_desact(button, 1);
__delay_ms(250);
led_act_desact(button, 0);
active_button_array[current_button_array] = button;
current_button_array++;
if(button == 15)
{
button = 0;
}
else
{
button++;
}
}
else
{
if(button == 15)
{
button = 0;
}
else
{
button++;
}
}
for(int elem = 0; elem <= (size_aba -1); elem++)
{
if(active_button_array[elem] != (-1))
{
if(elem == (size_aba -1))
{
flag = 1;
break;
}
else
{
continue;
}
}
else
{
break;
}
}
if(flag == 1)
{
break;
}
}while ((level_time() == 0));
}
int checking_sequence(void) // Comparing the sequences. Comparing the CORRECT
// sequence with the sequence INPUTTED by the user;
{
int size_aba = size_sa;
int total_size = (size_aba + size_sa)/2;
button_act();
for(int elem_1 = 0; elem_1 <= (total_size -1); elem_1++)
{
if(active_button_array[elem_1] == sequence_array[elem_1] )
{
if(elem_1 == (total_size -1))
{
return 1;
}
else
{
continue;
}
}
else
{
return 0;
}
}
}
void display_current_sequence(void) // Displaying the random sequence;
{
for(int seq = 0; seq < (size_sa); seq++)
{
int current_array = sequence_array[seq];
led_act_desact(current_array, 1);
__delay_ms(500);
led_act_desact(current_array, 0);
__delay_ms(500);
}
}
void main(void)
{
ADCON1 = 0b00001111; // Configuring all ports as digital ones;
T0CON = 0b10111000; // Configuring Timer0:
// - Prescaler null;
// - External Clock (Counter Mode);
// - Transition High-low level;
// - Prescaler deactivated (Counter Mode);
// - Timer0 activated;
T1CON = 0b00110000; // Configuring Timer1:
// - Prescaler 1:8;
// - External Pulse;
// - Sync with external clock;
// - Internal Clock;
// - Timer1 deactivated;
TRISA = 0b00010000; // A Ports: All port as output, except RA4 because
// it is configured as the input for
// the external clock of Timer0;
TRISB = 0b10000000; // B Ports: All configured ad output, except RB7
// because it is configured as the
// input for the keypad;
TRISC = 0b10111000; // C Ports: Half are configured ad output for the
// LEDs and half are configured as
// input for the keypad;
TRISD = 0b11111111; // D Ports: All configured as input for the keypad;
TRISE = 0b00000111; // E Ports: RE0, RE1 and RE2 are configured ad input
// for the keypad;
TMR0 = 0; // Initializing Timer0 as 0;
flag_timer_0 = 0; // Setting Timer0's flag as 0;
int button;
__delay_ms(1000);
LCD_Initialize(); // Booting up LCD;
while(1) // Beginning of the infinity loop;
{
adding_to_array(random_sequence()); // Generating random numbers and
// adding them to the sequence
// array;
putsUSART("Level:"); // Sending information to the LCD through serial
// communication;
level_cd = itoa(level, size_sa, 10); // Converting the pseudo value
// of the array size into string;
putsUSART(level_cd); // Sending information to the LCD through serial
// communication;
LCD_Third_Line(); // Setting the cursor in the first column of the
// third row on the LCD;
putsUSART(" SEQUENCE..."); // Sending information to the LCD through
// serial communication;
__delay_ms(1000);
display_current_sequence(); // Displaying the random sequence by
// light up the LEDs;
LCD_Third_Line(); // Setting the cursor in the first column of the
// third row on the LCD;
putsUSART(" READY??? "); // Sending information to
// the LCD through
// serial communication;
for(button = 0; button <= 15; button++){led_act_desact(button, 1);}
// Lighting up LEDs;
__delay_ms(1000);
for(button = 0; button <= 15; button++){led_act_desact(button, 0);}
// Turning off LEDs;
LCD_Third_Line(); // Setting the cursor in the first column of the
// third row on the LCD;
putsUSART(" GO "); // Sending information to the LCD
// through serial communication;
if((size_sa % 2) == 0) // Checking if the current level is a even
// number;
{
time_sec = (size_sa / 2) * 5; // If the value is even, calculating
// the new remaining time for user's
// input;
}
else
{
time_sec = ((size_sa + 1) / 2) * 5; // If the value is odd,
// calculating a new
// remaining time for user's
// input;
}
if(checking_sequence() == 1) // Checking if the return value of the
// function is TRUE;
{ // If the value is TRUE, the user got the sequence RIGHT;
__delay_ms(500);
LCD_Fourth_Line(); // Setting the cursor in the first line on the
// fourth row of the LCD;
putsUSART(" "); // Sending information to the LCD
// through serial communication;
LCD_Third_Line(); // Setting the cursor in the first column of the
// third row on the LCD;
putsUSART(" PERFECT "); // Sending information to
// the LCD through
// serial communication;
for(button = 8; button <= 11; button++){led_act_desact(button, 1); __delay_ms(100);}
// Lighting up the row of green buttons, one by one;
__delay_ms(125);
for(button = 8; button <= 11; button++){led_act_desact(button, 0);}
// Turning off the row of green buttons;
__delay_ms(125);
for(button = 8; button <= 11; button++){led_act_desact(button, 1);}
// Lighting up the entire row of green buttons;
__delay_ms(250);
for(button = 11; button >= 8; button--){led_act_desact(button, 0); __delay_ms(100);}
// Turning off the row of green buttons, one by one;
LCD_Clear(); // Cleaning up the LCD;
size_sa++; // Adding one to the pseudo value of the array size;
}
else
{
// If it's FALSE, the user got the sequence WRONG, didn't push
// any buttons or ran out of time;
__delay_ms(500);
LCD_Fourth_Line(); // Setting the cursor in the first line on the
// fourth row of the LCD;
putsUSART(" "); // Sending information to the LCD
// through serial communication;
LCD_Third_Line(); // Setting the cursor in the first column of the
// third row on the LCD;
putsUSART(" INCORRECT "); // Sending information
// to the LCD
// through serial
// communication;
for(button = 0; button <= 3; button++){led_act_desact(button, 1); __delay_ms(100);}
// Lighting up the row of red buttons, one by one;
__delay_ms(125);
for(button = 0; button <= 3; button++){led_act_desact(button, 0);}
// Turning off the row of red buttons;
__delay_ms(125);
for(button = 0; button <= 3; button++){led_act_desact(button, 1);}
// Lighting up the row of red buttons.
__delay_ms(250);
for(button = 3; button >= 0; button--){led_act_desact(button, 0); __delay_ms(100);}
// Turning off the row of red buttons, one by one;
LCD_Clear(); // Cleaning up the LCD;
time_sec = 5; // Resetting the remaining time as 5 seconds;
size_sa = 1; // Returning to Level 1;
}
if(flag_timer_0 == 1) // Checking if Timer0's flag was activated;
{
// Case TRUE:
flag_timer_0 = 0; // Reset Timer0's flag;
}
else
{
// Case FALSE:
TMR0++; // Adding one to TIMER0 which is running in Counter Mode;
}
}
}