Code for C32 Microprocessor
Pseudocode
SmokeNMirrors_Main Module
Main
Call Function InitializePortM
Call Function InitializePortAD
Call Function InitializePWM
Call Function InitializePortT
Call Function InitializeTimer
Forever
Begin
Set eventcode to value returned by CheckGameEvents
Call Function HandleGameEvents and pass eventcode as parameter
(Both CheckGameEvents and HandleGameEvents are part of GameControl Module)
End
InitializePortM
Set Bit M0 to input from Coin Sensor
Set Bit M1 to input from Laser Break circuit
Set Bit M2 to input from Target
Set Bit M3 to input from Difficulty switch
Set Bit M4 to output to the Laser Fire
Set Bit M4 to low to begin with
Set Bit M5 to output to the Vibrating Motor
Set Bit M5 to low to begin with
InitializePortAD
Initialize the A/D converter and port AD with bits 3 to 7 as outputs and bits 0-2 as analog inputs
If the initialization is unsuccessful, print an error message
InitializePWM
Initialize the PWM subsystem using PWM12_Init
Set Channels 0,1 and 2 to output a duty cycle with a predefined period
InitializePortT
Set Bit T3 to output to Solenoid 1
Set Bit T4 to output to Solenoid 2
Set Bit T5 to output to Clock Countdown
Set Bit T6 to output to Clock Reset
Set Bit T7 to output
Set Bits T3 to T7 as low to begin with
InitializeTimer
Initialize the S12Timer module with a 1ms rate
GameControl Module
CheckGameEvents
Determines which event has taken place during game play. Takes no parameters
and returns a character value to indicate the corresponding event.
Set Event is NO_EVENT
If Solenoid 1 timer has expired
Event is SOLENOID_1_CLOCK_EXPIRE_EVENT
If Solenoid 2 timer has expired
Event is SOLENOID_2_CLOCK_EXPIRE_EVENT
If the timer in between the two solenoids has expired
Event is BETWEEN_SOLENOIDS_CLOCK_EXPIRE_EVENT
If the clock pulse timer has expired
Event is COUNTDOWN_CLOCK_PULSE_EXPIRE_EVENT
If the reset pulse timer has expired
Event is COUNTDOWN_RESET_PULSE_EXPIRE_EVENT
If the GameState is GAME_OVER
If coin is inserted
Event is COIN_INSERT_EVENT
If Servo Ground timer has expired
Event is SERVO_GND_TIMER_EXPIRE_EVENT
Endif
If the GameState is GAME_ON
If the Laser Fire timer has expired
Event is LASER_CLOCK_EXPIRE_EVENT
If the Clock countdown value is greater than zero and the countdown clock number has expired
Event is COUNTDOWN_CLOCK_EXPIRE_EVENT
If the difficulty level has been changed
Event is DIFFICULTY_CHANGE_EVENT
If the target has been hit
Event is TARGET_HIT_EVENT
If the Laser (non-contact sensor) has been broken
Event is LASER_BREAK_EVENT
If the game clock expires
Event is CLOCK_VALUE_TO_ZERO_EVENT
If the potentiometer has been changed
Event is POT_CHANGE_EVENT
Endif
Return Event
HandleGameEvents
Handles the services required for every event parameter returned by the CheckGameEvents function.
If Event is LASER_BREAK_EVENT
If Laser is not firing
FireLaser
Endif
Endif
If Event is DIFFICULTY_CHANGE_EVENT
SwitchDifficulty
Endif
If Event is TARGET_HIT_EVENT
Stop the Laser time and Clock Countdown timer
Call SetSolenoid1 to release lower Solenoid to dispense prize
Call StartSolenoidTimer to start timer and hold solenoid 1 open
Set Bit M5 to high to hit victory buzzer
Set GameState to GAME_OVER
Call StartServoTimer to start timer and ground the servos
Endif
If Event is LASER_CLOCK_EXPIRE_EVENT
Clear the expired timer
StopFiringLaser
Endif
If Event is POT_CHANGE_EVENT
UpdateServos
Endif
If Event is SERVO_GND_TIMER_EXPIRE_EVENT
Clear the expired timer
Ground the servos
Endif
If Event is COIN_INSERT_EVENT
StartGame
Endif
If Event is SOLENOID_1_CLOCK_EXPIRE_EVENT
Clear expired timer
Hold Solenoid 1
Start timer between the two solenoids
Endif
If Event is BETWEEN_SOLENOIDS_CLOCK_EXPIRE_EVENT
Clear expired timer
Release Solenoid 2
Start timer to keep Solenoid 2 in released state
Endif
If Event is SOLENOID_2_CLOCK_EXPIRE_EVENT
Clear expired timer
Hold Solenoid 2
StopFiringLaser
Endif
If Event is COUNTDOWN_RESET_PULSE_EXPIRE_EVENT
Clear expired timer
Set the Clock Reset line to low
Endif
If Event is COUNTDOWN_CLOCK_PULSE_EXPIRE_EVENT
Clear expired line
Set the Clock Countdown line to low
Endif
If Event is COUNTDOWN_CLOCK_EXPIRE_EVENT
Clear expired timer
Decrement the display on the Clock by 5 secs
Endif
If Event is CLOCK_VALUE_TO_ZERO_EVENT
Stop Clock Countdown and Laser timers
StopFiringLaser
Start Servo Grounding timers
Set GameState = GAME_OVER
Endif
CheckPots
CheckPots check to see if any of the three potentiometers have changed beyond the pot change thresholds.
It takes no parameters and returns the state of the pots as POT_CHANGED or POT_UNCHANGED.
Set static variable lastPot1State = -1
Set static variable lastPot2State = -1
Set static variable lastPot3State = -1
Set static variable currentPot1State to read the analog value from Pin1 of Port AD
Set static variable currentPot2State to read the analog value from Pin2 of Port AD
Set static variable currentPot3State to read the analog value from Pin3 of Port AD
If ((currentPot1State - lastPot1State > POT_CHANGE_THRESHOLD)
or (lastPot1State - currentPot1State > POT_CHANGE_THRESHOLD))
lastPot1State = currentPot1State
return POT_CHANGED
Endif
If ((currentPot2State – lastPot2State > POT_CHANGE_THRESHOLD)
or (lastPot2State – currentPot2State > POT_CHANGE_THRESHOLD))
lastPot2State = currentPot2State
return POT_CHANGED
Endif
If ((currentPot3State – lastPot3State > POT_CHANGE_THRESHOLD)
or (lastPot3State – currentPot3State > POT_CHANGE_THRESHOLD))
lastPot3State = currentPot3State
return POT_CHANGED
Endif
Return POT_UNCHANGED
UpdateServos
UpdateServos sets the duty cycle for each servo to match the potentiometer value.
Set static variable currentPot1State to read the analog value from Pin1 of Port AD
Set static variable currentPot2State to read the analog value from Pin2 of Port AD
Set static variable currentPot3State to read the analog value from Pin3 of Port AD
If (currentPot1State >= 0)
Servo1DutyCycle = (unsigned char)((unsigned int) DUTY_CYCLE_RATIO * currentPot1State/512)
If (Servo1DutyCycle > MAX_DUTY_CYCLE)
Servo1DutyCycle = MAX_DUTY_CYCLE
Endif
If (Servo1DutyCycle < MIN_DUTY_CYCLE)
Servo1DutyCycle = MIN_DUTY_CYCLE
Endif
Set duty cycle on Channel 0 to Servo1DutyCycle
Endif
If (currentPot2State >= 0)
Servo2DutyCycle = (unsigned char)((unsigned int) DUTY_CYCLE_RATIO * currentPot2State/512)
If (Servo2DutyCycle > MAX_DUTY_CYCLE)
Servo2DutyCycle = MAX_DUTY_CYCLE
Endif
If (Servo2DutyCycle < MIN_DUTY_CYCLE)
Servo2DutyCycle = MIN_DUTY_CYCLE
Endif
Set duty cycle on Channel 1 to Servo2DutyCycle
Endif
If (currentPot3State >= 0)
Servo3DutyCycle = (unsigned char)((unsigned int) DUTY_CYCLE_RATIO * currentPot3State/512)
If (Servo3DutyCycle > MAX_DUTY_CYCLE)
Servo3DutyCycle = MAX_DUTY_CYCLE
Endif
If (Servo3DutyCycle < MIN_DUTY_CYCLE)
Servo3DutyCycle = MIN_DUTY_CYCLE
Endif
Set duty cycle on Channel 2 to Servo3DutyCycle
Endif
StartLaserTimer
Starts the timer for firing the Laser. Inputs no parameters and returns no value.
Initialize Laser timer for duration depending on current difficulty level
CheckCoin
Checks coin sensor. Inputs no parameters and returns COIN_INSERTED, COIN_NOT_INSERTED, or NO_EVENT
Set Coin_status = COIN_NOT_INSERTED
If (Coin_status = COIN_NOT_INSERTED) and (presence of coin is detected)
Coin_status = COIN_INSERTED
Return Coin_status
Elseif (Coin_status = COIN_INSERTED) and (absence of coin is detected)
Coin_status = COIN_NOT_INSERTED
Return Coin_status
Endif
Return NO_EVENT
CheckLaserBreak
Checks non-contact laser sensor. Inputs no parameters and returns LASER_BROKEN, LASER_NOT_BROKEN, or NO_EVENT
Set laserBreak = LASER_NOT_BROKEN
If (laserBreak = LASER_NOT_BROKEN) and (laser is broken)
laserBreak = LASER_BROKEN
Return laserBreak
Elseif (laserBreak = LASER_BROKEN) and (laser is not broken)
laserBreak = LAER_NOT_BROKEN
Return laserBreak
Endif
Return NO_EVENT
CheckDifficultySwitch
Checks if difficulty has been changed. Inputs no parameters and returns DIFFICULTY_CHANGE or DIFFICULTY_NO_CHANGE
If (DifficultyLevel = EASY) and (difficulty is changed)
Return DIFFICULTY_CHANGE
Elseif (DifficultyLevel = HARD) and (difficulty is changed)
Return DIFFICULTY_CHANGE
Endif
Return DIFFICULTY_NO_CHANGE
CheckTarget
Checks if the target has been hit. Inputs no parameters and returns TARGET_HIT, TARGET_NOT_HIT or NO_EVENT
Set lastTarget1 = TARGET_NOT_HIT;
If (lastTarget1 = TARGET_NOT_HIT) and (target is hit)
lastTarget1 = TARGET_HIT
return TARGET_HIT
Elseif (lastTarget1 = TARGET_HIT) and (target is not hit)
lastTarget1 = TARGET_NOT_HIT
return TARGET_NOT_HIT
Endif
Return NO_EVENT
StartGame
Sets up the game to start playing. Inputs no parameters and returns no value.
Set GameState = GAME_ON
Turn off vibrating motor by setting Bit M5 to low
Reset the game clock to 25 secs
Start the Countdown Reset timer
FireLaser
Fires the Laser and starts timers for Laser firing and Clock Countdown.
Fire Laser by setting Bit M4 to high
Set LaserState = LASER_FIRING
If (DifficultyLevel = HARD)
Fire Laser for shorter duration
Else
Fire Laser for 5 secs
Endif
Start the Laser timer
Start the Clock Countdown timer
StopFiringLaser
Stops firing the Laser. Inputs no parameters and returns no value.
Set Bit M4 to low
Set LaserState = LASER_NOT_FIRING
SetSolenoid
Sets the state of a solenoid to be either released or held. Inputs solenoid number and state. Returns no value.
If (solenoid_state = SOLENOID_RELEASE)
Release Solenoid
Endif
If (solenoid_state = SOLENOID_HOLD)
Hold Solenoid
Endif
StartSolenoidTimer
Starts timer for either lower, upper solenoid or the airlock. Inputs solenoid number and returns no value.
If (solenoid_number = SOLENOID_1)
Start timer for Solenoid 1 for 1 sec
Endif
If (solenoid_number = SOLENOID_2)
Start timer for Solenoid 2 for 1 sec
Endif
If (solenoid_number = BETWEEN_SOLENOIDS)
Start timer for airlock for 3 secs
Endif
SwitchDifficulty
Switch the game difficulty. Inputs no parameters and returns no value.
If (DifficultyLevel = EASY)
DifficultyLevel = HARD
Endif
If (DifficultyLevel = HARD)
DifficultyLevel = EASY
Endif
SetCountdownReset
Sets the value of the reset line to high or low. Inputs reset value and returns no value.
If (reset_value = RESET_LINE_HI)
Set Bit T6 to high
Endif
If (reset_value = RESET_LINE_LO)
Set Bit T6 to low
Endif
SetCountdownClock
Sets the clock value of the Countdown clock to high or low. Inputs the clock value and returns no value.
If (clock_value = CLOCK_LINE_HI)
Set Bit T5 to high
Endif
If (clock_value = CLOCK_LINE_LO)
Set Bit T5 to low
Endif
StartCountdownResetTimer
Starts the Countdown reset pulse timer. Inputs no parameters and returns no value.
Start the Countdown reset timer to count to 10ms
StartCountdownTimer
Starts the Countdown timer. Inputs no parameters and returns no value.
Start the Countdown clock timer to count to 1 sec or 500ms depending on difficulty being EASY or HARD respectively
StartCountdownClockPulseTimer
Starts the timer for the clock pulse. Inputs no parameters and returns no value.
Start the clock pulse timer to count to 100ms
GroundServos
Grounds the three servos by setting the PWM duty cycles to zero. Inputs no parameters and returns no value.
Set duty cycle of channels 0, 1 and 2 to zero.
StartServoTimer
Starts timer for grounding the servos. Inputs no parameters and returns no value.
Start the servo ground timer to count to 2 secs
StopTimers
Stop timers for laser fire and countdown clock to prevent these events from continuing after game is over.
Inputs no parameters and returns no value.
Stop timer for the laser
Stop timer for the Countdown clock