is Kinetis's timer Consistent ?

cancel
Showing results for 
Search instead for 
Did you mean: 

is Kinetis's timer Consistent ?

Jump to solution
571 Views
Amit_Kumar1
Senior Contributor II

Hi

I am using K60 ,100Mhz uc with CW10.5 and PE. I wanted some exact timer in my project. so I used the PE component TimerInt but I am no getting the exact value. Is there anything wrong in my code? I wanted timer to interrupt at around 800 Hz or more. but it is not consistent, I tried for lesser values also the attached program is for 400Hz with the o/p. Most stable values I was getting at 100 Hz higher than that frequency, the timer values was inconsistent. I don't think that 100MHz uc is supposed to work like this(at least not for such a small frequency) but I feel there is something wrong in the code. kindly have a look and help me to resolve this issue.  In the attached documents o/p values are captured at 100Hz and 400Hz, I have marked the occurrence of inconsistent values in both.

 

Kind Regards

Amit Kumar

Original Attachment has been moved to: Output_400Hz.ods

Original Attachment has been moved to: output_100Hz.ods

Original Attachment has been moved to: Timer_K60.zip

1 Solution
264 Views
mjbcswitzerland
Specialist V

Amit

I think that your measurement method is inaccurate and also has design faults in it.

void RTC1_OnSecond(LDD_TUserData *UserDataPtr)

{

  /* Write your code here ... */

    printf("loop = %d\n", loop_counter);

    loop_counter = 0;

}

You look to be using the RTC seconds interrupt to print out the number of timer interrupts that took place in the last second. Depending on how your RTC clock is derived they may not be synchronised and also may drift form another.

Furthermore PE printf() tends to be a blocking function which will only return when all output characters have been sent. You reset the loop counter in the 1s interrupt after you have sent the value - if the other timer interrupt has higher priority it may still be counting when the printf() output is being sent and so resetting it here may be losing an incermenet (that is - the loop variable may be at 100 (or 400) when used as output value but already be at 101 (or 401) when it is reset (thus losing a count value which will be seen in teh next measurement). If your timers may not be synchroinised I don't think that you can be sure of obtaining an accuracy of better that +0/-1.

I would simply toggle an output in the timer interrupt routine and measure its value with an oscilloscope. This will probably show that it is highly accurate but your output code introduces the errors.

- certainly avoid using printf() in interrupts (especially the PE derived one if it blocks)

- when changing variables in more than 1 interrupt routine be aware of the interrupt routine priorities (can one interrupt the other and cause variable value corruption?)

Regards

Mark

View solution in original post

4 Replies
264 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Amit Kumar,

I agree with Mark's method, please kindly refer to my result as below, it is based on TWR-K53N512, and the timer interrrupt toggles PTA11(Pin 1 of J4) to generate a 800Hz wave form.

tek00008.png

I also attached my project here. Hope that helps,


Have a great day,
B.R
Kan

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

264 Views
Amit_Kumar1
Senior Contributor II

Hi Kan_Li

Thanks for the code and the waveform. The issue was of printf and clock sync as mentioned by Mark.

Kind Regards

Amit Kumar

0 Kudos
265 Views
mjbcswitzerland
Specialist V

Amit

I think that your measurement method is inaccurate and also has design faults in it.

void RTC1_OnSecond(LDD_TUserData *UserDataPtr)

{

  /* Write your code here ... */

    printf("loop = %d\n", loop_counter);

    loop_counter = 0;

}

You look to be using the RTC seconds interrupt to print out the number of timer interrupts that took place in the last second. Depending on how your RTC clock is derived they may not be synchronised and also may drift form another.

Furthermore PE printf() tends to be a blocking function which will only return when all output characters have been sent. You reset the loop counter in the 1s interrupt after you have sent the value - if the other timer interrupt has higher priority it may still be counting when the printf() output is being sent and so resetting it here may be losing an incermenet (that is - the loop variable may be at 100 (or 400) when used as output value but already be at 101 (or 401) when it is reset (thus losing a count value which will be seen in teh next measurement). If your timers may not be synchroinised I don't think that you can be sure of obtaining an accuracy of better that +0/-1.

I would simply toggle an output in the timer interrupt routine and measure its value with an oscilloscope. This will probably show that it is highly accurate but your output code introduces the errors.

- certainly avoid using printf() in interrupts (especially the PE derived one if it blocks)

- when changing variables in more than 1 interrupt routine be aware of the interrupt routine priorities (can one interrupt the other and cause variable value corruption?)

Regards

Mark

View solution in original post

264 Views
Amit_Kumar1
Senior Contributor II

Hi Mark

Thanks for the reply.

Regards

Amit Kumar

0 Kudos