QTR-1RC with AVR xMega Input Capture

I’m trying to interface a QTR-1RC with an AVR xMega32A4. I have the input connected to PC5 and am following the advice from the product page and from the Atmel Timer Counter Application note AVR1306. I set the pin to an output and drive it high. wait 20us for the cap to charge, then configure the pin for input capture, change the direction to input and try to read the the input capture register. I can see very nice results on the scope that match the ones shown in the product page and that change properly depending on the reflectivity of the surface. The line charges nicely to 3.3V, then decays. but my input capture fails for some reason. I get stuck waiting for the Interrupt Capture Flag to be set. Is it missing the falling edge? Can anyone see anything in my code that might cause this?
Thanks for the help,


//Take a reading on QTR Sensor
PORTC.DIR |= (1 << PIN5); //set middle sensor as output
PORTC.OUT |= (1 << PIN5); //drive high to charge capacitor
_delay_us(20); //wait for cap to charge
PORTC.PIN5CTRL = PORT_ISC_FALLING_gc; //set input to trigger on falling edge
EVSYS_CH0MUX = EVSYS_CHMUX_PORTC_PIN5_gc; //Set PC5 as multiplex input for Event Ch0
//Configure TCC1 for Input capture
//Enable Input Capture Channel B
//Activate TC by setting clock

PORTC.DIR &= ~(1 << PIN5); //change pin to input

do {
//wait for input capture
} while (!(TCC1.INTFLAGS & TC1_CCBIF_bm));
IR.middle = TCC1.CCB;


You are making this unnecessarily complicated. I suggest you either (a) get rid of the interrupt code for now and just poll the pin state in your main loop or (b) use something other than the QTR sensor to test your input capture code to make sure it is working (e.g. a switch or even just enable the internal pull-up resistor on the pin and use a wire to touch it to ground).

By the way, when you switch PC5 to an input, you are leaving the internal pull-up enabled. You probably want to just have it be a floating input.

- Ben

Thanks Ben,

I will try both of those. About the input though, how do i leave it floating? Do I change just set the PORTC.PIN5CTRL byte to 0?


You would clear the appropriate PORTC.OUT bit:

PORTC.OUT &= ~(1 << PIN5);

Make sure you do this after you set the pin to an input, though, or else it will briefly drive low!

- Ben