attachInterrupt will it be fast ?


To handle some kind of events it might be useful to consider handling them inside of an interrupt procedure.

But be aware: no matter how short your ISR might be there is a long overhead slowing down the performance.

Just in between the PUSHs and the POPs there is the ICALL to your ISR.

IMPLEMENT_ISR(INT0_vect, EXTERNAL_INT_0)
 256:	1f 92       	push	r1
 258:	0f 92       	push	r0
 25a:	0f b6       	in	r0, 0x3f	; 63
 25c:	0f 92       	push	r0
 25e:	11 24       	eor	r1, r1
 260:	2f 93       	push	r18
 262:	3f 93       	push	r19
 264:	4f 93       	push	r20
 266:	5f 93       	push	r21
 268:	6f 93       	push	r22
 26a:	7f 93       	push	r23
 26c:	8f 93       	push	r24
 26e:	9f 93       	push	r25
 270:	af 93       	push	r26
 272:	bf 93       	push	r27
 274:	ef 93       	push	r30
 276:	ff 93       	push	r31
 278:	e0 91 04 01 	lds	r30, 0x0104
 27c:	f0 91 05 01 	lds	r31, 0x0105
 280:	09 95       	icall
 282:	ff 91       	pop	r31
 284:	ef 91       	pop	r30
 286:	bf 91       	pop	r27
 288:	af 91       	pop	r26
 28a:	9f 91       	pop	r25
 28c:	8f 91       	pop	r24
 28e:	7f 91       	pop	r23
 290:	6f 91       	pop	r22
 292:	5f 91       	pop	r21
 294:	4f 91       	pop	r20
 296:	3f 91       	pop	r19
 298:	2f 91       	pop	r18
 29a:	0f 90       	pop	r0
 29c:	0f be       	out	0x3f, r0	; 63
 29e:	0f 90       	pop	r0
 2a0:	1f 90       	pop	r1
 2a2:	18 95       	reti

This code alone will take about 2.5 μs to execute.

So consider to get acquainted with ISR(INT0_vect).

In setup just write:

  EICRA = B00001010; // configure both for falling
  EIMSK = B00000011; // mask for INT1 and INT0

and get the job done in

ISR(INT0_vect) {
  ...
}

and

ISR(INT1_vect) {
  ...
}



There is still an advantage of the Arduino function attachInterrupt: you can detach it and attach it to a different function at run-time if you want to. This cannot be done with the AVR ISR as these vectors are stored in the FLASH memory.


contact: nji(at)gmx.de