NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Support Forum » C function that returns time?
January 06, 2013 by Eirias42 |
Hi. Newbie here. Is there any c function that will return the time (like elapsed time since the program started or something similar to that?) Thanks! |
---|---|
January 06, 2013 by Noter |
Not in the world of micro-controllers. You'd have to write your own function or find one someone else wrote. |
January 06, 2013 by Eirias42 |
OK.... Anybody know of one someone else wrote that's in the public domain? It's presumably possible, there's a millis() function on arduino that seems to do pretty much that... thanks in advance for any help! |
January 07, 2013 by JimFrederickson |
Hopefully this will give you a little bit of an understanding about timers.
The above code doesn't particularly do it, but you can tweak the code so as to create and second/minute/hour/day time source accurate to a couple of seconds per week. (Depending on how stable your power source it, how consistent the temperature is, and how prone to vibration your wiring is. Maybe some other variables as well, but for consistency those for me were the primary ones.) Where "OCR0A = 155" it is a little different than the remark's would indicate, but that is because this build was using a non-standard Crystal. Basically that is creating an Interrupt about 100 times per second. NOTE: The variables must have "VOLATILE" as an attribute. What that attribute does is to make sure than in your code whenever any of those variables are accessed their values are read from memory and not from a register that the compiler had optimized. That is NECESSARY because both foreground and background/interrupt tasks are accessing those values. NOTE: It would be best, at least to start, to make sure that the values are ONLY changed from the interrupt routine. you can use that as a basis to create any timers that you may need/want. Try it, play with it, change it to suite your needs. |
January 07, 2013 by Noter |
Using the internal timer of the ATmega is one way to do it, another is to add a real time clock chip like the DS3232 and query it whenever you want the time. If you reset it to 00:00 when the program starts then the time you read would be the elapsed time. Check Rick's thread on the DS3232 RTC where he shows how to use it and provides code samples. |
January 07, 2013 by Ralphxyz |
Then of course there is always the Nerdkits Real Time Clock project if you want good documented help. Ralph |
Please log in to post a reply.
Did you know that the printf format string "%.3f" will show three digits after the decimal point? Learn more...
|