timer Module
Files:
Interface:
Function: InitPorts
Arguments: None.
Returns: None.
Description:Initializes the AD Ports.
Function: InitTimer
Arguments: None.
Returns: None.
Description:Initializes the Absolute Timer
Function: StartTimer
Arguments: Character representing the timer number and unsigned long representing timer duration in ms
Returns: CornerBeacons_t structure containing a character representing the side, the duty of the opponents side, and the duty of the friendly side.
Description:Returns the CornerBeacons structure for the current side.
Timer 1 is Left Encoder Stall Test
Timer 2 is Right Encoder Stall Test
Timer 3 is ControlLaw Update
Function: IsTimerExpired
Arguments: Character representing the timer number
Returns: Character representing TRUE when timer has expired.
Description:Checks if requested timer has expired.
Function: GetAbsoluteTime
Arguments: None
Returns: Unsigned long representing the absolute time in ticks
Description:retrieves the detected duty of the referenced beacon.
Pseudo-Code:
void InitPorts(void)
Initialize ports to "IIIAAAAA" configuration
void InitTimer(void)
Set up Timer 0 which is for the encoders
Turn the timer system on
Set timer prescale to divide by 128
Enable interrupts for IC4, IC5, OC6
Capture rising edges on 4,5,7
Set timer 0-6 as output compare
Schedule the first OC6 event
Clear IC4, IC5, OC6, IC7 flag
Clear overflow flag
Turn on overflow interrupts
Second, Timer 1 which is for the beacons, and the absolute timer
Turn the timer system on
Set timer prescale to divide by 128
Enable interrupts for IC4-7
Set up Timers for capturing beacon edges, and clear flags
Capture all edges on pin 4
Capture all edges on pin 5
Capture all edges on pin
Capture all edges on pin 7
Clear IC4-IC7 flags
Clear overflow flag
Turn on overflow interrupts
Initialize absolute timer
Set all timers to not started
Start Flag Captured Timer
void StartTimer(char TimerNum, unsigned long ms_duration)
Calculate ms value in clock ticks. May be truncated
Calculate end time and store in timer
char IsTimerExpired(char TimerNum)
If Timer has started and the value of the timer is less than the absolute time
Reset the Timer Expired flag
Return TRUE
Else
Return FALSE
unsigned long GetAbsoluteTime(void)
Disable overflow interrupts
Set the Absolute Time to the current tick count in the timer register
Enable overflow interrupts
Return the Absolute Time in ticks
short GetBeaconLevel(char BeaconPin)
Return the AD value on the requested pin
unsigned char Orientation(void)
If Front two beacons detect 70% duty
Return "Facing 70" value
Else If Front two beacons detect 30% duty
Return "Facing 30" value
Else If Left beacon detects 70% duty and Right beacon detects 30% duty
Return 70LEFT_30RIGHT
Else if Left beacon detects 30% duty and Right beacon detects 70% duty
Return Return 30LEFT_70RIGHT
Else
Return NO_SIGNAL_DETECTED
Interrupts:
void interrupt _Vec_tim1ovf Tim1OverflowInterrupt(void)
Increments the overflow counter for timer 1, for absolute timing purposes
Increment Overflow Counter
Clear Overflow interrup flag