/* FINAL PROJECT this program will implement the "follow me" 1 player game it will be used to drive the 8052 chip in order to achieve correct functionality of the game Amin Aun xxx-xx-1222 cs122aao@cs.ucr.edu */ #pragma SMALL DB OE #include #include "io.h" #include sbit latch0 = P3^2; sbit latch1 = P3^3; sbit light0 = P3^7; sbit light1 = P3^6; sbit light2 = P3^5; sbit light3 = P3^4; sbit decoder_enable = P0^0; //Delay function void delay() { int i, j; for(i=0; i<1000; i++) { for(j=0; j<100; j++) { i = i + 0; } } } void initialize_latches ( ) { decoder_enable = 1; light0 = 0; light1 = 0; light2 = 0; light3 = 0; latch0 = 0; latch1 = 0; latch0 = 0; latch1 = 1; latch0 = 1; latch1 = 0; latch0 = 1; latch1 = 1; decoder_enable = 0; } //this global variable will be used as the seed for the srand function, //in order to choose a random seed table every time for every execution of the //follow me game int seed; /* The functions to initialize and control the LCD are assumed to be in the file io.c */ /* Function to output the decimal value of the result on the LCD */ void PrintInt(int i) { char negative = '0'; char ch[4]; //If the answer is a negative number, print out the absolute //value and the "-" character if ( i < 0 ) { i = i * -1; negative = '1'; } //Write code to convert the count to a string value and use the //PrintString function provided in io.c //devide i by ten to attain the first character in the string ch[0] = '0' + (i / 10); //mod i by 10 to get the second character in the string ch[1] = '0' + (i % 10); ch[2] = '\0'; //ClearScreen ( ); if ( negative == '1' ) { PrintString ( "-" ); } PrintString (ch); delay(); } /* Routine to scan the key pressed */ unsigned char key_scan(int flag) { unsigned char i, j, temp1, temp2; unsigned char position; //set the global variable to 0 for every call of this function so that //the timer achieves a different value seed = 0; while( 1 ) /* keep waiting for a key to be pressed */ { for(i=0; i<4; i++) { /* Set each row to 0 */ P1 = 0xff & ~(1< 10000 && flag == 0 ) { return position; } //store the value at port 1 into an unsigned register temp1 = P1; //do an bitwise AND operation on the complemented value //from port 1 using //a 1 bit shifted j times, if the value at that bit position //was set to zero by the pressing of a key, then this XOR //operation will produce a value greater than zero if ( (~temp1 & (1< 0 ) { temp1 = i * 4; temp2 = j - 4; position = temp1 + temp2; return position; } } } } } void main() { //this number will be used to lite up one of the led lights unsigned char random_light; //this will be used to lite up one of the rows unsigned char row; //this will be used to lite up one of the coloums unsigned char coloum; //this number will correspond to the button pressed by the user int button_input; int button_input2; //this will be used to activate the led lights and differnt intervals int random_interval; //this variable will represent the number of points that the user has //scored in playing the game int score = 0; //this variable will represent a watchdog timer in that it will count //down a predetermined amount of time until the user has pressed a //button, if the user does not press a button /* You can have a conversion table to convert the key position into a valid number which indicates the position of a button */ int conv_table[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; InitIO(); while (1) { if ( score < -99 ) { ClearScreen ( ); button_input2 = conv_table[key_scan(0)]; button_input = button_input2; do { GotoXY ( 0, 0 ); PrintString ("GAME OVE"); GotoXY (1, 0 ); PrintString ( "R :-(" ); delay ( ); ClearScreen ( ); PrintString("Press to"); GotoXY ( 1, 0 ); PrintString(" play "); button_input = conv_table[key_scan(0)]; } while ( button_input == button_input2 ); } if ( score > 99 ) { ClearScreen ( ); button_input2 = conv_table[key_scan(0)]; button_input = button_input2; do { GotoXY ( 0, 0 ); PrintString ("YOU WIN!!!"); GotoXY (1, 0 ); PrintString ( "R :-)" ); delay ( ); ClearScreen ( ); PrintString("Press to"); GotoXY ( 1, 0 ); PrintString(" play "); button_input = conv_table[key_scan(0)]; } while ( button_input == button_input2 ); } score = 0; //set all lights low initialize_latches ( ); ClearScreen ( ); delay ( ); PrintString("Press to"); GotoXY ( 1, 0 ); PrintString(" start"); //wait for user to press a button button_input = conv_table[key_scan(1)]; ClearScreen ( ); //now take the variable counter which was set inside key_scan and //use it as a seed value to select a random seed table srand( seed ); while( score >= -99 && score <= 99 ) { ClearScreen ( ); PrintString ("SCORE: " ); GotoXY ( 1, 0 ); PrintInt ( score ); delay ( ); //make a timer that waits a random amount of time in order to //light up the an led at different intervals random_interval = rand ( ) % 1000; for ( ; random_interval != 0; random_interval-- ); //set the led random_light = rand() % 16; //ClearScreen ( ); //PrintString ("position" ); //GotoXY ( 1, 0 ); //PrintInt ( random_light ); row = random_light/4; coloum = random_light%4; switch (coloum) { case 0: light0 = 1; light1 = 0; light2 = 0; light3 = 0; break; case 1: light0 = 0; light1 = 1; light2 = 0; light3 = 0; break; case 2: light0 = 0; light1 = 0; light2 = 1; light3 = 0; break; case 3: light0 = 0; light1 = 0; light2 = 0; light3 = 1; break; default: light0 = 0; light1 = 0; light2 = 0; light3 = 0; break; } switch ( row ) { case 0: latch0 = 0; latch1 = 0; decoder_enable = 1; delay ( ); decoder_enable = 0; break; case 1: latch0 = 1; latch1 = 0; decoder_enable = 1; delay ( ); decoder_enable = 0; break; case 2: latch0 = 0; latch1 = 1; decoder_enable = 1; delay ( ); decoder_enable = 0; break; case 3: latch0 = 1; latch1 = 1; decoder_enable = 1; delay ( ); decoder_enable = 0; break; default: latch0 = 0; latch1 = 0; decoder_enable = 1; delay ( ); decoder_enable = 0; break; } //wait for user to press a button button_input = conv_table[key_scan(0)]; //set lights low light0 = 0; light1 = 0; light2 = 0; light3 = 0; decoder_enable = 1; delay ( ); decoder_enable = 0; //if the user pressed the wrong button then notify the user //that they have //lost one point if ( random_light != button_input && seed < 1000 ) { ClearScreen ( ); PrintString ( "WRONG BU" ); GotoXY (1, 0 ); PrintString ( "TON: -1" ); score--; } //if the user has not pressed the button in the specified //amount of time //notify the user that they have lost five points else if ( seed >= 1000 ) { ClearScreen ( ); PrintString ( "TOO SLOW" ); GotoXY (1, 0 ); PrintString ( " MINUS 5" ); score = score - 5; } //if the button corresponds to the correct light pressed, then //notify the user that they have earned a point else if ( random_light == button_input ) { ClearScreen ( ); PrintString ("HIT!!! P" ); GotoXY (1, 0 ); PrintString ( "LUS 1" ); score++; } delay ( ); } } }