Hi,
I am using one of the K60 Flex Timer (FTM) modules to act as
a simple pulse counter of the external clock FTM_CLKIN1
input. I want it to roll over at 0xffff back to 0x0000. However
occasionally I wish to write a specific value to the CNT register
and I dont seem to be able to make this work. The only setup
for the FTM I do is:
FTM1->SC = FTM_SC_CLKS(3) | FTM_SC_PS(0) |
FTM_SC_TOIE_MASK;
This sets external clock, Divide 1 and enable overflow
interrupt.
To write a value to the clock I use:
FTM1->SC &= ~FTM_SC_CLKS_MASK; // Stop
FTM1->CNTIN = lo_word; // value to write
FTM1->CNT = lo_word; // Trigger write CNTIN to CNT
FTM1->CNTIN = 0; // Reset for roll over
FTM1->SC = FTM_SC_CLKS(3) | FTM_SC_PS(0) |
FTM_SC_TOIE_MASK; // restart
However all this does is clear the counter to 0 and not the
value I set. What am I doing wrong?
Solved! Go to Solution.
OK I think I have found the problem. It would appear that you cannot write a value greater than the MOD register. As I left this at 0 then that is what was being written. The answer is to write the value to MOD before the update and then reset it afterwards. The only problem with this is that it appears you cannot write a value of 0xffff !!!
Hi
I suspect that your problem is to do with clock-domain timing.
The technique looks to be essentially correct since it is possible to set the CNT value by writing the CNTIN register and transferring it to the CNT value with a 'dummy' write (also with the timer clock stopped ).
If the timer module is being clocked from a different clock than the CPU it may be that the sequence
CNTIN = lo_word;
CNT = lo_word;
has not completed when the CPU sets CNTIN to 0 again. This would mean that the load trigger would start but the CNTIN has been changed again before it propogates to the latch cycle in the timer block.
Try doing something like
CNTIN = lo_word;
do {
CNT = lo_word;
} while (CNT != low_word);
CNTIN = 0;
Regards
Mark
Thanks Mark. Sensible suggestion however that does not seem to work either. The condition for the while loop never becomes true so I guess it is still not writing the value to the counter.
OK I think I have found the problem. It would appear that you cannot write a value greater than the MOD register. As I left this at 0 then that is what was being written. The answer is to write the value to MOD before the update and then reset it afterwards. The only problem with this is that it appears you cannot write a value of 0xffff !!!