Hello @Iotelctronic ,
Thanks for your post.
From the ConfigTool screenshot, it appears that you configured the FTM in edge‑aligned mode and set the timer output frequency to 200 Hz. This means you have effectively enabled the timer period function, causing the counter to wrap around at the MOD value rather than at 0xFFFF.
I recommend trying the following two approaches:
1. Configure the counter as a true free‑running timer to avoid periodic wrap‑around
This allows CNT to increment continuously from 0x0000 to 0xFFFF.
Disable the timer-related settings in ConfigTool and add the following initialization code:
FTM1->SC = 0;
FTM1->MODE |= FTM_MODE_FTMEN_MASK;
FTM1->CNTIN = 0;
FTM1->MOD = 0xFFFF;
FTM1->CNT = 0;
FTM1->SC = FTM_SC_CLKS(1) | FTM_SC_PS(4); // CLKS=01 System clock, PS=100 /16
2. Keep the current ConfigTool configuration (200 Hz), but correct the wrap‑around logic in the ISR
In this case, the counter wraps at the configured MOD value rather than at 0xFFFF, so the wrap‑around calculation should use FTM1->MOD.
Updated ISR example:
void interruption_kbi(void)
{
uint16_t now = FTM1->CNT;
uint16_t mod = FTM1->MOD;
uint16_t dt;
if (now >= last_time)
dt = now - last_time;
else
dt = (mod - last_time) + now + 1;
last_time = now;
timerarr[countim++] = dt;
KBI_ClearInterruptFlag(KBI0);
}
Hope it helps.
BR
Celeste