Interrupts by default

Hi, I'm making a project that includes rpm readings throug interrupt at pin 2 and another stuff so my questions are:

a) are the interrupts enabled by default?

b) If so, do I need to "detachInterrupt(0)" in the setup in order to not interfer with the code until it is necesary?

I have read some examples and all of them make "attatchInterrupt(isr,RISING)" in the setup, but I need to execute some code before the rpm reading so, I have read too, that interrupts can be harmful for other parts of the code.
Thank you in advance and sorry for my bad English.

See.see see this from Mr. Gammon:

a) are the interrupts enabled by default?

The external interrupts are not enabled by default.

As LarryD said:

The external interrupts are not enabled by default.

I thought it was just that there is nothing to be done, by default, when the interrupt occurs.

To make something happen, of course, you need to use attachInterrupt(), so whether the interrupt happens but gets ignored, or doesn't happen at all, before attachInterrupt() is used, doesn't really matter.

No, there is a flag which actually enables the external interrupt (two flags on the Atmega328P, in fact, one for each one). If that flag is clear, as it would be on a reset, there is no interrupt. And indeed for a small sketch there would be no interrupt handler either. The compiler generates a jump to the __bad_interrupt address, which itself has a jump to the reset vector. So, if you enable the interrupt flag and do nothing else, the processor will restart from address 0x0000 when that interrupt occurs.

Example sketch:

void setup () { }
void loop () { }

Generated code:

00000000 <__vectors>:
   0: 0c 94 34 00 jmp 0x68 ; 0x68 <__ctors_end>       <--- reset vector
   4: 0c 94 51 00 jmp 0xa2 ; 0xa2 <__bad_interrupt>   <--- INT0_vect
   8: 0c 94 51 00 jmp 0xa2 ; 0xa2 <__bad_interrupt>   <--- INT1_vect
   c: 0c 94 51 00 jmp 0xa2 ; 0xa2 <__bad_interrupt>
...
  3c: 0c 94 51 00 jmp 0xa2 ; 0xa2 <__bad_interrupt>  
  40: 0c 94 67 00 jmp 0xce ; 0xce <__vector_16>       <--- TIMER1_OVF_vect
  44: 0c 94 51 00 jmp 0xa2 ; 0xa2 <__bad_interrupt>
...
000000a2 <__bad_interrupt>:
  a2: 0c 94 00 00 jmp 0 ; 0x0 <__vectors>              <--- back to start

000000a6 <setup>:
  a6: 08 95       ret

000000a8 <loop>:
  a8: 08 95       ret

The code that would enable external interrupt 0 (on pin D2) could be:

EICRA |= bit (ISC01);    // set wanted flags (falling level interrupt)
EIMSK |= bit (INT0);     // enable it

Of course, such a sketch then needs the handler or it won't get far:

ISR (INT0_vect)
  {
  // handle the interrupt
  }

nickgammon:
The external interrupts are not enabled by default.

Nick you are not right :slight_smile:

EIMSK - just put External Interrupt Mask

BUT

Global interrupt flag is enabled by default

void setup()
{
EICRA |= 1;   // 0:LOW,1:CHANGE,2:FALLING,3:RISING for INT0  (0 will fire continuously while level low)
EIMSK |= 1;   // attachINT0
//here NO need in sei(); - which enable global interrupt flag
//interrupts will work
}

so interrupts are enabled by default in Arduino.
just need to apply interrupt mask (attach) if you want to work with it

PS: if you write code in pure C (w/o IDE) then need to enable global interrupts by sei();