5

I have a need for timer interrupt (for the sake of this exercise - say I need to execute something every second)

I found code in many places on the internets that does this using ISR for 328/Uno. (or specifically for other chips) But of course that does not work on other chipsets.

Is there a portable timer library that works across many/most/all arduino boards/chipsets?

For now it is a simple project - I have no other motors or tones, etc.

In my current project I have conditional compiles for different chipsets, to set up a callback/interrupt but would rather use a library that hides this all.

  • 1
    My guess would be no. You are asking for a library that interfaces with interrupts and they are usually quite processor specific. You would need a layer that interfaced to the user code and abstracted the hardware away into a replaceable module. – Code Gorilla Sep 29 '16 at 12:01
  • 1
    @matt, Right, I am wondering if someone wrote a wrapper - so that everyone who wants a timer doesn't have to write their own. Just as I have three different conditionally compiled timer implementations - that could be wrapped in a library. I will keep looking. But what you say makes sense - since the lib writer couldn't know what other things the timers might be used for in the client code. I guess it's not really going to happen for that reason – Tim Sep 29 '16 at 14:50
1

I think it would be hard, or impossible, to make such a library. Already there are problems with pin-change interrupt libraries, and Software Serial (which uses pin-change interrupts).

Plus, to have a generic "callback" might tempt people to do things which they shouldn't anyway in an ISR, like do serial printing.

Let's say you choose to use a spare timer (eg. Timer 1). You have the issue now that you won't be able to do (some) PWM output because that uses Timer 1.

The closest would be to lever off the existing millis() code and just have a test in loop to see if the time interval is up, and then call a function - however that wouldn't be an ISR.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.