/**************************************************************************** Module IRmanager.c ****************************************************************************/ /*----------------------------- Include Files -----------------------------*/ /* include header files for this state machine as well as any machines at the next lower level in the hierarchy that are sub-machines to this machine */ #include "ES_Configure.h" #include "ES_Framework.h" #include "BallRequestService.h" #include "S12eVec.h" #include "TopHSM.h" /*----------------------------- Module Defines ----------------------------*/ #define _MS_ *375 #define ON_PERIOD (10 _MS_) #define OFF_PERIOD (30 _MS_) #define MAJORBREAK 250 /*---------------------------- Module Functions ---------------------------*/ /* prototypes for private functions for this service.They should be functions relevant to the behavior of this service */ void interrupt _Vec_tim0ch7 BallRequestInterrupt(void); /*---------------------------- Module Variables ---------------------------*/ // with the introduction of Gen2, we need a module level Priority variable static uint8_t MyPriority; static unsigned char cycleNumber; static unsigned char ballNumber; static unsigned char betweenStepTimeOuts; /*------------------------------ Module Code ------------------------------*/ /**************************************************************************** Function InitIRmanager Parameters uint8_t : the priorty of this service Returns bool, false if error in initialization, true otherwise Description Saves away the priority, and does any other required initialization for this service Notes Author J. Edward Carryer, 01/16/12, 10:00 ****************************************************************************/ bool InitBallRequestService ( uint8_t Priority ) { ES_Event ThisEvent; MyPriority = Priority; cycleNumber = 0; ballNumber = 0; betweenStepTimeOuts = 0; DDRS |= BIT3HI; //IR Emitor Circuit is an Output TIM0_TSCR1 |= _S12_TEN; /* turn the timer system on */ TIM0_TSCR2 |= _S12_PR1 | _S12_PR2; /* set pre-scale to /64 =375kHz timer clk */ TIM0_TIOS |= _S12_IOS7; /* set cap/comp 4 to output compare */ TIM0_TCTL1 &= ~(_S12_OL7 | _S12_OM7); /* no pin connected */ TIM0_TFLG1 = _S12_C7F; /* clear OC4 flag */ EnableInterrupts; // post the initial transition event ThisEvent.EventType = ES_INIT; if (ES_PostToService( MyPriority, ThisEvent) == true) { return true; }else { return false; } } /**************************************************************************** Function PostIRmanager Parameters EF_Event ThisEvent ,the event to post to the queue Returns bool false if the Enqueue operation failed, true otherwise Description Posts an event to this state machine's queue Notes Author J. Edward Carryer, 10/23/11, 19:25 ****************************************************************************/ bool PostBallRequestService( ES_Event ThisEvent ) { return ES_PostToService( MyPriority, ThisEvent); } /**************************************************************************** Function RunTemplateService Parameters ES_Event : the event to process Returns ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise Description add your description here Notes Author J. Edward Carryer, 01/15/12, 15:23 ****************************************************************************/ ES_Event RunBallRequestService( ES_Event ThisEvent ) { ES_Event ReturnEvent; if (ThisEvent.EventType == ES_REQUEST_BALL) { //State Machine Calls For Ball Request To Begin PTP|=BIT4HI; //TURN ON INDICATOR LED FOR FIRST BALL TIM0_TC7 = TIM0_TCNT + ON_PERIOD; /* program next compare */ TIM0_TFLG1 = _S12_C7F; TIM0_TIE |= _S12_C7I; } if (ThisEvent.EventType == ES_TIMEOUT) { if (betweenStepTimeOuts == 15) { if ((ballNumber ==5) /*|| (!Read2Reload())*/ ) { ballNumber = 0; ReturnEvent.EventType = ES_FINISHED_RELOAD; PostMasterSM(ReturnEvent); } else { PTP|=BIT4HI; //TURN ON INIDICATOR LED FOR NEXT CYCLE betweenStepTimeOuts = 0; TIM0_TC7 = TIM0_TCNT +ON_PERIOD; /* program next compare */ TIM0_TFLG1 = _S12_C7F; TIM0_TIE |= _S12_C7I; } } else { betweenStepTimeOuts++; ES_Timer_InitTimer(BALL_REQUEST_TIMER, MAJORBREAK); } } ReturnEvent.EventType = ES_NO_EVENT; // assume no errors return ReturnEvent; } /*************************************************************************** private functions ***************************************************************************/ void interrupt _Vec_tim0ch7 BallRequestInterrupt(void) { static bool lineHigh = true; TIM0_TFLG1 = _S12_C7F; /* clear OC4 flag */ if (lineHigh) { PTS |= BIT3HI; lineHigh = false; cycleNumber ++; TIM0_TC7 += ON_PERIOD; } else { PTS &=BIT3LO; lineHigh = true; if (cycleNumber == 10) { TIM0_TIE &= ~_S12_C7I; cycleNumber = 0; PTP &= BIT4LO; //TURN OFF INDICATOR LED ballNumber++; betweenStepTimeOuts++; ES_Timer_InitTimer(BALL_REQUEST_TIMER, MAJORBREAK); } else { TIM0_TC7 += OFF_PERIOD; } } } /*------------------------------- Footnotes -------------------------------*/ /*------------------------------ End of file ------------------------------*/