Input Capture using FTM

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

Input Capture using FTM

1,203 Views
jmvalero
Contributor I

Dear Sir/Madame,

We are programming a board when a MCU MK20DX256VLL10 is included and connected to a driver motor. We would like to send PWM (edge aligned PWM) in a channel to the motor and know the frequency of the motor (0- 2kHz) in the other channel, so we use the FTM component but the result of the frequency is not correct and the PWM sends well (the motor moves). The code are shown a below that we think it should work (the clock source is 20.972MHz and the prescaler is 32, Auto initialization is enabled):

void flexTimer2_IRQHandler(void)
{
  //FTM_DRV_IRQHandler(flexTimer2_IDX);
  /* Write your code here ... */

  if(FTM_HAL_HasTimerOverflowed(g_ftmBase[flexTimer2_IDX]) == 0x01)
      {
      FTM_HAL_ClearTimerOverflow(g_ftmBase[flexTimer2_IDX]);

      }

      if(FTM_HAL_HasChnEventOccurred(g_ftmBase[flexTimer2_IDX],CHAN0_IDX) == 0x01)
        {

          /* clear channel flag */
          FTM_HAL_ClearChnEventFlag(g_ftmBase[flexTimer2_IDX],CHAN0_IDX);

        /* Record value of current capture */
          temp1 =  period_CH0_FTM2;
        capture_CH0_FTM2 = FTM_HAL_GetChnCountVal(g_ftmBase[flexTimer2_IDX],CHAN0_IDX);
          if(capture_CH0_FTM2 > temp1)
          {
            period_CH0_FTM2 = capture_CH0_FTM2 - temp1;
          }
          else
          {
            periodo_CH0_FTM2 = 65535 - captura_CH0_FTM2 + temp1;
          }
         // FTM_HAL_SetChnCountVal(g_ftmBase[flexTimer2_IDX],CHAN0_IDX,0);
          flaj1=1;

         }

}

int main(void)

  FTM_DRV_CounterStart(flexTimer2_IDX, kCounting_FTM_UP, 0x00, 0xFFFF, true);
  FTM_DRV_SetupChnInputCapture(flexTimer2_IDX,kFtmRisingEdge,CHAN0_IDX,0);

for(;;){

     if(flaj1==1)
     {
         flaj1=0;
         frequency_CH0_FTM2=20972000/(32*period_CH0_FTM2);

      }

}

We will appreciate if you help us to solve this situation.

Thanks you and Best regards,

José Manuel.

0 Kudos
3 Replies

1,076 Views
nxf63973
NXP Employee
NXP Employee

Hi José Manual

 

I want to confirm that you are working with the latest SDK version for the K20_100MHz. Could you please let me know which SDK version you are using?

 

To download the latest SDK for your MCU please check the link below.

MCUXpresso SDK Builder

 

Please tell me if you need any more information.

0 Kudos

1,076 Views
jmvalero
Contributor I

Dear Jose,

Thanks you for your reply, my SDK version is 1.3.0. Now I have seen the last version, that is 2.2.0, so we are going to including this version.

For now, we have used the PWM_LLD() and Capture_LLD() libraries and the results are correct, but we have a question: how could we configure PWM output and input capture with the same FTM? For example, the pin for PWM output is FTM0_CH4 and the pin for input capture is FTM0_CH3. Is that we have tried but it gives us error. Also we try configure PWM output by ProcessorExpert and input capture by registers (it is extraed by "3.8.Single-edge capture mode" of the document AN5142 "Features of the FlexTimer Module") . I understand it happens because the FTM counter is only used by one pin.

Best regards,

José Manuel.

0 Kudos

1,076 Views
nxf63973
NXP Employee
NXP Employee

Hi José Manuel

 

The channels of a FlexTimer can be independent between them. So, it is possible to have different configurations for each channel at the same time. There is more detailed information about this module available in Chapter 38: FlexTimer Module (FTM) of the K20 Sub-Family Reference Manual.

 

It may be possible that some of the files generated by Processor Expert are not compatible with the latest MCUXpresso SDK.

Could you please check the MCUXpresso Config Tools? These tools help to generate the software to set up the pins, clocks, and peripherals of your MCU. 

 

Also, the MCUXpresso Config Tools is available as a download for use with supported IDEs. Could you please let me know which IDE are you working with? 

 

Please let me know if you need any more information.

0 Kudos