/***************************************************************************
IRSensor.c
ME 218B Project Hats Off
Team Firefox
March 7, 2010
This senses the IR
PT4 of E128, Timer1, channel 4: IR sensor
AD7 of E128: IR sensor
***************************************************************************/
/*-------------------------- Include Files
-----------------------------*/
#include
"IRSensor.h"
/*-------------------------- Module Cals
-----------------------------*/
const static unsigned int
Black_Range = 380; //Analog
reading of lowest value of black tape
const static unsigned int
High_Period_Value = 20000; //Upper range of valid period
const static unsigned int
Low_Period_Value = 18000; //Lower
range
/*-------------------------- Module Vars
-----------------------------*/
static unsigned int
uPeriod; //Logs the period between IR pulses
static unsigned int
FlagTime; //Helps
timeout interrupt
static unsigned int
TapeReads[10];
static unsigned int
timeLastTapeRead = 0;
static unsigned int
TapeIndex = 0;
/*-------------------------- Module Code
-----------------------------*/
//Use Timer1 writing to channel 4 set to PT4 E128 for IR sensor
void initIRSensing(void) {
//input
capture
DDRT &=
BIT4LO;
TIM1_TSCR1 =
_S12_TEN; //Turn on
timer system to allow IC
TIM1_TSCR2 =
TIM1_TSCR2 & ~(_S12_PR2 | _S12_PR1 | _S12_PR0); //pre-scale /1 = 24.0MHz
/* Set up IC4
to capture falling edges of PT4*/
TIM1_TCTL3 |=
_S12_EDG4B; //falling
edge
TIM1_TFLG1 =
_S12_C4F; //Clear IC4 flag
TIM1_TIE |= _S12_C4I; //Enable IC4
interrupt
EnableInterrupts; //Globally
enable interrupts
MyTimer_Init();
//Make
sure 1 ms timer is running
timeLastTapeRead = MyTimer_GetTime();
}
//Interrupt Response Routine is called when TFLG1bit4 is set
//meaning interrupt of rising edge has occurred
void interrupt
_Vec_tim1ch4 IRSensorICRoutine(void){
static unsigned int lastEdge;
PTT ^= BIT7HI;
//For scope debugging
FlagTime =
MyTimer_GetTime(); //Get FlagTime
uPeriod = TIM1_TC4 - lastEdge; //TC4 = clock tick count in an IR pulse
lastEdge = TIM1_TC4;
TIM1_TFLG1 =
_S12_C4F; //Clear
IC4 flag
}
//If IR signal uPeriod is in valid
range, we are aligned with Beacon
char isAlignedWithBeacon(void){
if
((uPeriod
< High_Period_Value) && (uPeriod > Low_Period_Value)){
return TRUE;
}
return
FALSE;
}
void checkIRSensing(void){
unsigned int myTime;
myTime = MyTimer_GetTime();
if ((myTime - FlagTime) > 50) {
//It's
been too long, an edge should have occured, timeout
period measurement
uPeriod =
0;
}
}