FTM Counter setting

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

FTM Counter setting

Jump to solution
1,991 Views
seprol
Contributor II

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?


0 Kudos
1 Solution
1,181 Views
seprol
Contributor II

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 !!!

View solution in original post

0 Kudos
3 Replies
1,181 Views
mjbcswitzerland
Specialist V

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

 

0 Kudos
1,181 Views
seprol
Contributor II

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.

0 Kudos
1,182 Views
seprol
Contributor II

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 !!!

0 Kudos