Pages: [1]   Go Down
Author Topic: MsTimer2 with microseconds!  (Read 531 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have a project where I want to an interupt to run every 250uS. I need to use timer 2, however the only library I could find, MsTimer2 has a resolution of 1ms. How do I change the prescaler to lower the resolution? I have no idea where to start.

Also, I can't use timer 1 because I have tone() running on a pin, and it appears if I also use TimerOne it screws up my tone.

Thanks!
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 7
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I found this really helpful: http://www.avrbeginners.net/architecture/timers/timers.html

MsTimer2 is a great convenience, but for custom intervals, my advice is just figure out the timer control values in the data sheet.
Logged


0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 360
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I find it rather strange that nobody ever suggests FlexiTimer2, which is MsTimer2 with a parameter for the interval.

Code:
FlexiTimer2::set(1, 1.0/4, isr);

will set it to once every quarter-second.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12425
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

FlixeTimer looks quite interesting, but

Quote
I want to an interupt to run every 250uS.
Quote
will set it to once every quarter-second.

4x per second  != (4000 x per second)

Can flexitimer do 4000/sec ?

I had a quick look at the FlexiTimer page and saw it used doubles as param.  That implies bigger footprint (float math) and it raises some questions about accuracy and precission. Does there exist a 100% integer version of this lib ?

Rob

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 360
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry, I misread (but it works at anything you can set a timer to, 250us included).

FlexiTimer2 uses double precision only in the set command (see the source to check) - it uses the hardware Timer2 to actually do the timing.  However, there may be rounding issues, since the hardware timer is set with integer parameters.  A purely integer version of the library would be quite impractical - the whole point is an intuitive interface, otherwise directly programming the registers is much preferred. 

Of course, if you can afford about 1% (or maybe even less) inaccuracy, these issues are essentially moot.

Code:
FlexiTimer2::set(1, 1.0/4000, isr);
Logged

Pages: [1]   Go Up
Jump to: