This module
provides functions to run the miniature basketball game.
Here are
codes for this module. Please click on the file names for detail.
- GAME.h
- GAME.c
void
InitializeGame(void);
/*
Prototype: void InitializeGame(void)
Parameters: None
Returns: None
Usage: InitializeGame();
Description
InitializeGame(void) initializes a game by
initializing LCD and score,
stopping all the motors, and turning on the
green LED.
*/
void
CheckPenny(void);
/*
Prototype: void CheckPenny(void)
Parameters: None
Returns: None
Usage: CheckPenny();
Description
CheckPenny(void) waits until Penny is
detected.
*/
void
StartMotors(void);
/*
Prototype: void StartMotors(void)
Parameters: None
Returns: None
Usage: StartMotors();
Description
StartMotors(void) starts all the necessary
motors at the beginning of each game.
*/
short
CheckEvent(void);
/*
Prototype: void CheckEvent(void)
Parameters: None
Returns: short
event, which is the detected event by the function
Usage: CheckEvent();
Description
CheckEvent(void) waits for the goal or
potentiometer change until time out.
*/
void
HandleEvent(short event);
/*
Prototype: void HandleEvent(short event)
Parameters: short
event, the input event to handle
Returns: None
Usage: HandleEvent(Goal);
Description
HandleEvent(short event) handles the event
according to the input.
If time is over, do nothing and go back to
the main function.
If there's a goal, increment the score and
display it on LCD, and turn on the buzzer.
If the potentiometer has been turned, change
the motor speed of the loop.
The function returns nothing after taking
care of the event.
*/
void
FinishGame(void);
/*
Prototype: void FinishGame(void)
Parameters: None
Returns: None
Usage: FinishGame();
Description
FinishGame(void) finishs up the game by
turning off the motors, turning red LED on,
and handling SWAG.
*/
#include
<stdio.h>
#include
<ME218_C32.h>
#include
<timers12.h>
#include
<PWMS12.h>
#include
<ADS12.h>
#include
"LCD.h"
#include
<string.h>
/*****
C32 bit define part *****/
#define
PWMS12_1333US 0x500A
#define
TimeOverLEDBit BIT0HI //Port AD
#define
GoalDetectBit BIT1HI //Port AD
#define
PennyInputCheckBit BIT3HI //Port AD
#define
PotentiometerBit BIT4HI //Port AD
#define
Potentiometer 4
#define
BuzzerBit BIT5HI //Port AD
#define
ShooterMotorBit BIT6HI //Port AD
#define
SWAGmotorBit BIT7HI //Port AD
#define
GoalMotorLBit BIT2HI //Port AD
#define
GoalMotorRBit BIT3HI //Port M
/*****
Module variables *****/
static
short MaxSpeed = 11;
static
short Goal = 1500;
static
short Timeover = 1501;
static
unsigned char TimeOK = 1;
static
unsigned char TimeOVER = 0;
static int
GameStartTime;
static long
GameTimeOut = 25000;
static int
goal_start_Time;
static
unsigned char score;
static
unsigned char WinningScore = 2;
/*****
function declaration *****/
void
InitializeGame(void);
void
CheckPenny(void);
static char
GetPennySignal(void);
void
StartMotors(void);
signed char
SetMotorSpeed(unsigned char MotorChan, short MotorSpeed);
short
CheckEvent(void);
void
HandleEvent(short event);
void
InitialLCDdisplay1(void);
void InitialLCDdisplay2(void);
void
TimeOverLCDdisplay(void);
void
IncrementScore(void);
unsigned
char CheckTimeOut(void);
void
HandleSWAG(void);
void
Buzzer(void);
void
FinishGame(void);
/***
main function ***/
void
main(void)
{
short checkEvent;
while(1)
{
/*
Initialization */
InitializeGame();
/* Wait for
penny insertion */
CheckPenny();
/* Game Starts!!!!! */
GameStartTime = TMRS12_GetTime();
StartMotors();
while(CheckTimeOut() == TimeOK)
{
// Wait for the event
checkEvent = CheckEvent();
// Handle the event
HandleEvent(checkEvent);
}
/* Game Over!!!!! */
FinishGame();
}
return;
}
/*****
function implementation *****/
void
InitializeGame(void)
{
if (ADS12_Init("OOOAIOIO") ==
ADS12_OK)
{
// Initialize
LCD
LCDInit();
InitialLCDdisplay1();
// Stop
motors
PTAD &= ~ShooterMotorBit;
PTAD &= ~SWAGmotorBit;
PTAD &= ~GoalMotorLBit;
DDRM |= GoalMotorRBit;
PTM &= ~GoalMotorRBit;
PWMS12_Init();
// Initialize score
score = 0;
// Turn on
the gree LED (indicating 'can play the game')
PTAD &= ~TimeOverLEDBit;
printf("Initialization
ends\n");
}
}
/**
Wait until Penny is detected **/
void
CheckPenny(void)
{
int signal = GetPennySignal();
int new_signal = GetPennySignal();
/* Wait for the input to be HI. If the
input is already HI, wait for it to drop and then to rise again */
if( signal == 1 )
{
for( ; signal == new_signal;new_signal = GetPennySignal());
for( signal = GetPennySignal(); signal ==
new_signal; new_signal = GetPennySignal());
}
else
{
for( ; signal == new_signal;new_signal =
GetPennySignal());
}
InitialLCDdisplay2();
return;
}
/**
Return 1 if the input bit is high, return 0 otherwise **/
static char
GetPennySignal(void)
{
if( PTIAD & PennyInputCheckBit )
return 1;
else
return 0;
}
/**
Start all the necessary motors at the beginning of each game **/
void StartMotors(void)
{
if (SetMotorSpeed(PWMS12_CHAN0,
ADS12_ReadADPin(Potentiometer))== PWMS12_OK)
{
PTAD |= GoalMotorLBit;
}
if (PWMS12_SetDuty(14*MaxSpeed,
PWMS12_CHAN1) == PWMS12_OK)
{
PTAD |= ShooterMotorBit;
}
goal_start_Time = TMRS12_GetTime();
}
/**
Set the motor speed by changing the duty cycle **/
signed char
SetMotorSpeed(unsigned char MotorChan, short MotorSpeed)
{
unsigned char speed;
speed = MotorSpeed / MaxSpeed;
return PWMS12_SetDuty(speed, MotorChan);
}
/**
Wait for the goal or potentiometer change until time out **/
short
CheckEvent(void)
{
static char flag = 0;
int curTime;
int delTime;
unsigned char goal_signal = PTIAD &
GoalDetectBit;
unsigned char goal_newSignal;
short pot_signal =
ADS12_ReadADPin(Potentiometer);
short pot_newSignal;
/* Check for time
over, goal, and potentiometer change */
do
{
goal_newSignal = PTIAD &
GoalDetectBit;
pot_newSignal = ADS12_ReadADPin(Potentiometer);
// Change the
direction of the Goal Motor every 10 sec
curTime = TMRS12_GetTime();
delTime = curTime - goal_start_Time;
if (delTime >= 5000)
{
if (flag == 0)
{
PTAD &= ~GoalMotorLBit;
PTM |= GoalMotorRBit;
goal_start_Time =
TMRS12_GetTime();
flag = 1;
} else if (flag == 1)
{
PTAD |= GoalMotorLBit;
PTM &= ~GoalMotorRBit;
goal_start_Time =
TMRS12_GetTime();
flag = 0;
}
}
} while((CheckTimeOut() == TimeOK)
&& (goal_signal == goal_newSignal) &&
((pot_signal-pot_newSignal)<=50) &&
((pot_signal-pot_newSignal)>=-50));
/* return the result */
if (goal_signal != goal_newSignal)
{
return Goal;
} else if (CheckTimeOut() == TimeOVER)
{
return Timeover;
} else if (pot_signal != pot_newSignal)
{
return pot_newSignal;
}
}
/**
Handle the Event according to the input **/
void
HandleEvent(short event)
{
if (event == Timeover)
{
/* If time is
over, do nothing and go back to the main function. */
} else if (event == Goal)
{
/* if there's a goal, increment the
score and display it on LCD, and turn on the buzzer */
IncrementScore();
Buzzer();
} else
{
/* If the potentiometer has been turned, change the motor
speed of the loop */
SetMotorSpeed(PWMS12_CHAN0, event);
}
}
/**
Initial LCD display when the game is turned on **/
void
InitialLCDdisplay1(void)
{
char i;
char display[40] = "PLEASE INSERT
PENNY";
for (i = 0; i< strlen(display); i++)
{
LCDputchar(display[i]);
}
}
/**
Initial LCD display when the game is started **/
void
InitialLCDdisplay2(void)
{
char i;
char display[40] = " SCORE : 0";
for (i = 0; i< strlen(display); i++)
{
LCDputchar(display[i]);
}
}
/**
Initial LCD display when the time is over **/
void
TimeOverLCDdisplay(void)
{
char i;
char display[40] = "TIME OVER!! WAIT..";
for (i = 0; i< strlen(display); i++)
{
LCDputchar(display[i]);
}
}
/**
Increment the score and display the new score on the LCD **/
void
IncrementScore(void)
{
score ++;
printf("SCORE is %d", score);
Control_RS('c');
DDRT |= 0xF8;
DDRM |= 0x07;
ArrangeLCDbits(0x10);
Pulse_EN();
ArrangeLCDbits(0x1F);
Pulse_EN();
LCDputchar((unsigned char)(0x30+score));
}
/**
Check for Time over **/
unsigned
char CheckTimeOut(void)
{
int delT;
int
GameCurrTime = TMRS12_GetTime();
delT = GameCurrTime - GameStartTime;
if (delT >= GameTimeOut)
{
return TimeOVER;
} else
{
return TimeOK;
}
}
/**
Give out SWAG according to the user's performance **/
void HandleSWAG(void)
{
char i;
char displayWin[40] = "CONGRATULATIONS!!!!";
char displayLose[40] = "SORRY! TRY
AGAIN!!!";
if (score >= WinningScore)
{
/* If score
is high, display win message and give out SWAG */
for (i = 0; i< strlen(displayWin);
i++)
{
LCDputchar(displayWin[i]);
}
// run the
motor for SWAG
PTAD |= SWAGmotorBit;
Wait(1500);
PTAD &= ~SWAGmotorBit;
} else
{
/* If score
is low, display lose message */
for (i = 0; i< strlen(displayLose);
i++)
{
LCDputchar(displayLose[i]);
}
}
}
/**
Turn on the Buzzer for 0.4 sec **/
void
Buzzer(void)
{
PTAD |= BuzzerBit;
Wait(200);
PTAD
&= ~BuzzerBit;
}
/**
Finish up the game by turning off the motors, turning red LED on, and handling
SWAG **/
void
FinishGame(void)
{
TimeOverLCDdisplay();
Wait(500);
// Turn on the
red LED indicating TimeOver
PTAD |= TimeOverLEDBit;
// Stop motors
PTAD &= ~ShooterMotorBit;
PTAD &= ~GoalMotorLBit;
PTM &= ~GoalMotorRBit;
// Handle SWAG
HandleSWAG();
Wait(3000);
}