Go Down

Topic: MsTimer2 with microseconds! (Read 1 time) previous topic - next topic



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.



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.


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

Code: [Select]

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

will set it to once every quarter-second.


FlixeTimer looks quite interesting, but

I want to an interupt to run every 250uS.

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 Tillaart

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


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: [Select]

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

Go Up

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

shortcuts: alt+s submit/post or alt+p preview