08-29-2012 10:15 AM
I am trying to utilze the tick count function as a counter that resets every 1 second. Is there a way to break it down into just tenth's of seconds?
Basically, can I break the string down into just the last 2 or 3 digits?
I have tried the elapsed time function but it isn't as fluid as I am looking for.
08-29-2012 10:33 AM - edited 08-29-2012 10:35 AM
Use the Quotient & Remainder. Use the remainder of the tick count divided by 1000 to get the milliseconds.
08-29-2012 10:49 AM - edited 08-29-2012 11:21 AM
@ajsp wrote:
I am trying to utilze the tick count function as a counter that resets every 1 second. Is there a way to break it down into just tenth's of seconds?
Basically, can I break the string down into just the last 2 or 3 digits?
I have tried the elapsed time function but it isn't as fluid as I am looking for.
You talk about tick count, counter, string, and elapsed time, and there is very little in common between these. Obviously you are not telling us the entire story. Tick count gives you relative ms, which is a u32 that rolls over when it reaches the top.
The number of displayed digits is a function of the display format. Are you displaying in seconds or milliseconds? Can you show us some code? What is your definition of "fluid"?
Tick **bleep** cannot really be used as a "counter", because it is a passive function. What else is in your code? What determines the polling interval?
08-29-2012 10:53 AM
@crossrulz wrote:
Use the Quotient & Remainder. Use the remainder of the tick count divided by 1000 to get the milliseconds.
Since the range of u32 is not a multiple of 1000, this code will have short intervals occasionally. Not good!
08-29-2012 10:57 AM
@altenbach wrote:
Since the range of u32 is not a multiple of 1000, this code will have short intervals occasionally. Not good!
True. I was hoping the OP would be subtracting the tick counts before performing this Remainder logic. I guess I could have made my example a little better. I was just trying to show off the math though.
08-29-2012 11:29 AM
The quotient & Remainder worked like a charm. At the end of the day I am trying to get a running 1 second count that I can use for a % of a second that a digital ouput is on.
Attached is a set of code similar to what I am doing, but I am doing it in a much larger program.
08-29-2012 11:37 AM - edited 08-29-2012 11:40 AM
You are still incorrect, because the current code will have a 295ms period once in a while. The range of U32 is 4294967295, which is not a multiple of 1000. Whenever you are rolling over, you will be short.
Here's what you could do instead.
08-29-2012 12:00 PM - edited 08-29-2012 12:01 PM
@altenbach wrote:
You are still incorrect, because the current code will have a 295ms period once in a while. The range of U32 is 4294967295, which is not a multiple of 1000. Whenever you are rolling over, you will be short.
Here's what you could do instead.
Sorry, but I'm not seeing how that simple of code fixes the issue. You will still have the error, you are just moving at what point the error happens. The clearest way to see it is if the initial tick count was 0. TC-0=TC. Right back to your rollover issue.
I think the only way to fix it is to keep a TC in a shift register. Subtract the saved TC from the current TC. If there's a "reset" (went over 1 second), then store the new TC in the shift register.
NOTE: Yes, this is a greedy loop. Please add waits as necessary.
08-29-2012 02:26 PM
@crossrulz wrote:
Sorry, but I'm not seeing how that simple of code fixes the issue. You will still have the error, you are just moving at what point the error happens. The clearest way to see it is if the initial tick count was 0. TC-0=TC. Right back to your rollover issue.
True. However, the subtraction guarantees that the rollover problem does not occur for the first ~50 days or so. WIthout the subtraction, it could occur at the first iteration. 🙂
There is probably a better solution to this entire problem.
08-29-2012 02:57 PM - edited 08-29-2012 02:58 PM
How about using the High resolution relative seconds (I agree it is limited depending on the system). I just gave a try please comment if improvements required.