Overview | Main | State Machine | Events | IR | Tape | Drivetrain | PWM | Flash | Two Minute Timer | Bump

lightSensor.c
lightSensor.h

void initLights(void): initialize the IR sensing subsystem

void getIRDutyCycle(void): get the duty cycle being seen by a given sensor

void interrupt _Vec_tim0ch4 ICInterrupt0-7(void): process IR light sensing

void interrupt _Vec_tim2ch7 OutputCompareInterrupt(void): notice zero duty cycles

void main(void): test harness for IR module

The module for sensing, reading, and interpreting the incoming IR signal. It expects a duty cycle from the hardware at a 1.25KHz frequency.

PSEUDOCODE

(Download Pseudocode .doc 30kb)

void initLights(void)
    Set all of port T to be an input
    Enable Timer 0
    Set Timer 0 to a 3MHz clock rate
    Enable Timer 1
    Set Timer 1 to a 3MHz clock rate    
    Turn the timer system on for Timer 2
    Set pre-scale to /128 or 187.5 kHz clock
    
    Set up TIM0_IC4 as an IC
    Enable TIM0_TC4 as an interrupt
    Set TIM_0 IC4 to capture any edge
    Clear TIM_0 IC4 flag
    
    Set up TIM0_IC5 as an IC
    Enable TIM0_TC5 as an interrupt
    Set TIM_0 IC5 to capture any edge
    Clear TIM_0 IC5 flag    
    
    Set up TIM0_IC6 as an IC
    Enable TIM0_TC6 as an interrupt
    Set TIM_0 IC6 to capture any edge
    Clear TIM_0 IC6 flag
    
    Set up TIM0_IC7 as an IC
    Enable TIM0_TC7 as an interrupt
    Set TIM_0 IC7 to capture any edge
    Clear TIM_0 IC7 flag

    Set up TIM1_IC4 as an IC
    Enable TIM1_TC4 as an interrupt
    Set TIM_1 IC4 to capture any edge
    Clear TIM_1 IC4 flag

    Set up TIM1_IC5 as an IC
    Enable TIM1_TC5 as an interrupt
    Set TIM_1 IC5 to capture any edge
    Clear TIM_1 IC5 flag

    Set up TIM1_IC6 as an IC
    Enable TIM1_TC6 as an interrupt
    Set TIM_1 IC6 to capture any edge
    Clear TIM_1 IC6 flag

    Set up TIM1_IC7 as an IC
    Enable TIM1_TC7 as an interrupt
    Set TIM_1 IC7 to capture any edge
    Clear TIM_1 IC7 flag

    set pre-scale to /128 or 187.5 kHz clock
    
    Set up TIM2_OC7 as an OC
    Enable TIM2_TC7 as an interrupt
    Set TIM2_TC7 to output capture
    There is no pin connected to TIM2_OC7
    Set first compare for TIM2_OC7
    Clear flag for TIM2_OC7

    Set every value in the dutyCycle array to 0
end initLights

void getDutyCycle(char sensor)
    return the current duty cycle
end GetDutyCycle

Note: There are 8 input capture interrupts, each the same as below.
void interrupt _Vec_tim0ch4 ICInterrupt0-7(void)
    Record the current time
    If we are on a rising edge
        Set lastTime to currTime
        Set the lastTimeFlag to 0
    end if
    If we are on a falling edge
        If an overcount occured
            Set sawDutyCycle to 100-(((currTime<<1) - (lastTime+currTime))/(PERIOD/100))
            Add sawDutyCycle to integrateAvg
        end if
        else we have a non overcount situation
            Set sawDutyCycle to 100-((currTime-lastTime)/(PERIOD/100));
            Add sawDutyCycle to integrateAvg
        end else
        Increment the amount of times we saw a full duty cycle
    end if
    if we have counted integrateTimes to 200
        Average the duty cycle, and set it to dutyCycle
        Reset integrate average
    end if
    Reset the interrupt flag
end interrupt

void interrupt _Vec_tim2ch7 OutputCompareInterrupt(void)    
    Iterate through lastTime flag and increment its value
    If lastTimeFlag is at its maximum
        We must have a zero percent duty cycle
    end if
    Clear the flag
     Set the next output compare
end interrupt

/*Test Harness*/
void main()
    Initialize the module
    Enable Interrupts
    Iterate through the duty cycle array and print its value
end main