FTM TIMER to get the pulse width of a signal

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FTM TIMER to get the pulse width of a signal

Jump to solution
683 Views
mitun_embedded
Contributor II

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);


}
Tags (3)
0 Kudos
1 Solution
598 Views
mitun_embedded
Contributor II
According to NXP in this application note : https://www.nxp.com/docs/en/application-note/AN5303.pdf, chapter 3.5. Single-edge capture mode "If the selected capture mode is sensitive either on the rising edge
(ELSnB:ELSnA= 0:1) or on the falling edge (ELSnB:ELSnA= 1:0), the difference is equal to the signal
period. If the selected capture mode is sensitive on both edges (ELSnB:ELSnA = 1:1), the difference is
equal to the pulse width of the tested signal." Which is also what I did and it is working.
Using Dual Edge Capture mode is not optimal because there I would have to use two channels. In Singe Edge capture mode, by using rising and falling edge interrupts, I am able to measure the pulse width of the signal. The thing to pay attention is to handle the overflow when doing the subtraction while measuring the pulse width

View solution in original post

0 Kudos
4 Replies
665 Views
mitun_embedded
Contributor II
  • I am using Single Edge capture mode to measure the pulse width. Do I need to use Dual Edge capture mode for measuring the pulse width ?
  • Do I need to clear the FTM counter everytime I get and interrupt on the rising or falling edge or this done automatically? Because I think, I am having the overflow, since the FTM counter is in free running mode and it is not being reset everytime I detect and edge.

 

0 Kudos
657 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

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.

0 Kudos
599 Views
mitun_embedded
Contributor II
According to NXP in this application note : https://www.nxp.com/docs/en/application-note/AN5303.pdf, chapter 3.5. Single-edge capture mode "If the selected capture mode is sensitive either on the rising edge
(ELSnB:ELSnA= 0:1) or on the falling edge (ELSnB:ELSnA= 1:0), the difference is equal to the signal
period. If the selected capture mode is sensitive on both edges (ELSnB:ELSnA = 1:1), the difference is
equal to the pulse width of the tested signal." Which is also what I did and it is working.
Using Dual Edge Capture mode is not optimal because there I would have to use two channels. In Singe Edge capture mode, by using rising and falling edge interrupts, I am able to measure the pulse width of the signal. The thing to pay attention is to handle the overflow when doing the subtraction while measuring the pulse width
0 Kudos
669 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

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;

FTM_GetDefaultConfig kFTM_Prescale_Divide_1.png

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.

ftm_dual_edge_capture overflow.png


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.
-------------------------------------------------------------------------------

0 Kudos