I want to calculate frequency using FTM in MK02FN128VFM10. Plz check if I am on right path:

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

I want to calculate frequency using FTM in MK02FN128VFM10. Plz check if I am on right path:

424 Views
snehalpatil
Contributor II

#define TIMER_CLOCK 72000000

#define PRESCALER 1

uint16_t channel0_val=0;

uint16_t channel1_val=0;

float frequency;

void ftm_init()

{

ftm_dual_edge_capture_param_t param;

ftm_user_config_t user_info;

FTM_Type *ftmBase = g_ftmBase[BOARD_FTM_INSTANCE];

FTM_HAL_ClearChnEventFlag(ftmBase, BOARD_FTM_X_CHANNEL);  //should be set zero initially

FTM_HAL_ClearChnEventFlag(ftmBase, (BOARD_FTM_X_CHANNEL+1)); //should be set zero initially

user_info.tofFrequency=0; /*!< Select ratio between number of overflows to times TOF is set @internal gui name="Overflow frequency" id="OvFrequency" */

user_info.BDMMode= kFtmBdmMode_11; /*!< Select FTM behavior in BDM mode @internal gui name="BDM mode" id="BdmMode" */

user_info.isWriteProtection=false; /*!< true: enable write protection, false: write protection is disabled @internal gui name="Write protection" id="WriteProtection" */ user_info.syncMethod=kFtmUseSoftwareTrig;

FTM_DRV_Init(BOARD_FTM_INSTANCE, &user_info);   

FTM_DRV_SetClock(BOARD_FTM_INSTANCE, kClock_source_FTM_SystemClk, kFtmDividedBy1);

param.mode=kFtmContinuous;

param.currChanEdgeMode=kFtmRisingEdge;

param.nextChanEdgeMode=kFtmRisingEdge;

FTM_DRV_SetupChnDualEdgeCapture(BOARD_FTM_INSTANCE, &param,BOARD_FTM_X_CHANNEL,0);

}

void ftm_channel_data()

{

FTM_Type *ftmBase = g_ftmBase[BOARD_FTM_INSTANCE]; 

if ((FTM_HAL_HasChnEventOccurred(ftmBase,BOARD_FTM_X_CHANNEL)== 1)&&(FTM_HAL_HasChnEventOccurred(ftmBase,(BOARD_FTM_X_CHANNEL+1))== 1))     

channel0_val=FTM_HAL_GetChnCountVal(ftmBase,BOARD_FTM_X_CHANNEL); //check this 

channel1_val=FTM_HAL_GetChnCountVal(ftmBase,(BOARD_FTM_X_CHANNEL+1)); //check this 

FTM_HAL_ClearChnEventFlag(ftmBase, BOARD_FTM_X_CHANNEL); 

FTM_HAL_ClearChnEventFlag(ftmBase, (BOARD_FTM_X_CHANNEL+1));   

}

}

void frequency_calculation()

{

uint16_t capture=0; 

if (channel1_val>channel0_val) 

{

capture=channel1_val - channel0_val; 

else 

{

capture=65535 - channel1_val + channel0_val; 

frequency=(float)(TIMER_CLOCK / (PRESCALER+1)) / capture;

}

Labels (1)
0 Kudos
1 Reply

312 Views
EarlOrlando
Senior Contributor II

Hello Snehal Patil,

The FTM's Dual Edge Capture Mode which is described in the Reference Manual is used to measure the period of a signal when the two channels are configured with the same edge. Since this is your configuration's case I think that conceptually you are in the right direction.

Also, your configurations seems to be Ok. Could you please confirm that you are getting the desired results after read the Channel's value (CnV)? Is the measured period correct?

Best regards,

Earl.

0 Kudos