How do you set the FTMx counter in a KE02Z to a particular value?

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

How do you set the FTMx counter in a KE02Z to a particular value?

Jump to solution
1,334 Views
davetelling
Contributor I

I have a FDM-KE02Z20M board, using IAR Kickstart for ARM development tools.

I am using FTM2, CH0 to capture a rising edge event. I also want to reset the counter to a small value after processing the capture, so that the counter effectively restarts from zero. If, for example, the capture processing takes 5us, I want to set the counter to the count equivalent of 5us so that the time between capture events is correct.

According to the reference manual, writing ANY value to the CNT register sets the counter to the CNTIN register value. However, I do not see this occurring. I set the initial value like this:

FTM_SetCounterInitValue(FTM2, 20); and I can see, that, in fact, that value is in the CNTIN register if my breakpoint is reached in the capture function. However, FTM2->CNT is always zero after reaching this line in the code:

FTM2->CNT = 20;

Theoretically, it shouldn't make any difference what value I use, according to manual, "Writing any value to COUNT updates the counter with its initial value, CNTIN."

What might I be doing wrong here? CNTIN is set to 20.

Tags (2)
0 Kudos
1 Solution
1,055 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Dave Telling,

      Your configuration is wrong, you can't set CNTIN to 20, when your FTM2 work in input capture mode, please take care the note in page 401 from MKE02Z64M20SF0RM:

        The Input Capture mode must be used only with CNTIN = 0x0000.

        Then, when you :Writing any value to COUNT updates the counter with its initial value, CNTIN. This function works ok on my side.

        My test result is like following:

    1: Before write any value to FTM2_CNT,the  CNT is 0x3a.

pastedImage_0.png

2:  After work one step through: FTM2_CNT =20; the CNT value is 2, it is very near to 0,  this means your CNT is updates to the initial value , 2 is just when your reading, the CNT cout to 2 from 0 again.

pastedImage_11.png

Wish it helps you!

If you still have question, please contact me!


Have a great day,
Jingjing

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
8 Replies
1,056 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Dave Telling,

      Your configuration is wrong, you can't set CNTIN to 20, when your FTM2 work in input capture mode, please take care the note in page 401 from MKE02Z64M20SF0RM:

        The Input Capture mode must be used only with CNTIN = 0x0000.

        Then, when you :Writing any value to COUNT updates the counter with its initial value, CNTIN. This function works ok on my side.

        My test result is like following:

    1: Before write any value to FTM2_CNT,the  CNT is 0x3a.

pastedImage_0.png

2:  After work one step through: FTM2_CNT =20; the CNT value is 2, it is very near to 0,  this means your CNT is updates to the initial value , 2 is just when your reading, the CNT cout to 2 from 0 again.

pastedImage_11.png

Wish it helps you!

If you still have question, please contact me!


Have a great day,
Jingjing

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,055 Views
davetelling
Contributor I

However, no matter what I do, even if I do not have the FTM2 set as input capture, if I set CNTIN to 20, whenever I write to the counter, it always goes to zero, NOT to CNTIN.

See this from debugger:

Kinetis_counter_not_set_1.jpg

Note: I commented out my normal initialization code so that all I am doing is setting up FTM2 counter clock and registers.

Any Freescale techs want to weigh in on this?

0 Kudos
1,055 Views
egoodii
Senior Contributor III

To avoid issues with interrupt-response time and thus WHEN you might want to 'pre-set' the FTM counter, I highly recommend you just KEEP the 'old value' from each capture, and use 16-bit unsigned subtraction from a 'new value' to get the counter-difference, and let the FTM counter free-run at all 16 bits.  I do this to accurately measure a sequence of sub-100uS pulse widths.

static uint16_t saved_time;

    uint16_t crnt_time,time_dif;

    (void)FSK_GetCaptureValue(&crnt_time);        //Time counts are in microseconds from 1MHz

    time_dif = crnt_time - saved_time;            // 16-bit end-around math is valid (upto 65.5ms, of course)

    saved_time = crnt_time;

1,055 Views
davetelling
Contributor I

Earl,

I have also used that technique on systems in the past, but in this case, because of the application requirements (I need to keep the counter prescale as low as possible, but also have to time long periods) I was hoping to not have to deal with counter overflows and subsequent overflow value management. By using the method i describe, counter overflows are only dealt with at very low repetition rates. If the flex timer had a real 32 bit counter (and I'm totally amazed that, on a 32-bit CPU core, it doesn't), much of this would be moot. I may end up back to that method again, but for now, I just add a "fudge factor" to the captured value, and reset the counter to zero.

I would, however, like to know why I cannot get the counter to accept the CNTIN value, no matter what I do.

0 Kudos
1,055 Views
egoodii
Senior Contributor III

Oh, I commiserate fully with your 'not 32bit-ness'!  I use FTM1&2 in quadrature mode, and I use encoders that run to 2million counts in any one direction (200mm linear encoders at 0.1 micron step size).  So I have to deal with over/underflow interrupts thru the sequence, but most especially I have an 'unavoidable hole' if there happens to be high-speed dither right at said over/under-flow -- the software 'timer extension' counts risk losing a whole transition.

0 Kudos
1,055 Views
davetelling
Contributor I

I know exactly what you mean!

0 Kudos
1,056 Views
davetelling
Contributor I

Jingjing,

You are correct - the manual does say this. I wish it said it earlier in the FTM section. So, there is not way I can do what I want - very disappointed in this restriction. Thanks for your time!

0 Kudos
1,056 Views
yasuhikokoumoto
Senior Contributor I

Hi Dave,


I think there would be two possible reason.


1) FTM clock was not supplied.
2) You read the value from a debugger.

Best regards,
Yasuhiko Koumoto.

0 Kudos