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.