Anda di halaman 1dari 14

ADMulti.

- Sets up ADC0 to convert up to 4 channels using SS2

Define Module-level Arrays (4 elements):


HowMany2Mask, HowMany2Mux, HowMany2CTL
Declares Module-Level-Variable: NumChannelsConverting
Two Functions:
ADC_MultiInit
Initializes the A/D converter to convert on channels 1-4
Input: Number of A/D converters needed
Output:
None
Define function variable: delay
Define function variable: index (to represent the index for HowMany2Mask, HowMan
y2Mux, HowMany2CTL arrays)
Make sure the number of A/D converters ranges from 1 4
Activate ADC0, clock for Port E
Set PE0, PE1, PE2, PE3 pins as inputs
Enable alternating function and disable digital function on PE0, PE1, PE2, PE3 p
ins
Enable analog function on PE4, PE5 pins
Configure sample rate to 125000 samples/second
Set sequencer 3 as lowest priority, and disable sample sequencer 2
Set appropriate channel for SS2 using elements in HowMany2Mux
Set appropriate sample for SS2 using elements in HowMany2CTL
Disable SS2 interrupts, and enable sample sequencer 2
ADC_MultiRead
Input:
Output:

Triggers A/D conversion and waits for result


data
None

Declare i as count for the for loop


Initialize SS2, and wait for A/D conversion(s) to complete
Reads results one at a time using i
Clear int to acknowledge completion

ButtonService.c
Declare module variable MyPriority
Define module buttonstate variable CurrentState = InitButton
Set module variable DelayTime
Three Functions:
InitButtonService
Initializes Button Service during ES_Init
Input:
Priority of the service
Output:
Boolean. False only when there is initialization error
Set ES_Event as This Event, and set MyPriority = Priority
Post the Initialization event
PostButtonService
Input:
ES_Event This Event
Output:
Boolean. False when queue operation failed
Return ES_PostToService
End PostButtonService
RunButtonService
Input:
ES_Event This Event
Output:
ES_Event
Set ReturnEvent as ES_NO_EVENT
If CurrentState is InitButton
Set CurrentState as Debouncing
Call ES_Timer_InitTimer with DEBOUNCE_TIMER and DelayTime
Else if CurrentState is Debouncing
If the EventType is ES_TIMEOUT
Set CurrentState as Ready2Check
Else if CurrentState is Ready2Check
Call ES_Timer_InitTimer with DEBOUNCE_TIMER and DelayTime
Set CurrentSTate as Debouncing
Endif
Return ReturnEvent
End RunButtonService

ButtonSM.c

Button State Machine

Declare Module Variables: CurrentState, LastStartState, LastTriggerState, LastSt


artTacState, LastEndTacState, LastReceiverState, MyPriority
Functions
InitButtonSM
Input:
Output:

Priority
Boolean. False when error in initialization

Set MyPriority = Priority


Set CurrentState as DebouncingButton to put into initial pseudo-state
Initialize Music Shift Register and write 0x0000
Activate Start Button by initializing pin PC4 as digital input
Activate Start TAC (button for tanker s start state) by initializing pin PE5 as di
gital input
Activate IR Gun by initializing pins PD0, PD1 as digital input
Activate End TAC (Button for tanker s end state) by initializing pin PF4 as digita
l input
Read pins PC4, PE5, PF4, PD0, PD1 and respectively save it to these variables La
stStartState, LastStartTacState, LastEndTacState, LastTriggerState, LastReceiver
State
Post the initial transition event with ES_Timer_InitTimer
Return True
End InitButtomSM
Check4StartButton
takes no input, returns boolean
Set ReturnVal as false initially.
Sample pin PC4 and store value into CurrentStartState
If CurrentStartState does not equal to LastStartState
Set ReturnVal to true
If CurrentStartState is not 0
Declare ThisEvent as ES_Event
Set EventType as Start_Button_HI_E
Post Event to PostButtonSM
Else
Declare ThisEvent as ES_Event
Set EventType as Start_Button_LO_E
Post Event to PostButtonSM
End If
End if
Set LastStartState to CurrentStartState
Return ReturnVal
End Check4StartButton
Check4GunTrigger takes no input, returns Boolean
Set ReturnVal as false initially.
Sample pin PD0 and store value into CurrentTriggerState
If CurrentTriggerState does not equal to LastTriggerState
Set ReturnVal to true
If CurrentStartState is not 0

Declare ThisEvent as ES_Event


Set EventType as Start_Trigger_HI_E
Post Event to PostButtonSM
Else
Declare ThisEvent as ES_Event
Set EventType as Start_Trigger_LO_E
Post Event to PostButtonSM
End If
End if
Set LastTriggerState to CurrentTriggerState
Return ReturnVal
End Check4StartButton
Check4StartTac takes no input, returns boolean
Set ReturnVal as false initially.
Sample pin PE5 and store value into CurrentStartTacState
If CurrentStartTacState does not equal to LastStartTacState
Set ReturnVal to true
If CurrentStartTacState is not 0
Declare ThisEvent as ES_Event
Set EventType as Start_Tac_HI_E
Post Event to PostButtonSM
Else
Declare ThisEvent as ES_Event
Set EventType as Start_Tac_LO_E
Post Event to PostButtonSM
End If
End if
Set LastStartTacState to CurrentStartTacState
Return ReturnVal
End Check4StartTac
Check4EndTac takes no input, returns Boolean
Set ReturnVal as false initially.
Sample pin PF4 and store value into CurrentEndTacState
If CurrentEndTacState does not equal to LastEndTacState
Set ReturnVal to true
If CurrentEndTacState is not 0
Declare ThisEvent as ES_Event
Set EventType as End_Tac_HI_E
Post Event to PostButtonSM
Else
Declare ThisEvent as ES_Event
Set EventType as End_Tac_LO_E
Post Event to PostButtonSM
End If
End if
Set LastEndTacState to CurrentEndTacState
Return ReturnVal
End Check4EndTac
Check4IR takes no input, returns Boolean
Set ReturnVal as false initially.
Sample pin PD1 and store value into CurrentReceiverState
If CurrentReceiverState does not equal to LastReceiverState
Set ReturnVal to true
If CurrentReceiverState is not 0

Declare ThisEvent as ES_Event


Set EventType as IR_RECEIVER_HI_E
Post Event to PostButtonSM
Else
Declare ThisEvent as ES_Event
Set EventType as IR_RECEIVER_LO_E
Post Event to PostButtonSM
End If
End if
Set LastReceiverState to CurrentReceiverState
Return ReturnVal
End Check4IR
PostButtonSM Post event to this State Machine s queue
Return ES_PostToSErvice(MyPriority, ThisEvent);
RunButtonSM

Run the Button State Machine

Set New_Event and ReturnEvent


Assume no errors and return ES_No_Event
Switch Block:
If CurrentState = DebouncingButton
If the Event type is ES_TIMEOUT
If Event Parameter is BUTTON_TIMER
Set CurrentState to Ready2SampleButton
Call Music Shift Register Write Function
Endif
Default
If CurrentState = Ready2SampleButton
If Event type is START_BUTTON_HI_E
Call Timer with appropriate timer values
Set CurrentSTate to DebouncingButton
If the Query game state equals WaitingToStart
Declare GameStarted Event
Set Event type for GameStarted event to Game_Started
Post the GameStarted Event to all services
End if
If Event type is START_BUTTON_LO_E
Call Timer with appropriate timer values
Set CurrentState to DebouncingButton
If Event type is GUN_TRIGGER_LO_E
Call Timer with appropriate timer values
Set CurrentSTate to DebouncingButton
If the LastReceiverState is not 0
Post TargetShot Event to GAMEBRAINSM & GAMESTATESM
Post Play_Effect with Successful_Shot parameter to MusicService
Else
Post Play_Effect with Missed_Shot parameter to MusicService
If Event type is GUN_TRIGGER_HI_E
Call Timer with appropriate timer values
Set CurrentSTate to DebouncingButton
If Event type is START_TAC_LO_E
Call Timer with appropriate timer values
Set CurrentSTate to DebouncingButton

If Event type is START_TAC_HI_E


Call Timer with appropriate timer values
Set CurrentSTate to DebouncingButton
Post GameReady with Game_Reset parameter to all services
If Event type is END_TAC_LO_E
Call Timer with appropriate timer values
Set CurrentSTate to DebouncingButton
If Event type is END_TAC_HI_E
Call Timer with appropriate timer values
Set CurrentSTate to DebouncingButton
Post GameOver with Game_Over parameter to all services
If Event type is GAME_OVER
If LastStartTacState is not zero
Post GameReset with GAME_RESET parameter to all services
End if
If Event type is PLAY_EFFECT
Call Timer with appropriate timer values
Set CurrentSTate to DebouncingButton
Write to Music Shift Register with the event s parameter
Repeat the state pattern required for other states
End Switch statement
Return ReturnEvent
End RunBUTTONSM function
QuertButtonSMService
Returns the CurrentState
CheckLastStartTacState
Returns the LastStartTacState

LightService.c

Service for the 7 segment displays (for the score board)

Declare module variable MyPriority


Define module array variable (10 elements) Segment
Functions
InitLightsService
input Priority and output boolean
Declare ThisEvent and set MyPriority as Priority
Initialize Shift Registers for Guitar, Score, 7 Wheel Lights, and Car Targets
Activate Port A and wait for it
Sets pin PA7 as digital output
Post initial transition event
PostLightsService
Post ES_service
RunLightsService
Declare ReturnEvent and assume no errors
If the event type is NEW_TARGET_CAR
Set New_Target equals to the Event s parameter
Write to the Shift Registers for Target Cars and light the corresponding
LED
If the event type is NEW_SCORE
Set Display_Value to 0
Define the Hundred, Ten, One Place values with equations
Define the correct Display_Value by using the Segment array
Write the Display_Value to the three 7-segment displays
If the Event type is NEW_WHEEL_SEGMENT
Write the event s parameter into the shift register for the 7 LEDS
If the Event type is FLAMES_ON
Set the pin for fan as high
If the event type is FLAMES_OFF
Set the pin for fan as low
If the event type is NEW_NOTE_PATTERNLOW16
Write the parameter into the Guitar s Shift register (low 16 bits)
If the event type is NEW_NOTE_PATTERNHIGH16
Write the parameter into the Guitar s Shift register (high 16 bits)
If the event type is NEW_NOTE_PATTERNHIGH8
Write the parameter into the Guitar s Shift register (high 8 bits)
ShiftRegisterWrite.c low level interface to write data into shift registers
Create a static variable LocalRegisterImage and set it equal to 0
Functions
SR_Init
no inputs or outputs
Initialize pin PB1 as digital output
Initialize pin PB3 as digital input
Set the data and shift clock bits as low
Set the register clock as high
SR_Write no output. Inputs a 8 bit value
Lower the register clock

Shift each 8-bit data one by one.


If the value is 0, set data line as low. If the value is 1, set data line as hig
h.
Pulse shift clock
Raise register clock
Pseudo-code for the GameBrainSM
Data Private to GameBrainSM: Score; HighScore; MyPriority
InitGameBrainSM
Set MyPriority to the parameter passed in.
Set CurrentState to BrainOff
Set Score and HighScore to 0
Post EventType NEW_SCORE, EventParam Score to TestHarness.
Post HAVENT_STRUMMED_TIMER, HAVENT_STRUMMED_TIME to ES_Timer_SetTimer
Post NEW_TARGET_TIMER, NEW_TARGET_DELAY to ES_Timer_SetTimer
PostGameBrainSM
Post MyPriority and ThisEvent to ES_PostToService.
RunGameBrainSM
Event Types in this SM: NEW_SCORE, FLAMES_ON, FLAMES_OFF , TURN_OFF_SONG, TURN_
ON_SONG, GUITAR_ANIMATION_OVER, TARGETSHOT, CORRECT_STRUM, ADD_POINTS
Switch Block on Current State:
If CurrentState is BrainOff
If ThisEvent.EventType is GAME_STARTED
Remove HighScore
Set First Target Car
Turn on Flames
End if
If ThisEvent.EventType is GUITAR_ANIMATION_OVER
Post HighScore
End if
If CurrentState is BrainOn
If ThisEvent.EventType is GAME_OVER
Recalculate HighScore
Turn Flames Off
Turn off song
If Score > WINNING_SCORE
Play winning music
Else
Play Losing Music
End if
If ThisEvent.EventType is TARGETSHOT
Post Score++
Play Target Shot Music
Create New_TARGET_CAR
If ThisEvent.EventType is CORRECT_STRUM
Post Score++
If ThisEvent.EventType is ADD_POINTS
Post Score++
End Switch Block
End RunGameBrainSM

QueryGameBrainSM
Return CurrentState
QueryGameBrainScoreSM
Return Score
Pseudo-code for the GuitarHeroSM.c
Data Private to GuitarHeroSM: CurrentState, MyPriority, Lowest16Bits, Highest16B
its, Highest8Bits
Array Private to GuitarHeroSM: GuitarNotes[9], GuitarNotesQueue[8], GuitarScreen
Characters[3], WinningSequence[], LosingSequence[]
InitGuitarHeroSM
Set MyPriority to value passed in
Set CurrentState to GuitarInactive
Pass in GUITAR_FLASH_TIMER, GUITAR_FLASH_DELAY to ES_Timer
RunGuitarHeroSM
Main EventType in this SM: ES_TIMEOUT, STRUM_OCCURED, GAME_OVER, GuitarInactive,
GameOverAnimation
Switch Block on CurrentState:
If CurrentState is GuitarActive
If ThisEvent.EventType is ES_TIMEOUT
If ThisEvent.EventParam is NOTE_TIMER
Shift and add notes
Set ES_TIMER_InitTimer
End if
If ThisEvent.EventParam is INACTIVE_TIMER
Post Event IncorrectStrum
End
End if
If CurrentState is STRUM_OCCURED
If ThisEvent.EventParam is GuitarNotesQueue[0]
Shift and Add Notes
Post Event Correct_Strum
Else
Post Event Incorrect_Strum
Post Event PlayEffect
End if
If CurrentState is GAME_OVER
Set CurrentState to GameOverAnimation
Set ES_TIMER to RESETTING
If CurrentState is GuitarInactive
If EventType is ES_TIMEOUT
If EventParam is GUITAR_FLASH_TIMER
Shift and Add Notes
Post NewWheelSegment Event
Post New target Event
End if
End if
If CurrentState is GAME_STARTED
For loop 8 times
Shift and add notes

End for loop


Set CurrentState to GuitarActive.
If Current State is GameOverAnimation
If EventType is ES_TIMEOUT and parameter is RESETING_CHAR_TIMER and QueryGameBra
inScoreSM > Winning Score
Print Winning Sequence on screen
Else
Print Losing Sequence on screen
End if
End Switch Block
Return ReturnEvent
QueryGuitarHeroSM
Return CurrentState
DisplayNewNotes
Create 16 bit variables ClearedLowest16bits, ClearedHighest16bits, ClearedHighe
st8bits
Set all bits high.
Set Lowest16Bits, Highested16Bits and Highest8Bits to the cleared values.
ShiftAndAddNotes
For loop 7 times
Set fist 7 notes same
End for loop
Set last bit to be a random number between 1 to 5.
PrintGuitarScreen
Post 3 new events with passed in value array as event parameter.
NextWinningSequence
If count is 31
Set CurrentState to be GuitarInactive
Post GUITAR_ANIMATION_OVER TO A NEW event
Set count to 0
Set timer
PrintGuitarScreen
Else
Set timer
End if
increment count
Return WinningSequence.
NextLosingSequence
If count is 31
Set CurrentState to be GuitarInactive
Post GUITAR_ANIMATION_OVER TO A NEW event
Set count to 0
Set timer
PrintGuitarScreen
Else
Set timer
End if
Increment Count
Return LosingSequence.
Pseudo-code for the GuitarHeroSR

Private variables: HIGH16_REGISTER_STATE, LOW16_REGISTER_STATE, HIGHEST8_REGISTE


R_STATE
GuitarHero_SR_Init
Activates port B
Set PB3,PB4,PB5 to be digital output pins
GuitarHero_SR_WriteLOW16
Save the new value as the current register state
For loop 16 times
Checks if the Most Significant Bit is a one
Make data line high then low
Pulse the shift clock
End for loop
Pulse the register clock
GuitarHero_SR_WriteHIGH16
Save the new value as the current register state
For loop 16 times
Checks if the Most Significant Bit is a one
Pulse Data LIne
Pulse the shift clock
End for loop
Pulse the register clock
GuitarHero_SR_WriteHIGH8
Save the new value as the current register state
For loop 8 times
Checks if the Most Significant Bit is a one
Pulse Data Line
Pulse the shift clock
End for loop
Pulse the register clock
Pseudo-code for the Lights_Service
Private variables to module: MyPriority, Segment[10]
InitLightsService
Initialize Port functions
MyPriority = Priority
Initialize shift registers for Notes, Target Car, Score, and 7 wheel lights
Activate port A
Set PA7 to be digital output pin
Post the initial transition event
RunLightsService
EventType: NEW_TARGET_CAR, NEW_NOTE_PATTERN, NEW_SCORE, NEW_WHEEL_SEGMENT, FLAME
S_ON, FLAMES_OFF
If ThisEvent.EventType is NEW_TARGET_CAR
Write value to target car
Else if ThisEvent.EventType is NEW_NOTE_PATTERN
Notes_write new notes and current register values
Else if ThisEvent.EventType is NEW_SCORE
Write Display value to score write
Else if ThisEvent.EventType is NEW_WHEEL_SEGMENT
Write event parameter to wheel_lights_write
Else if ThisEvent.EventType is FLAMES_ON
Set Flame bits high
Else if ThisEvent.EventType is FLAMES_OFF

Set Flame bits low


End if
Return ReturnEvent
Pseudo-code for the MusicService.c
Private variable: MyPriority
InitMusicService
Set MyPriority to the value passed in.
Initialize Shift Registers for Music
Set event type to ES_INIT
RunMusicService
Set RegisterState to Music_SR_getCurrentRegister()
If event type is PLAY_EFFECT
Write RegisterState to Music_SR_Write. Pull it high then low
Else if event type is ES_INIT
Write 0x0000 to music SR
Else if Event type is PLA_MAIN_SONG
Write 0x0001 to Music SR
Else if Event Type is TURN_OFF_SONG
Write 0x0000 to Music SR
End if
Return ReturnEvent
Pseudo-code for the MusicSR
Private variable: REGISTER_STATE
Music_SR_Init
Activates port C
Sets PC5,PC6 ,and PC7 to be digital output pins
Music_SR_Write
Save the REGISTER_STATE as a NewValue
For loop because there are two Shift register in series
Checks if the Most Significant Bit is a one
Make data line high then low
Pulse the shift clock
End for loop
Pulse the register clock
Pseudo-code for the ScoreSR
Score_SR_Init
Activates port F
Sets PF1,PF2 ,and PF3 to be digital output pins
Music_SR_Write
Save the REGISTER_STATE as a NewValue
For loop 24 times
Checks if the Most Significant Bit is a one
Make data line high then low
Pulse the shift clock
End for loop
Pulse the register clock
Pseudo-code for the StrumDebounceSM
Private Variables: CurrentState, LastStrumState, MyPriority

InitStrumDebounceSM
Set MyPriority to the value passed in.
Activate Port E for Strum, Port D for Guitar Buttons
Activate Port F
Post the initial transition event
Set timer
CheckStrumEvents
Set ReturnVal to be false
Set CurrentButtonState
If CurrentButtonState not equal to LastStrumState
Set ReturnVal to be true
If CurrentButtonState is not equal to 0
Post event STRUM_DOWN
Else
Post STRUM_UP Event
End if
Set LastStrumState to be CurrentButtonState
Return ReturnVal
RunStrumDebounceSM
Event Types: Debouncing, Ready2Sample
Switch Block of CurrentState
If CurrentState is Debouncing
If Event type is ES_TIMEOUT
If EventParam is STRUM_TIMER
Set CurrentState tobe Ready2Sample
End if
End if
If CurrentState is Ready2Sample
If Event Type is STRUM_UP
Set StartTimer to be STRUM_TIMER
Set CurrentState to be Debouncing.
End if
If Event Type is STRUM_DOWN
Set StartTimer to be STRUM_TIMER
Set CurrentState to be Debouncing.
Post event STRUM_OCCURED
End
End Switch Block
Return ReturnEvent
QueryStrumDebounceSM
Return CurrentState
ReadGuitarButtons
Set ButtonVals to 0
Read ButtonVals from port

Anda mungkin juga menyukai