Setup Input Capture

	//----- SETUP TIMER 3 -----
	//Used for: Input capture
	OpenTimer3((T3_ON | T3_IDLE_CON | T3_GATE_OFF | T3_PS_1_1 | T3_SOURCE_INT), 0xffff);
	
	//-------------------------------
	//----- SETUP INPUT CAPTURE -----
	//-------------------------------
	//Parameter 1:
	//IC_EVERY_FALL_EDGE			Capture timer value on every falling edge of input applied at the ICx pin
	//IC_EVERY_RISE_EDGE 			Capture timer value on every rising edge of input applied at the ICx pin
	//IC_EVERY_4_RISE_EDGE 			Capture timer value on every fourth rising edge of input applied at the ICx pin
	//IC_EVERY_16_RISE_EDGE 		Capture timer value on every sixteenth rising edge of input applied at the ICx pin
	//IC_EVERY_EDGE					Capture timer value on every rising and falling edge of input applied at the ICx pin
	//IC_SP_EVERY_EDGE				Capture timer value on the specified edge and every edge thereafter
	//
	//Parameter 2:
	//IC_INT_1CAPTURE 			//Interrupt on first Capture 
	//IC_INT_2CAPTURE 			//Interrupt on second Capture 
	//IC_INT_3CAPTURE 			//Interrupt on third Capture 
	//IC_INT_4CAPTURE 			//Interrupt on fourth Capture 
	//
	//Parameter 3:
	//IC_TIMER2_SRC 				//Timer2 is the clock source for Capture 
	//IC_TIMER3_SRC 				//Timer 3 is the clock source for Capture 
	//
	//Parameter 4:
	//IC_FEDGE_FALL 				//Capture Falling Edge First 
	//IC_FEDGE_RISE 				//Capture Rising Edge First 
    OpenCapture1( IC_EVERY_FALL_EDGE | IC_INT_1CAPTURE | IC_TIMER3_SRC | IC_FEDGE_RISE | IC_ON );
	OpenCapture2( IC_EVERY_FALL_EDGE | IC_INT_1CAPTURE | IC_TIMER3_SRC | IC_FEDGE_RISE | IC_ON );
	OpenCapture3( IC_EVERY_FALL_EDGE | IC_INT_1CAPTURE | IC_TIMER3_SRC | IC_FEDGE_RISE | IC_ON );
	OpenCapture4( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER3_SRC | IC_FEDGE_RISE | IC_ON );
	
	//FIF0
	//Each Input Capture has a four-level deep FIFO buffer
	//If the FIFO is full with four capture events and a fifth capture event occurs prior to a read of the FIFO, ICOV bit (ICxCON<4>) is set/
	//The fifth capture event is not recorded, subsequent capture events do not alter the current FIFO contents until the overrun condition is
	//cleared, and an input capture error interrupt will be generated.
Reading the Input Capture

	//GET INPUT CAPTURE
	if (mICxCaptureReady())
	{
		//if (ICxCONbits.ICOV)
		//{
		//	//INPUT CAPTURE HAS OVERRUN
		//	//You don't have to worry about this if you don't care about every capture as reading the 4 FIFO entries will clear it and start it storing
		//	//again - it just stops storing while the FIFO is full).
		//	//If you care then disable the input capture and then re-enable it again
		//}
				
		while(mICxCaptureReady())			//Each Input Capture has a four-level deep FIFO buffer
		{
			int CaptureTime = mICxReadCapture();
		}
	}

 

 

 

UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.