CODE
Signaling Module: [Header File] [Pseudocode] [C Code] Timing Module: [Header File] [Pseudocode] [C Code] Pinout: [Pinout Diagram] |
Game Play Module: [Header File] [Pseudocode] [C Code] Main Module: [Header File] [Pseudocode] [C Code] |
Signaling C Code
/* Signal module for Railroad HandCar arcade game */ /**************************************************/ /* Includes */ /**************************************************/ #include <stdio.h> #include <ME218_C32.h> #include <ADS12.h> #include <PWMS12.h> #include "Signaling.h" #include "Timing.h" #include "GamePlay.h" /**************************************************/ /* Reference Program Variables */ /**************************************************/ extern unsigned char VillainPos; /**************************************************/ /* Functions */ /**************************************************/ /* Set DDR port T byte to input, M byte to output */ /* Set port AD I/O bits and initialize PWM module */ void InitializePorts(void) { DDRT = BYTELO; DDRM = BYTEHI; PTM = BYTELO; ADS12_Init("OOOOOOOA"); PTAD |= BIT1HI; PWMS12_Init(); } /* Pulse enable (port M0) to signal jumping motor */ void PulseEnable(void) { PTM |= BIT0HI; Wait(100); PTM &= BIT0LO; } /* Set start indicator LED ON (State != 0) or OFF */ void ReadyLED(char State) { if (State) PTM |= BIT4HI; else PTM &= BIT4LO; } /* Check whether penny inserted; LO means present */ char CheckForPenny(void) { return !(PTT & BIT7HI); } /* Sound the game horn at 'OnTime' amount of time */ void SoundHorn(unsigned int OnTime) { PTAD &= BIT1LO; Wait(OnTime); PTAD |= BIT1HI; } /* Beep (sound game chime) at indicated frequency */ void Beep(char NumTimes) { char i; for (i = 0; i < NumTimes; i++) { SoundHorn(BEEP_TIME); Wait(BEEP_TIME); } } /* Check whether 'start' button has been pressed */ char CheckStartButton(void) { return !(PTT & BIT3HI); } /* Get the current handle position (range 0-1023) */ short HandlePosition(void) { return ADS12_ReadADPin(0); } /* Set port M5 HI or LO to direct puppet handpump */ void Pump(char Direction) { if (Direction == UP) PTM |= BIT5HI; /* HI for UP */ else PTM &= BIT5LO; /* LO for DN */ } /* Set PWM scroll speed on port T0 & adjust range */ void SetScrollSpeed(unsigned int Speed) { unsigned int Duty = 0; if (Speed) Duty = Speed / 2 + 25; PWMS12_SetDuty((unsigned char) Duty, PWMS12_CHAN0); } /* Place villain within servo range from position */ void PlaceVillain(unsigned char Pos) { PWMS12_SetDuty((unsigned char) Pos, PWMS12_CHAN1); } /* Check whether the hand car is over an obstacle */ char AtObstacle(void) { return !(PTT & BIT6HI); } /* Set port M1 HI or LO to direct motor direction */ void Jump(char Direction) { if (Direction == UP) PTM |= BIT1HI; /* HI for UP */ else PTM &= BIT1LO; /* LO for DN */ PulseEnable(); } /* Check whether a jump or landing event occurred */ char GetJumpEvent(void) { static char OldState = DN; char State; if ((PTT & BIT4HI) || (PTT & BIT5HI)) State = DN; else State = UP; if (State == OldState) return NO_EVENT; OldState = State; if(State == UP) { printf("Jumped\r\n"); return JUMPED; } printf("Landed\r\n"); return LANDED; } /* Set port M2 LO to vibrate handle for collision */ void VibrateHandle(char Signal) { if(Signal == ON) PTM |= BIT2HI; else PTM &= BIT2LO; } /* Dispense SWAG for the specified length of time */ void DispenseSWAG(void) { printf("Dispensing Cheez Whiz.\n\r"); PTM |= BIT3HI; Wait(SWAG_TIME); PTM &= BIT3LO; }