I have a 1 Hz square wave connected to INT1, free-running so it is asynchronous WRT the µC. Using the following sketch, the interrupt appears to fire as soon as it's enabled; I consistently see 999 as the first number output. Then the time to the second interrupt varies, and is less than one second. I was expecting the opposite, i.e. the first number should be some random value less than one second, and then the rest should be one second apart.
I've been poring over the datasheet and I can't find anything that would indicate this behaviour, hoping someone can help. I've checked the interrupt flag (INTF1 in EIFR) to ensure it's not set going in, and it's not, have tried resetting it anyway, no joy, etc. etc.
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
#define LED LED_BUILTIN
volatile boolean int1Flag;
boolean ledState;
void setup(void)
{
delay(1000);
Serial.begin(115200);
pinMode(LED, OUTPUT);
EICRA = _BV(ISC11); //external interrupt 1 on falling edge
EIMSK = _BV(INT1); //enable external interrupt 1
}
void loop(void)
{
if (int1Flag) {
int1Flag = false;
digitalWrite(LED, ledState = !ledState);
Serial << millis() << endl;
}
}
ISR(INT1_vect)
{
int1Flag = true;
}
Typical output that I see:
999
1652
2652
3652
4652
--------
999
1056
2056
3056
4056
-------
999
1841
2840
3841
4840
I would have expected:
652
1652
2652
3652
4652
--------
56
1056
2056
3056
4056
--------
841
1841
2840
3841
4840