You are on page 1of 4

Module level variables:

EdgeCounter, OneShotCount, LastEdgeTime, AvgSignalPeriod, P_Emit, P_EmitTicks

InitReloadCapture
start by enabling the clock to the timer (Wide Timer 1)

enable the clock to Port C

since we added this Port C clock init, we can immediately start


into configuring the timer, no need for further delay
make sure that timer (Timer A) is disabled before configuring

set it up in 32bit wide (individual, not concatenated) mode


the constant name derives from the 16/32 bit timer, but this is a 32/64
bit timer so we are setting the 32bit mode

we want to use the full 32 bit count, so initialize the Interval Load
register to 0xffffffff (its default value)

set up timer A in capture mode (TAMR=3, TAAMS = 0),


for edge time (TACMR = 1) and up-counting (TACDIR = 1)

To set the event to rising edge, we need to modify the TAEVENT bits
in GPTMCTL. Rising edges = 00, so we set the TAEVENT bits

Now Set up the port to do the capture (clock was enabled earlier)
start by setting the alternate function for Port C bit 6 (WT1CCP0)

Then, map bit 6's alternate function to WT1CCP0


7 is the mux value to select WT1CCP0, 16 to shift it over to the
right nibble for bit 6 (4 bits/nibble * 6 bits)

Enable pin on Port C for digital I/O

make pin 4 on Port C into an input

back to the timer to enable a local capture interrupt

enable the Timer A in Wide Timer 1 interrupt in the NVIC


it is interrupt number 96 so appears in EN3 at bit 0

make sure interrupts are enabled globally

Start the input capture timer later when needed


End of InitReloadCapture

StartReloadCapture
now kick the InputCaptureTimer off by enabling it and
enabling the timer to stall while stopped by the debugger
End of StartReloadCapture

StopReloadCapture
now stop the InputCaptureTimer by writing 0 to it
End of StopReloadCapture
ReloadCaptureResponse
start by clearing the source of the interrupt, the input capture event

grab ThisEdgeTime from timer register


calculate SignalPeriod using ThisEdgeTime and LastEdgeTime
increment EdgeCounter
calculate AvgSignalPeriod by caiculating the average period during the
whole EdgeCCounter time

if EdgeCounter is large enough (to exclude noise)


if AvgSignalPeriod is within a proper range (to exlcude noise)
calculate the period that we need to emit to get the ball
post an event to GameMaster with the event type as
EV_EMITTER_DETECTED and event paramete as the period of the reload emitter
restart one shot timer so that it doesn't time out
clear EdgeCounter
else
Clear EdgeCounter and AvgSignalPeriod
end if

update LastEdgeTime with ThisEdgeTime


End of ReloadCaptureResponse

InitReloadOneShot
start by enabling the clock to the timer (Wide Timer 1)

kill a few cycles to let the clock get going

make sure that timer (Timer B) is disabled before configuring

set it up in 32bit wide (individual, not concatenated) mode


the constant name derives from the 16/32 bit timer, but this is a 32/64
bit timer so we are setting the 32bit mode

set up timer B in 1-shot mode so that it disables timer on timeouts


first mask off the TBMR field (bits 0:1) then set the value for
1-shot mode = 0x01

set up timer B DOWN-counting (TBCDIR = 0)


so that rewrting the OneShotTimeout will restart timerB

set timeout

enable a local timeout interrupt. TBTOIM = bit 0

enable the Timer B in Wide Timer 1 interrupt in the NVIC


it is interrupt number 97 so appears in EN3 at bit 1

make sure interrupts are enabled globally


End of InitReloadOneShot

StartReloadOneShot
now kick the timer off by enabling it and enabling the timer to
stall while stopped by the debugger
End of StartReloadOneShot

StopReloadOneShot
now stop timer by writing 0 to enable register
End of StopReloadOneShot

ReloadOneShotIntResponse
start by clearing the source of the interrupt
increment our counter so that we can tell this is running
clear accumulated noise on counts
OneShotTimer timing out means the no beacon pulses detected
post an event to GameMasterSM with event type as EV_EMITTER_MISSED
End of ReloadOneShotIntResponse

InitIRPWM
IR output is PE4 M1PWM2
start by enabling the clock to the PWM Module (PWM1)

enable the clock to Port E

Select the PWM clock as System Clock/32

make sure that the PWM module clock has gotten going

disable the PWM while initializing

program generators to go to 1 at rising compare A/B, 0 on falling compare


A/B
GenA_Normal = (PWM_1_GENA_ACTCMPAU_ONE | PWM_1_GENA_ACTCMPAD_ZERO )
GenB_Normal = (PWM_1_GENB_ACTCMPBU_ONE | PWM_1_GENB_ACTCMPBD_ZERO )

Set the PWM period. Since we are counting both up & down, we initialize
the load register to 1/2 the desired total period. We will also program
the match compare registers to 1/2 the desired high time

Set the initial Duty cycle on A to 50% by programming the compare value
to 1/2 the period to count up (or down). Technically, the value to
program
should be Period/2 - DesiredHighTime/2, but since the desired high time
is 1/2
the period, we can skip the subtract

enable the PWM outputs

now configure the Port E pins to be PWM outputs


start by selecting the alternate function for PB6 & PB7

now choose to map PWM to those pins, this is a mux value of 5 that we
want to use for specifying the function on bits 4

Enable pins 4 Port E for digital I/O

make pins 4 on Port E into outputs

set the up/down count mode, enable the PWM generator and make
both generator updates locally synchronized to zero count
End of InitIRPWM

You might also like