ISR usage guidelines

I had a problem using TIMER2 interrupts, because tone library already
occupies it. Even when I do not include that library. I fixed it by
modifying Tone.cpp code with the weak attribute. Now your code
can reuse the interrupt vector. Of course tone() is now unavailable.
This would be a good practice for all base library ISR vectors.

ISR(TIMER2_COMPA_vect) attribute ((weak));

You can do this by editing and recompiling your project. Arduino IDE
seems to take care of recompilation of the modified tone.cpp
/Applications/Arduino/Contents/Resources/Java/hardware/cores/arduino/Tone.cpp

I compiled this sketch fine:

ISR(TIMER2_COMPA_vect) {
    PIND = 0xf0;
}
void setup() {}
void loop() {}

on both 1.0.1 and the newest github version (commit 222d51e3831d77f60ae6ce1f3d037b0febb0c717) with avr-gcc 4.7.1 and avr-libc 1.8.0

What "problems" are you having?

Not the OP, but I have also found I cannot redefine ISRs in my sketches, I assumed this was normal, are you suggesting that we should expect to be able to override INT0 etc ?

Duane B

rcarduino.blogspot.com

Even with the avr-gcc that gets downloaded with the source (version 4.3.2), this still compiles on my computer:

void setup() {}
void loop() {}
ISR(ADC_vect)               {PIND = 0x00;}
ISR(ANALOG_COMP_vect)       {PIND = 0x01;}
ISR(EE_READY_vect)          {PIND = 0x02;}
ISR(INT0_vect)              {PIND = 0x03;}
ISR(INT1_vect)              {PIND = 0x04;}
ISR(PCINT0_vect)            {PIND = 0x05;}
ISR(PCINT1_vect)            {PIND = 0x06;}
ISR(PCINT2_vect)            {PIND = 0x07;}
ISR(SPI_STC_vect)           {PIND = 0x08;}
ISR(SPM_READY_vect)         {PIND = 0x09;}
ISR(TIMER0_COMPB_vect)      {PIND = 0x0a;}
//ISR(TIMER0_OVF_vect)        {PIND = 0x0b;}
ISR(TIMER1_CAPT_vect)       {PIND = 0x0c;}
ISR(TIMER1_COMPA_vect)      {PIND = 0x0d;}
ISR(TIMER1_COMPB_vect)      {PIND = 0x0e;}
ISR(TIMER1_OVF_vect)        {PIND = 0x0f;}
ISR(TIMER2_OVF_vect)        {PIND = 0x10;}
ISR(TWI_vect)               {PIND = 0x11;}
//ISR(USART_RX_vect)          {PIND = 0x12;}
ISR(USART_TX_vect)          {PIND = 0x13;}
//ISR(USART_UDRE_vect)        {PIND = 0x14;}
ISR(WDT_vect)               {PIND = 0x15;}

The two USART ones didn't work with 4.3.2 but they did with 4.7.1, and the TIMER0_OVF one didn't work on either of them.

It would definitely make sense to make the functions weak. I tried it and it made the above sketch compile with everything uncommented. I think it should be a goal of arduino to be able to use any program that don't use arduino libraries in the IDE, and that means being able to redefine the ISRs.

Interestingly, that worked for me as well, I wonder if it was because I was defining the ISR in a cpp file rather than the sketch file ?

Duane B

I had problems in compilation using TIMER0_OVF and the following code and was going crazy as it worked ok using the code below:
void setup() {
bitSet(DDRB, 5); // Pin 13 as output
TCCR0A = 0; TCCR0B = 1<<CS02 ; bitSet(TIMSK0, TOIE0);
}
void loop() { }
ISR(TIMER0_OVF_vect) {
bitSet(PINB, 5); // toggle pin 13
}
The error was C:\arduino-1.0\hardware\arduino\cores\arduino/wiring.c:49: multiple definition of `__vector_16'

However, the next lines compile (and run) ok
void setup() {
bitSet(DDRB, 5); // Pin 13 as output
TCCR1A = 0; TCCR1B = 1<<CS12 ; bitSet(TIMSK1, TOIE1);
}
void loop() { }
ISR(TIMER1_OVF_vect) {
bitSet(PINB, 5); // toggle pin 13
}

Now I've seen the comment two posts above ( and the TIMER0_OVF one didn't work on either of them )and felt some relief. Thanks a lot wizenedEE