I am trying to used the FTM timer to get the pulse width of a signal which is 10us long.
But I getting overflow in the counter value. Attaching my implementation, so that someone can tell me what mistake I have been making.
/* The Flextimer instance/channel used for board */
#define DEMO_FTM_BASEADDR FTM1
/* FTM channel used for the capture */
#define BOARD_FTM_INPUT_CAPTURE_CHANNEL kFTM_Chnl_1
/* Interrupt to enable and flag to read; */
#define FTM_CHANNEL_INTERRUPT_ENABLE kFTM_Chnl1InterruptEnable
#define FTM_CHANNEL_FLAG kFTM_Chnl1InterruptEnable
static volatile u32 FTM_Timercountervalue = 0u;
static volatile s32 pulseWidth = 0u;
void TIMER_ConfigureFTM1_v(void)
{
FTM_SetWriteProtection(FTM1, false);
CLOCK_EnableClock(kCLOCK_Ftm1);
FTM_SetupInputCapture(DEMO_FTM_BASEADDR, BOARD_FTM_INPUT_CAPTURE_CHANNEL, kFTM_RiseAndFallEdge, 0);
FTM1->CNT = 0;
FTM0->SC |= FTM_SC_PS(1U);// system clock, divide by 1
FTM_EnableInterrupts(FTM1, kFTM_TimeOverflowInterruptEnable);
EnableIRQ(FTM1_IRQn);
NVIC_SetPriority(FTM1_IRQn, 2);
InstallIRQHandler(FTM1_IRQn, (u32)(&HALUSER_TIMER_FTM1TimeOverflow_vi));
}
static void HALUSER_TIMER_FTM1TimeOverflow_vi(void)
{
pulseWidth = ((u32)(FTM1->CONTROLS[BOARD_FTM_INPUT_CAPTURE_CHANNEL].CnV) - (u32)FTM_Timercountervalue ) ;
FTM_Timercountervalue = (u32) FTM1->CONTROLS[BOARD_FTM_INPUT_CAPTURE_CHANNEL].CnV;
FTM1->CONTROLS[BOARD_FTM_INPUT_CAPTURE_CHANNEL].CnSC &= ~FTM_CnSC_CHF_MASK;
FTM_ClearStatusFlags(FTM1, kFTM_TimeOverflowFlag);
NVIC_ClearPendingIRQ(FTM1_IRQn);
}
解決済! 解決策の投稿を見る。
The Dual Edge Capture mode allows to measure a pulse width or period of the signal on the input of channel (n) of a channel pair. I think Dual Edge Capture mode is better for measuring pulse width than Input Capture mode.
Please download KV10P48M75RM. Check "35.4.24.1 One-Shot Capture mode" or "35.4.24.2 Continuous Capture mode" which is more suitable for you. you can see that it is not necessary to reset the FTM counter every time
From "Figure 35-247. Dual Edge Capture – One-Shot mode for positive polarity pulse width
measurement" and "Figure 35-248. Dual Edge Capture – Continuous mode for positive polarity pulse width measurement" you can see that it is not necessary to clear the FTM counter every time.
Hi
If you don't want the FTM timer to overflow frequently, you can change the FlexTimer pre-scaler factor selection for the clock source so that its period is greater than 10us.
FTM_GetDefaultConfig select the default prescale = kFTM_Prescale_Divide_1;
I think you can refer to the frdmkv11z_ftm_dual_edge_capture example in MCUXpresso SDK. It will show you how to handle FTM timer overflow when dual edge capture is used to measure pulse width.
Best Regards,
Robin
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------