Need code for Input Capture. Please help if anybody has written it.
Hi xiangjun,
Thank you for quick reply. It was very helpful, but in current project we are using kinetis SDK API. I want to know which API's to use and in what order for input capture.
Hi, Aniket
Of course, the SDK2.0 include the capture function, I copy it here which is included in fsl_ftm.c and fsl_ftm.h loctaed at:
C:\Freescale\SDK2.0_FRDM_K22F\devices\MK22F51212\drivers
You can follow up the procedure:
1)enable FTM clock with the function CLOCK_EnableClock(kCLOCK_Ftm0); //assume you use FTM0 and FTM0_CH0 as input pin.
2)set up pin assigmnet, FTM0_CH0 is multiplexed with PTA0 for 121 pin mapbga package
CLOCK_EnableClock(kCLOCK_PortA);
PORT_SetPinMux(PORTB, 0U, kPORT_MuxAlt4);
3)initializing FTM0 module:
ftm_config_t ftmInfo;
FTM_GetDefaultConfig(&ftmInfo);
FTM_Init(FTM0, &ftmInfo);
FTM_EnableInterrupts(FTM0, kFTM_Chnl0InterruptEnable);
NVIC_EnableIRQ(FTM0_IRQn);
FTM_SetupInputCapture(FTM0,0,kFTM_RisingEdge,0);
4)In the ISR of FTM0, ckecking the FTM_GetStatusFlags(FTM0), if the kFTM_Chnl0Flag is set, you can read the FTM0_C0V register.
Note I do not test the above code, pls have a try.
Hope it can help you
BR
XiangJun Rong
void FTM_SetupInputCapture(FTM_Type *base,
ftm_chnl_t chnlNumber,
ftm_input_capture_edge_t captureMode,
uint32_t filterValue)
{
uint32_t reg;
reg = base->CONTROLS[chnlNumber].CnSC;
reg &= ~(FTM_CnSC_MSA_MASK | FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
reg |= captureMode;
/* Set the requested input capture mode */
base->CONTROLS[chnlNumber].CnSC = reg;
/* Input filter available only for channels 0, 1, 2, 3 */
if (chnlNumber < kFTM_Chnl_4)
{
reg = base->FILTER;
reg &= ~(FTM_FILTER_CH0FVAL_MASK << (FTM_FILTER_CH1FVAL_SHIFT * chnlNumber));
reg |= (filterValue << (FTM_FILTER_CH1FVAL_SHIFT * chnlNumber));
base->FILTER = reg;
}
#if defined(FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT) && (FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT)
/* Set to input mode */
FTM_SetPwmOutputEnable(base, chnlNumber, false);
#endif
}
Hi, Aniket,
From hardware perspective, you can connected the tested low frequency signal to the FTM_CHn, it is okay, Once the edge of tested signal is detected, an interrupt is triggered, the FTM_CNT is loaded into the FTM_CnV reg automatically. In the capture ISR, you can read the FTM_CnV register.
I attach a capture example based on KE02, hope it can help you. In your application code, you can add the code to select the FTM_CHn pin.
BR
Xiangjun Rong