Go Down

Topic: timers and internal interrupts (Read 760 times) previous topic - next topic

dado

hello! I'm trying to use Arduino as a DAQ. I've finally managed to reach a good speed, but now I have a new problem, that is, having a regular sampling step; I need 1KHz; I've tried measurig time passed from one iteration to another, and sampling only if the number of millisecond passed since the program starts has changed; but this method proved faulty, since every 41st-42nd ms it loses 1 ms; plus the professor doesn't like the idea.
My first idea was using an iternal timer, which would fire once every millisecond, and have an interrupt to handle this, acquiring and sending the necessary data. but I really can't find a timer function! can anybody help me?

Magician

There is an example of sampling sub-function, based on TimerOne library: http://coolarduino.wordpress.com/2012/06/22/audio-input-to-arduino/ After that 3 projects follow, which implement this sub-function, 15.6 ksps, 10 ksps, and double channel - stereo sampling.

PaulS

Quote
but I really can't find a timer function!

There is a TimerOne library.

You could avoid the millis() jumping issue by using micros(). millis() jumps periodically because the clock ticks 1024 times then millis increments by 1. After 41 to 42 iterations, the extra 24 ticks add up to a whole millisecond.

dado

Quote
There is a TimerOne library.

oops! why is it that sometimes I look for something which is right in front of my eyes and I don't find it! thank you. only one thing, I guess it's the same, but, now that I look with more attention, I find a MsTimer2 library; where is TimerOne? is there a difference?
Quote
You could avoid the millis() jumping issue by using micros(). millis() jumps periodically because the clock ticks 1024 times then millis increments by 1. After 41 to 42 iterations, the extra 24 ticks add up to a whole millisecond.

this is really weird, 1 ms every 41-42 is quite a big error! so you say micros() is precise?

PaulS

Quote
this is really weird

Given something that ticks 1024 times per microsecond, how would YOU report accurately the time to the nearest whole millisecond?

There is nothing weird about it. It is a fact of life that you need to be aware of.

Quote
where is TimerOne?

http://www.arduino.cc/playground/Code/Timer1

Quote
is there a difference?

Read for yourself:
http://arduino.cc/playground/Main/MsTimer2

dado

well, it's just that over 10 seconds it yields a 0.2 seconds error. say that to a runner! thank you for your help, now I'll try to see how it goes

PaulS

Quote
well, it's just that over 10 seconds it yields a 0.2 seconds error.

No. The error is not cumulative. The value returned by millis() is accurate to within 1000/1024 milliseconds. The discrepancy will never exceed 24 microseconds.

Go Up