overriding Arduino interrupt handlers

The Arduino code defines several interrupt handlers, such as the Timer0 overflow interrupt for instance.

If I want to define my own version of one of these handlers (e.g., I'm using Timer0 in my own way), overriding the predefined versions, how do I do that?

Is it enough to define the interrupt service routine (ISR) in my sketch? Or is more involved? Is it even possible?

I'd really appreciate any help you could offer. Thanks.

(Sorry. I put this in the wrong category by mistake. I meant to put it in Software. If a moderator could reclassify it, I'd appreciate it.)

Timer0 is used for the millis timer and the delay function. These are fundamental functions in the arduino environment and breaking these by modifying timer0 would make your code arduino unfriendly.

Timer2 is a much better choice, its similar to timer0 and you can define your own handler for this without conflict.

You may want to look at using the MStimer2 library to handle the interrupts for you.

Mem, I’m clear on that, thanks. The issue is a more subtle one.

The underlying issue is that I am using all three timers for different purposes. Timer 2 to generate fast PMW, Timer 1 for a pattern matching state machine (and I need the full word range for that one), and Timer 0 for basic timing using delay() and delayMicroseconds().
The Timer2 PWM only works at my target frequencies on Pin 11. (I can explain why this is, but for now take my word for it – Pin 3 is out.)
This is all working.

The problem is that I also need to use SPI which also uses Pin 11. If I could swap Timer 0 and Timer 2, all would be solved as I could do the PWM on Pin 5, and write a mimic of delay, millis, etc. for Timer 2 which is pretty straightforward.

To do this I can either rewrite wiring.c which is easy but involves changing the core, or I can write my own delay etc. and use Timer 0 as desired. But I’d like to be able to use the interrupt handlers I now use for Timer 2 for Timer 0 instead, which means I’d like to be able to override the default ISR’s in wiring.c. Hence, my query.

So, bottom line. I need to swap Timer 2 and Timer 0. I’m out of pins and timers otherwise.

I also don’t see the big deal about Arduino unfriendly code. (Several people have had this response.) It’s for this one particular project, loaded on one chip, and it does what it should. Yes, I’m more or less using this as an AVR with some nice Arduino help, but that’s fine I think. I’d be interested to hear the counterarguments.

Thanks again. I am very eager to hear your thoughts in response.

I think I would go with changing wiring.c to swap the timers and put this modifed core in a new core subdirectory, for example called: arduinoHacked

You can create a new entry in boards.txt for the chip you use that points to this core. For example:

#############################################
atmega328Hacked.name=Arduino w/ ATmega328 using hacked timers

atmega328Hacked.build.core=arduinoHacked
. . . other entries here

that way you can build a standard or hacked version depending on your board selection in the IDE.

Mem, thanks for the help. That's just what I need. Fantastic!