C Code

 

Return to main page.

 

C32 Pin Connections

 

E0: Coin Sensor

E1: Button (User Input)

M0: Vertically-aligned outer ring tape sensor

M1: Offset outer ring tape sensor

M2: Yellow LEDs

M3: Red LEDs

M4: Green LEDs

M5: Inner ring tape sensor

T0: Wheel motor

T1: Swag dispenser motor

T2: Nothing

T3: LCD (DB0)

T4: LCD (DB1)

T5: LCD (DB2)

T6: LCD (DB3)

T7: LCD (DB4)

AD0: Dial (User Input)

AD1: Nothing

AD2: Buzzer

AD3: LCD Enable

AD4: LCD RS (Data/Command Mode)

AD5: LCD (DB7)

AD6: LCD (DB6)

AD7: LCD (DB5)

 

Header Files

 

LCD.c

Purpose: Initialize LCD with a particular sequence of bit-writes to set the proper text movement


CheckEvents.c

Purpose: Check all possible game events during both the Price Guessing and Wheel Spinning portions of the MPAG

 

Wheel.c

Purpose: Evaluate the game-play events based on the user inputs (button, dial, wheel)

 

PWMS12.c

Purpose: Apply the appropriate software to operate the Pulse Width Modulation (PWM) features of the relevant C32 pins

 

ADS12.c

Purpose: Apply the appropriate software to drive the analog-digital pins of the C32

 

CODE FILES

 

LCD.c

LCD.h

CheckEvents.c

CheckEvents.h

Wheel.c

Wheel.h

PWMS12.c

PWMS12.h

ADS12.c

ADS12.h

 

 

PSUEDOCODE

 

MODULE: CheckEvents.c

 

MAIN FUNCTION

Input: None

Returns: None

 

Begin

If there is a current event that is an actual event

                Handle the event

Forever

 

INITIALIZATION FUNCTION

Input: None

Returns: None

 

Set M0, M1, M5 as input pins

Set M2, M3, M4 as output pins

Set T3-T7 as LCD output pins

Initialize PWM module

Initialize LCD

Turn off wheel and SWAG dispenser motors (0% duty cycle)

Turn off the buzzer

Turn on the pairs of yellow, red, and green LEDs

 

CHECK EVENTS FUNCTION

Input: None

Returns: Current event

 

Poll Port M to get its current status

Poll Port E to get its current status

If Port M’s status has not changed

                If Port E’s status has not changed

                                If the timer status has changed and it has expired

                                                Set the current event to the timer being expired

                                End-If

                                Else

                                                Set the current event to No Event

                                End-Else

                End-If

                Else

                                Consider the states of E0 (Coin Sensor) and E1 (Button Input):

                                                E0:

                                                                If E0 is not LO

                                                                                Set the current event to E0 is HI

                                                                End-If

                                                                Else

                                                                                Set the current event to E0 is LO

                                                                End-Else

                                                                Stop considering the states of E0-E1

                                                E1:

                                                                If E1 is not LO

                                                                                Set the current event to E1 is HI

                                                                End-If

                                                                Else

                                                                                Set the current event to E1 is LO

End-Else

Stop considering the states of E0-E1

                                End of considering the states of E0 and E1

                End-Else

If Port M’s status has changed

                Consider the states of M0-M5:

                                M0:

                                                If M0 is not LO

                                                                Set the current event to M0 is HI

                                                End-If

                                                Else

                                                                Set the current event to M0 is LO

                                                End-Else

Stop considering the states of M0-M5

                                M1:

                                                If M1 is not LO

                                                                Set the current event to M1 is HI

                                                End-If

                                                Else

                                                                Set the current event to M1 is LO

                                                End-Else

                                                Stop considering the states of M0-M5

M2:

                                                If M2 is not LO

                                                                Set the current event to M2 is HI

                                                End-If

                                                Else

                                                                Set the current event to M2 is LO

                                                End-Else

                                                Stop considering the states of M0-M5

M3:

                                                If M3 is not LO

                                                                Set the current event to M3 is HI

                                                End-If

                                                Else

                                                                Set the current event to M3 is LO

                                                End-Else

                                                Stop considering the states of M0-M5

M4:

                                                If M4 is not LO

                                                                Set the current event to M4 is HI

                                                End-If

                                                Else

                                                                Set the current event to M4 is LO

                                                End-Else

                                                Stop considering the states of M0-M5

M5:

                                                If M5 is not LO

                                                                Set the current event to M5 is HI

                                                End-If

                                                Else

                                                                Set the current event to M5 is LO

                                                End-Else

                                                Stop considering the states of M0-M5

                End of considering the states of M0-M5

End-If

Set the last status of M to the current status of M

Set the last status of E to the current status of E

Set the last timer status to the current timer status

Return the current event

 

EVENT HANDLER FUNCTION

Input: Current event

Returns: None

 

Consider the new possible events:

                New event being No Event:

                                Do nothing

                                Stop considering new possible events

                New event being M0 is HI

Find the wheel’s current position since the outer ring’s vertically-aligned tape sensor (connected to M0) is HI

                                Handle the game event of the wheel moving

                                Stop considering new possible events

                New event being M0 is LO

Find the wheel’s current position since the outer ring’s vertically-aligned tape sensor (connected to M0) is LO

                                Handle the game event of the wheel moving

                                Stop considering new possible events

                New event being M1 is HI

Find the wheel’s current position since the outer ring’s offset tape sensor (connected to M1) is HI

                                Stop considering new possible events

                New event being M1 is LO

Find the wheel’s current position since the outer ring’s offset tape sensor (connected to M1) is LO

                                Stop considering new possible events

                New event being M2 is HI

Do nothing because the event of the pair of the yellow LEDs turning off is not an event to be handled in this function

Stop considering new possible events

New event being M2 is LO

Do nothing because the event of the pair of the yellow LEDs turning on is not an event to be handled in this function

Stop considering new possible events

New event being M3 is HI

Do nothing because the event of the pair of the red LEDs turning off is not an event to be handled in this function

Stop considering new possible events

New event being M3 is LO

Do nothing because the event of the pair of the red LEDs turning on is not an event to be handled in this function

Stop considering new possible events

New event being M4 is HI

Do nothing because the event of the pair of the green LEDs turning off is not an event to be handled in this function

Stop considering new possible events

New event being M4 is LO

Do nothing because the event of the pair of the green LEDs turning on is not an event to be handled in this function

Stop considering new possible events

New event being M5 is HI

Find the wheel’s current position since the inner ring’s tape sensor (connected to M5) is HI

Stop considering new possible events

New event being M5 is LO

Find the wheel’s current position since the inner ring’s tape sensor (connected to M5) is LO

Stop considering new possible events

New event being E0 is HI

                Handle the event of a penny being inserted

Stop considering new possible events

New event being E0 is LO

                No penny was inserted

Stop considering new possible events

New event being E1 is HI

                Handle the event of the button being pressed (E1 could be HI or LO)

Stop considering new possible events

New event being E1 is LO

                Handle the event of the button being pressed (E1 could be HI or LO)

Stop considering new possible events

                New event being the timer has expired

                                Turn off the wheel’s motor (duty cycle is 0%)

Stop considering new possible events

                New event being an error

Stop considering new possible events

End of considering new events

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MODULE: Wheel.c

 

Define an array of the correct prices of all the items up for bid

Define an array of the prices’ tolerances

Define an array of the computer’s untoleranced price guesses

 

GET ITEM NAME FUNCTION

Input: Item to bid on (a number between zero and the number of items minus one)

Returns: The item’s full name

 

Define a variable with the item’s full name

Consider all of the items to bid on:

For each item, copy the item’s full name (within double quotation marks) to the item full name variable

Return the item’s full name

 

 

GET ITEM’S FULL CORRECT PRICE FUNCTION

Input: Item to bid on (a number between zero and the number of items minus one)

Returns: Correct price of the item

 

For the proper index of the item, return the array value of the item’s correct price

 

GET COMPUTER’S UNTOLERANCED PRICE GUESS FUNCTION

Input: Item to bid on (a number between zero and the number of items minus one)

Returns: Computer’s untoleranced price guess

 

For the proper index of the item, return the array value of the computer’s untoleranced price guess

 

GET ITEM’S TOLERANCE PRICE BAND FUNCTION

Input: Item to bid on (a number between zero and the number of items minus one)

Returns: Item’s tolerance price band

 

Set the tolerance price band equal to the array value of the item’s tolerance price band plus one

Return a random number between zero and the tolerance price band

 

SWAG DISPENSING FUNCTION

Input: None

Returns: None

 

Turn on the SWAG motor (PWM Channel 1) at 100% duty cycle

Wait one second

Turn off the SWAG motor (0% duty cycle)

 

GAME EVENT HANDLING FUNCTION

Input: Current event

Returns: None

 

If the current event is the penny being inserted

                Turn the wheel off (PWM Channel 0 at 0% duty cycle)

                The game has started and is Price Guessing mode

Determine a random item to bid on within the array of items by calling the Get Item Name Function

Acknowledge penny insertion by turning on the buzzer and turning off all three pairs of LEDs

Wait half of a second

Turn off the buzzer and turn on all three pairs of LEDs

Call the LCD string display function to write a few messages to the LCD informing the user of the game’s instructions and rules

Wait one second until the user can begin to update his/her guess of the item’s price with the dial

Begin

Update the user’s guess of the item’s price every 200 milliseconds and display the guess on the LCD

Until the user presses the button

End-If

If the game has started (penny has been inserted)

                If the game-play mode is Price Guessing

                                Read the user’s final guess of the item’s price

Display on the LCD that the user has submitted his/her guess and display that actual guess

If the user’s guess is between the computer’s lower and upper bound guesses

Notify the user of success by turning on the buzzer and turning off all three pairs of LEDs for half a second, then turn off the buzzer and turn on all three pairs of LEDs

Display a congratulatory message on the LCD along with his/her guess, the correct price, and the computer’s lower bound guess (the LCD cannot display all of the upper bound guesses since some of them cannot be entered by the user due to the dial’s maximum guess of 1024)

Change the game-play mode to Wheel Spinning

Display wheel spinning rules and instructions on the LCD

                                End-If

                                Else

Display on the LCD that the user has lost along with his/her final guess, the correct price, and the computer’s lower bound guess

Turn off all three pairs of LEDs

Change the game-play mode to Pre-Game

Turn the wheel’s motor (PWM Channel 0 at 100% duty cycle)

Wait two seconds

Turn off the wheel motor

                                End-Else

                End-If

                If the game-play mode is Wheel Spinning

                                If the current event is wheel moving

                                                Start a timer for two seconds

                                                Get the current wheel’s position by calling the Encoder Function

                                                If the wheel’s position divided by three has a remainder of zero

                                                                Toggle the pair of green LEDs on and off

                                                                Toggle the buzzer on and off

                                                End-If

If the wheel’s position divided by three has a remainder of one

                                                                Toggle the pair of red LEDs on and off

                                                                Toggle the buzzer on and off

                                                End-If

Else

                                                                Toggle the pair of yellow LEDs on and off

                                                                Toggle the buzzer on and off

                                                End-Else

                                End-If

                                If the current event is the timer being expired

                                                Turn off the buzzer

                                                If the wheel was spun less than one complete revolution

                                                                Display on the LCD that the user must spin the wheel again

                                                                Set the last wheel position to the current wheel position

                                                End-If

                                                Else

Set the dollar value equal to the current wheel position’s dollar amount

Add the dollar value to the user’s total amount

If the total amount is greater than one dollar

Display on the LCD that the user went over a dollar and lost the game; display the user’s total amount

Turn off all three pairs of LEDs

Set the last wheel position to the current wheel position

Change the game-play mode to Pre-Game

Turn the wheel’s motor (PWM Channel 0 at 100% duty cycle)

Wait two seconds

Turn off the wheel motor

                                                                End-If

                                                                If the total amount equals one dollar

Display on the LCD that the user got one dollar and won the game

Set the last wheel position to the current wheel position

Call the SWAG Dispense Function to give the winner some SWAG

Turn off all three pairs of LEDs

Change the game-play mode to Pre-Game

Turn the wheel’s motor (PWM Channel 0 at 100% duty cycle)

Wait two seconds

Turn off the wheel motor

                                                                End-If

                                                                Else

Display on the LCD the dollar amount the user has obtained on the wheel along with the total amount

Display on the LCD that the user may spin again and can finish the spinning by pressing the button

Set the last wheel position to the current wheel position

                                                                End-Else

                                                End-Else

                                End-If

                                If the current event is the button being pushed and the timer has expired

                                                If the total amount is zero

Display on the LCD that the user must spin the wheel at least once before accepting the total amount

                                                End-If

                                                Else

                                                                Display on the LCD the user’s final score

Determine the computer’s final score (random number between 60 and 100 to make the game somewhat difficult)

                                                                If the user’s final score is greater than the computer’s final score

Display on the LCD that the user has won along with the computer’s final score

Call the SWAG Dispense Function to give the winner some SWAG

Turn off all three pairs of LEDs

Change the game-play mode to Pre-Game

Turn the wheel’s motor (PWM Channel 0 at 100% duty cycle)

Wait two seconds

Turn off the wheel motor

                                                                End-If

                                                                Else

Display on the LCD that the user has lost along with the computer’s final score

Turn off all three pairs of LEDs

Change the game-play mode to Pre-Game

Turn the wheel’s motor (PWM Channel 0 at 100% duty cycle)

Wait two seconds

Turn off the wheel motor

                                                                End-Else

                                                End-Else

                                End-If

                End-If

End-If

 

BUTTON PRESS HANDLING FUNCTION

Input: None

Returns: None

 

Prints a message indicating if the button was pressed successfully

 

WHEEL POSITION ENCODER FUNCTION

Input: Current event

Returns: Wheel’s current position

 

Declare a Last Event variable (initially equal to zero)

 

If the Last Event was Tape Sensor 1 HI and the Current Event is Tape Sensor 2 HI

                The wheel is turning in direction 0

End-If

If the Last Event was Tape Sensor 1 LO and the Current Event is Tape Sensor 2 LO

                The wheel is turning in direction 0

End-If

If the Last Event was Tape Sensor 2 HI and the Current Event is Tape Sensor 1 HI

                The wheel is turning in direction 1

End-If

If the Last Event was Tape Sensor 2 LO and the Current Event is Tape Sensor 1 LO

                The wheel is turning in direction 1

End-If

 

If the Current Event is Tape Sensor 1 HI or the Current Event is Tape Sensor 1 LO

                If the wheel is turning in direction 1

                                Subtract one from the wheel’s current position

                End-If

                Else

                                Add one to the wheel’s current position

                End-Else

End-If

If the Current Event is the wheel’s final position

                If the wheel is turning in direction 0

                                Return the wheel’s current position minus one (based on experimental results)

                End-If

                Else

                                Return the wheel’s current position plus one (based on experimental results)

                End-Else

End-If

If the Current Event is Tape Sensor 3 HI

The wheel’s current position is the current position minus the remainder of the current position divided by 16 (total number of wheel positions)

End-If

Set the Last Event equal to the Current Event

Return the wheel’s current position

 

LED AND BUZZER ON/OFF FUNCTIONS

Input: Desired on/off state (buzzer: 1 is on, 0 is off; LEDs: 1 is off, 0 is on)

Return: None

 

BUZZER:

If desired state is on

                Set AD2 HI

End-If

Else

                Set AD2 LO

End-Else

 

Red LEDs:

If desired state is on

                Set M3 HI

End-If

Else

                Set M3 LO

End-Else

 

Green LEDs:

If desired state is on

                Set M4 HI

End-If

Else

                Set M4 LO

End-Else

 

Yellow LEDs:

If desired state is on

                Set M2 HI

End-If

Else

                Set M2 LO

End-Else

 

WHEEL POSITION AND DOLLAR MATCH FUNCTION

Input: Current wheel position

Returns: Corresponding dollar amount

 

Consider all possible dollar amounts on the wheel for the remainder of the current wheel position divided by 16:

                Position 0: Return 95 (cents)

                Position 1: Return 30

Position 2: Return 70

Position 3: Return 10

                Position 4: Return 65

                Position 5: Return 40

Position 6: Return 20

Position 7: Return 60

Position 8: Return 35

Position 9: Return 80

Position 10: Return 15

Position 11: Return 100

Position 12: Return 5

Position 13: Return 90

Position 14: Return 25

Position 15: Return 45

 

End of considering possible dollar amounts on the wheel

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

               

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MODULE: LCD.c

 

LCD ENABLE FUNCTION

Input: None

Returns: None

 

Declare a dummy variable

 

Set AD3 enable line HI

For the dummy variable equaling one to seventy

                Set the dummy variable equal to the for-loop incrementing variable

End-For

 

Set AD3 enable line LO

 

LCD REGISTER SELECT FUNCTION

Input: Set value (1 for data mode, 0 for command mode)

Returns: None

 

If set value equals one

                Set AD4 HI

End-If

Else

                Set AD4 LO

End-Else

 

LCD DB WRITE FUNCTION

Input: A hexadecimal number for desired LCD display functions

Returns: None

 

If hex-number and bit 0 HI equal one

                Set T3 HI

End-If

Else

                Set T3 LO

End-Else

If hex-number and bit 1 HI equal one

                Set T4 HI

End-If

Else

                Set T4 LO

End-Else

If hex-number and bit 2 HI equal one

                Set T5 HI

End-If

Else

                Set T5 LO

End-Else

If hex-number and bit 3 HI equal one

                Set T6 HI

End-If

Else

                Set T6 LO

End-Else

If hex-number and bit 4 HI equal one

                Set T7 HI

End-If

Else

                Set T7 LO

End-Else

If hex-number and bit 5 HI equal one

                Set AD7 HI

End-If

Else

                Set AD7 LO

End-Else

If hex-number and bit 6 HI equal one

                Set AD6 HI

End-If

Else

                Set AD6 LO

End-Else

If hex-number and bit 7 HI equal one

                Set AD5 HI

End-If

Else

                Set AD5 LO

End-Else

 

Call the LCD Enable Function

 

LCD INITIALIZATION FUNCTION

Input: None

Returns: None

 

Initialize timer module with 1-ms ticks

Call LCD Select Register Function for command mode

 

Call LCD DB Write Function with 0x30

Wait 5 milliseconds

Call LCD DB Write Function with 0x30

Wait 1 millisecond

Call LCD DB Write Function with 0x30

Wait 1 millisecond

Call LCD DB Write Function with 0x08

Call LCD DB Write Function with 0x01

Wait 6 milliseconds

Call LCD DB Write Function with 0x06

Call LCD DB Write Function with 0x0F

 

Call LCD DB Write Function with 0x00; clears the display and sets the next address to be written to 0 and restores any shift that was applied

Call LCD DB Write Function with 0x07; the display will shift after every write and the cursor will increment (move right) after every write

Call LCD DB Write Function with 0x94; sets the next address to be written within the display RAM (80 bytes)

 

Call LCD Select Register Function for data mode

 

TIME WAIT FUNCTION

Input: Number of 1-ms ticks to wait

Returns: None

 

Start a timer with the number of 1-ms ticks to wait

While the timer has not expired

                Wait

End-While

 

LCD CHARACTER DISPLAY FUNCTION

Input: Character to be displayed

Returns: None

 

Calls LCD DB Write Function with character to be displayed

 

LCD STRING DISPLAY FUNCTION

Input: Character string to be displayed

Returns: None

 

Find length of character string array to be displayed

Display character string and wait 100 milliseconds in between each character

 

LCD DIAL DISPLAY FUNCTION

Input: Dial reading

Returns: None

 

Update dial’s current reading (user’s price guess)

Copy dial’s current reading to a string

 

Depending on the number of digits in the dial’s reading, copy the dial’s reading to a string

 

Display each digit of the dial’s reading on the LCD, waiting 1 millisecond in between each digit

 

WRITE TO LCD FIRST LINE FUNCTION

Input: None

Returns: None

 

Call LCD Select Register Function for command mode

Call LCD DB Write Function with 0x80 (sets the next address to be written at the first character in the top line)

Call LCD Select Register Function for data mode

 

WRITE TO LCD SECOND LINE FUNCTION

Input: None

Returns: None

 

Call LCD Select Register Function for command mode

Call LCD DB Write Function with 0xC5 (sets the next address to be written at the first character in the second line)

Call LCD Select Register Function for data mode       

 

 

 

Return to main page.