Go Down

Topic: MsTimer2 with microseconds! (Read 3284 times) 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