NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Get time between sensor readings
August 18, 2013 by dvdsnyd |
Hi all, Hope everyone is having a good weekend. I have been pondering something for quite some time, and that is - How do you go about getting the actual time between sensor readings? Or is it possible to write a GetTime() function? Where this function would have an init() macro that started the counter going, then I could call GetTime() and it would output the elapsed time in miliseconds. Any thoughts on this topic is greatly appreciated! Thanks, Dave |
---|---|
August 18, 2013 by esoderberg |
Dave, Here are a few code snippets that I'm using to time the delta between inputs from an ABS speed sensor. Set up an interupt on PB0:
This is the code that will execute on every change in PB0:
My code has a clock recording time (in .1 msec increments) in variable t. time setup:
// t will store the elapsed time // (10000 = 1 second)
|
August 18, 2013 by JimFrederickson |
Hello Dave, The quick answer is, of course it can be done. There were, are?, examples in the Library to illustrate this. However; the Library is Time, and understanding it's use, is an essential function of ANY real world There are very few Projects that I have where I do not have some sort of "Time Base". Pretty much all of the Basic NerdKit examples really either ignore time, or lead you in Like many things, to make some things easier other things have to become harder. One thing you need to really do is start looking at your Program, your Program Goals, in Let's look at on of the Nerdkit Examples of the "LED Blink"...
This is more of a "Step Oriented Approach"... While this approach works for SIMPLE situations, often times a little more complexity is I would recommend you adopt an approach of "Conditions, Events, Actions"... A key thing to understand is that the AVR MCU is ALWAYS doing something. Whether you The Program you write for the AVR MCU, which is always doing something, can ONLY DO 1 In order for you to "seemingly do multiple things at once" you need to schedule those While the "Delay_ms() Function" does create a delay of the specified amount of time, but it So while the AVR MCU is "busily executing code to create the delay" your Program is Let's stay with the "LED Blink example" for illustration purposes. Let's say we wanted to add second Blinking LED with an Interval of 1 second. Using the original "Step Process" you would come to:
There now you are blinking 2 Blinking LED's. Not really... What is happening is one Blinking LED does it's cycle, then the other Blinking LED does So there is a chain of Steps, but each one is deterministic on the other. What you really want is for each one to be "Time Deterministic"... A very simple, albeit not exact, way of making something "Time Deterministic" and I
So the example becomes:
That is a rudimentary method of having some form of a "Time Base". It also encourages you to think more in terms of "Conditions, Events, Actions"... I would really change it to, at the very least, this:
So now instead of the "Main Function" being just a series of steps it becomes more like If you have your LCD Hooked up and you adapt this program to count the tick ms's to In order to have something closer to an "Accurate Time Base" you have to use This is ONLY to give you ideas. Things to think about...
NOTE: While I do know that this does compile I do not have the means to "Test it" right Hopefully, it just works, but the concepts are mostly sound. They can be GREATLY expanded upon but just some things to think about... |
August 19, 2013 by Ralphxyz |
Thanks Eric and Jim, man that is what I love about this forum the detail of the help has always been so great. Ralph |
August 20, 2013 by dvdsnyd |
Eric and Jim, Thanks so much for your replies! I really appreciate the level of detail as well! I am sure I will have some questions once I start getting going on here. Thanks again! Dave |
Please log in to post a reply.
Did you know that interrupts can cause problems if you're not careful about timing and memory access? Learn more...
|