-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
316 lines (239 loc) · 6.4 KB
/
main.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
#include <p18F87J11.h>
#include <delays.h>
#include "LCD.h"
/*configuration bits*/
#pragma config DEBUG = ON, XINST = OFF, STVREN = ON, WDTEN = OFF
#pragma config CP0 = ON
#pragma config IESO = ON, FCMEN = ON, FOSC = ECPLL
#pragma config WDTPS = 1
#pragma config WAIT = OFF, BW = 16, MODE = MM, EASHFT = ON
#pragma config MSSPMSK = MSK7, PMPMX = DEFAULT, ECCPMX = DEFAULT,CCP2MX = DEFAULT
#define SWITCH_0 PORTBbits.RB0
#define SWITCH_1 PORTAbits.RA5
#pragma idata
unsigned char string[176]={" " //num play 0-15
" " //num play 16-29
" Start Game? " //init msg
" PRESS ANYKEY " //init msg
"WRONG,try again?" //wrong msg
"RB0->y || RA5->N" //wrong msg
"Congratulations!" //win msg
" YOU WIN!! " //win msg
"*****CORRECT****" //exit info
"*****SEQUENCE***" //exit info
"FINAL LENGTH "}; //final length
unsigned char var[30] = {1};
#pragma code
/*prototypes*/
void string_write(unsigned char);
void release(void);
void lcd_write(char , char);
void main(void)
{
// void* foo = 0, bar = 0, echo = 0, num = 0;
unsigned char foo = 0, bar = 0, echo = 0, num = 0, error = 0; //counter
/*reset contents of string*/
foo = 0;
while(foo<30)
{
string[foo] = ' ';
++foo;
}//end while
//LCD init
LCDInit();
/*switch init*/
/*RB0*/
INTCON2bits.RBPU = 0; // enable PORTB internal pullups
TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1)
/*RA5*/
WDTCONbits.ADSHR = 1;
ANCON0 = 0x1f;
TRISAbits.TRISA5 = 1;
/*prompt for start*/
lcd_write(2,3);
//hold the start info until the SWITCH being pressed
while( SWITCH_0 == 1 && SWITCH_1 == 1)
;
release();
//gen 30 numbers of 1 or 0 then store into var[]
foo = 0;
while(foo < 30)
{
num = rand2()%2;
if(num==1)
var[foo] = '1';
else
var[foo] = '0';
++foo;
}//end while
//loop game 3 to 30
bar = 3;
/*-----------------------------------------------------------------------------------------------------------------------
*while loop switch check, for the current bar*/
while( bar <= 3) //total game process
{
start:
/*display current string interval*/
foo = 0;
while(foo<bar)
{
string[0] = var[foo];
/*display num for 0.8s*/
lcd_write(0,1);
Delay10KTCYx(80);
/*display interval for 0.2s*/
lcd_write(1,1);
Delay10KTCYx(20);
++foo;
}//end while
/*-----------------------------------------------------------------------------------------------------------------------
*switch check, for the current char(echo)*/
echo = 0;
while(echo < bar)
{
/*switch input*/
while(1)
{
/*switch input CORRECT---------------------------------------------------------*/
if((SWITCH_0 == 0&& var[echo]=='0')||(SWITCH_1 == 0&&var[echo]=='1'))
{
release();
break;
}//end if
/*switch input WRONG-----------------------------------------------------------*/
else if((SWITCH_0 == 0&& var[echo]=='1')||(SWITCH_1 == 0&&var[echo]=='0')||(SWITCH_0 == 0&&SWITCH_1 == 0))
{
release();
/*increment error count*/
error++;
/*display wrong msg*/
lcd_write(4,5);
/*end the game when second error occur*/
if(error >= 2)
goto error;
/*pending switch input------------------------------------------------*/
Delay10KTCYx(50);
while(1)
{
/*play again with same bar length---------------------*/
if(SWITCH_0 == 0) //play again with same length
{
release();
goto start;
}//end if
/*end of the game, starting new round-----------------*/
else if(SWITCH_1 == 0) //end of game, new round
{
release();
error:
/*length-------------------------------------*/
/*determine length & renew string*/
if(bar/10==0)
{
string[175] = bar%10+'0';
}//end if
else
{
string[174] = bar/10+'0';
string[175] = bar%10+'0';
}//end else
/*display & delay*/
lcd_write(10,10);
Delay10KTCYx(200);
/*display exit answer info------------------*/
lcd_write(8,9);
Delay10KTCYx(200);
/*renew correct sequence*/
num = 0;
while(num<30)
{
string[num] = var[num];
++num;
}//end while
/*display correct sequence--------------------*/
lcd_write(0,1);
Delay10KTCYx(200);
goto end;
}//end else if
}//end while
break;
}//end else if
else if(SWITCH_0 == 1 && SWITCH_1 == 1) //unpressed
continue;
}//end while
/*check right -> continue move on to the next char*/
++echo;
}//end while
/*increment the length of bar and continue-----------------------------------------------*/
++bar;
/*reset error count*/
error = 0;
}//end while
//end display
lcd_write(6,7);
Delay10KTCYx(100);
/*renew correct sequence*/
num = 0;
while(num<30)
{
string[num] = var[num];
++num;
}//end while
/*pending for switch input and flicking LED, display correct sequence*/
TRISD = 0;
while(SWITCH_0 == 1 && SWITCH_1 == 1)
{
lcd_write(0,1);
LATD = 0xff;
Delay10KTCYx(3);
LATD = 0;
Delay10KTCYx(3);
}//end while
release();
LATD = 0;
end:
main();
}//end main
void lcd_write(char foo, char bar)
{
LCDLine_1();
string_write(foo);
LCDLine_2();
string_write(bar);
}//end lcd_write
/*call function release*/
void release(void)
{
/* this function halts execution
*until switchs are released
*/
unsigned char count = 0;
while(1)
{
Delay100TCYx(100);
if(SWITCH_0==1&&SWITCH_1==1)
count++;
else
count = 0;
if(count >= 5)
break;
}//end while
}//end release
//******************************************************************
// Write a string on LCD
//******************************************************************
void string_write(unsigned char idx)
{
unsigned int i=0;
unsigned int z=0;
char b;
while (i<16)
{
z = idx*16;
z = z + i;
b = string[z];
data_write(b);
Delay10TCYx(50);
i++;
}//end while
}//end string_write