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.
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.
nickgammon:
The external interrupts are not enabled by default.
Nick you are not right
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();