Pages: [1]   Go Down
Author Topic: timers and internal interrupts  (Read 630 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 27
Posts: 2563
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: