4

I'm writing a library which needs an ISR to turn off an LED some time after it was turned on. Since it's all about turning an LED on and off it doesn't need to be very precise. On the other hand I would like to use this library in a program where timer1 and timer2 are given out to other (more important) tasks, and I also need the millis() and micros() functions. Last, I'm using an ATmega328P microcontroller, which only has 3 timers (and I can't easily replace it with another one).


So I was wondering if I could attach an ISR to timer0 without affecting the above Arduino functions, and – if that can be done – what would be the restrictions of such an ISR (e.g. I suppose I could not use all the timer/interrupt modes...) and the disadvantages or side effects in using this timer in a non-Arduino library and the Arduino millis() and micros() functions.

Thanks in advance for any answer!

noearchimede
  • 482
  • 2
  • 7
  • 19

4 Answers4

9

So I was wondering if I could attach an ISR to timer0 without affecting the above Arduino functions,

Yes. A few ways, depending on your level of comfort:

  1. You can declare the stock Arduino Timer0 OVF "weak" and write your own where you can insert your ISR. But you have to handle the interaction between the millis() / micros() related variables.

  2. You can modify the stock Arduino Timer0 OVF to insert your own ISR.

    And – if that can be done – what would be the restrictions of such an ISR (e.g. I suppose I could not use all the timer/interrupt modes...)

    The stock ISR would look like this:

    ISR(TIM0_OVF_vect) {
        ...
        timer0_fract = f;
        timer0_millis = m;
        timer0_overflow_count++;
    
        //insert your isr here
        _mytmr0_isrptr();
    }
    

    Where _mytmr0_isrptr() is a function pointer to your own ISR handler.

  3. The simplest is probably to use the output compare interrupts. You can get it to behave like a virtual timer and can do lots of other stuff.

Obviously, you can think of tons of other ways and this is just a starting point.

What are those interrupts? How can I use them? –

Timer0 has three interrupts associated with it: overflow and compare channel A and channel B. The overflow interrupt is already being used by the timing functions millis() and micros(), as shown earlier.

The compare channel A/B interrupts are unused. And this discussion is about using them for timing purposes.

The general ideal is demonstrated in blog post Using Spare Output Compare Channels as Timers – the Collection.

The task there is more complicated than what you are trying to do here, but the basic ideal is the same. To do what you wanted here it is much simpler than that.

If you still cannot figure it out, I can provide you with code pieces to get you started.

Peter Mortensen
  • 435
  • 3
  • 12
dannyf
  • 2,813
  • 11
  • 13
3

I think the first solution suggested by dannyf's answer is wrong as ISR in AVR GCC can't be defined as weak function (why).

I think the best solution is to add handler (AKA hook) functions using function pointer to the Timer0 ISR inside the core wiring.c source file shipped with Arduino core, and assign latter (attache) this function pointer to any desired function in the other library/class or in the main application code.

More about this trick in this article here.

Yahya Tawil
  • 141
  • 3
1

An interrupt can only have one interrupt service routine. The only way you can do something else using an interrupt that is already handled elsewhere would be to modify that existing interrupt handler to also call your code. Not something that is practical for a library.

I can see no practical way of doing it short of modifying the entire millis() system in the main API to allow hooking of callback functions, which would be a really nice facility (chipKIT has a similar system with the Core Timer interrupt on the PIC32) and submitting it back to Arduino for inclusion in the main core - which would set a baseline version for compatibility of your library of course.

Majenko
  • 105,851
  • 5
  • 82
  • 139
1

So I was wondering if I could attach an ISR to timer0 without affecting the above Arduino functions ...

No, because those functions use an already-attached interrupt.

it doesn't need to be very precise ...

Just test for the current time (eg. using millis or micros) in your main loop.

the program main loop is sometimes quite long (e.g. sometimes it has to execute other loops) ...

Restructure so that it doesn't do that. You can use the state machine concept to do things from time to time, without getting stuck in inner loops for a long time.

Nick Gammon
  • 38,901
  • 13
  • 69
  • 125