FlexiTimer2

I propose FlexiTimer 2 to be added to http://arduino.cc/en/Reference/Libraries, in the “Timing” section.

What is it?[/b]
An Arduino library to use timer 2 with a configurable resolution. Based on MsTimer2 by Javier Valencia.
Where is the code?[/b]
http://github.com/wimleers/flexitimer2
Why is this useful?[/b]
Both Metro and MsTimer2 (which are already listed in the “Timing” section of http://arduino.cc/en/Reference/Libraries) limit the end user to a millisecond resolution. Suppose you need an interrupt service routine to fire 2000 times per second. You just can’t with those libraries. You’re forced to write code at a lower level, or write a library that makes it easy, like I did here.
My use case was the need to drive an 8x8 RGB led matrix that uses multiplexing, which therefore requires 8 leds (1 row active at a time) x 3 colors = 24 pins to manipulate in PWM mode. I’m doing this using an Arduino Mega, which only has 14 PWM pins. Hence I needed to simulate PWM in software, thus I had to use interrupts. I needed 8 rows x 6 intervals x 60 refreshes per second = 2880 interrupts per second.
With FlexiTimer2, that’s easy:
**__</strong> <strong><strong>**FlexiTimer2::set(1, 1.0/2880, callback); FlexiTimer2::start();**</strong></strong> <strong>__**
Which is just one extra parameter compared to MsTimer2:
**__</strong> <strong><strong>**MsTimer2::set(1, callback); MsTimer2::start();**</strong></strong> <strong>__**
In fact, this even works and defaults to MsTimer’s behavior (i.e. the default value of the second parameter is 0.001, i.e. a resolution of 1000 interrupts per second, or 1ms):
**__</strong> <strong><strong>**FlexiTimer2::set(1, callback); FlexiTimer2::start();**</strong></strong> <strong>__**
For lack of inspiration, I called it FlexiTimer2. IMVHO, it could easily replace MsTimer2 if FlexiTimer2’s documentation is improved somewhat.
It was written for the project associated with the “Mobile & Pervasive Computing” course at Hasselt University in Belgium.
I gladly welcome your feedback :slight_smile:

1.0/2880

Does it use floating-point variables / math?

Does it use floating-point variables / math?

Only once, just like MsTimer2:

void FlexiTimer2::set(unsigned long units, double resolution, void (*f)()) {
?
      tcnt2 = 256 - (int)((float)F_CPU * resolution / prescaler);
?
}

FlexiTimer2::set(1, 1.0/2880, callback);

(see FlexiTimer2.cpp)

Is there anything particular I must/can do to move this forward? :slight_smile:

Would you consider adding it to the Playground? You can create an account there and add it yourself. That would move it forward pretty quickly. :slight_smile:

Arduino Playground - HomePage would seem to be an appropriate place for it.

Can you create a webpage for the library (the playground would be fine) with some simple documentation (along the lines of your forum post)? Linking straight to a github page seems a bit unfriendly.

Playground page with documentation added: Arduino Playground - HomePage.

Added FlexiTimer2 to Arduino Playground - HomePage, just after MsTimer2. I felt this was more accurate/apt. Feel free to change that if that was a wrong assumption.

The GitHub wiki page now redirects to the Playground page: http://wiki.github.com/wimleers/flexitimer2/

Looking forward to the next round of feedback, and thanks TBAr and mellis for the feedback so far! :slight_smile:

Just for future reference, what is the highest resolution (smallest time interval) FlexiTimer2 can achive on a Mega @ 16MHz.

DJ :slight_smile: