PaulS:
Unless you disable interrupts (not a good idea), how do you limit where interrupts can occur?
The example above does not try to catch all interrupts during its while-loop. But if it should do it, I would propose this (the whole sketch is here for easy copy/paste/execute):
void setup() {
Serial.begin(115200);
Serial.println(F("Setup started"));
TIMSK2 &= ~(1<<TOIE2);
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
ASSR &= ~(1<<AS2);
TIMSK2 &= ~(1<<OCIE2A);
bitWrite(TCCR2B, CS20, 1); // counting 16 Mhz == no prescaler
bitWrite(TCCR2B, CS21, 0);
bitWrite(TCCR2B, CS22, 0);
TIMSK2 |= (1<<TOIE2);
Serial.println(F("timer2 is counting"));
delay(1000); // if you remove this, you get a serial comm interrupt below
byte c1, c2, d;
noInterrupts();
while (1) {
interrupts(); // 1 cycle. in effect after next instruction
c1 = TCNT2;// 2 cycles. this is still executed before any interrupt
// an interrupt can occur here
noInterrupts(); // 1 cycle. this is in effect immediately
c2 = TCNT2; // 2 cycles
d = c2 - c1; // d gets normally value 3 (cycles)
if ((d > 3) && (d != 28)) break; // d ==3, when no interrupt,
// and d == 28, when timer2 interrupt (ISR is below)
}
interrupts();
Serial.print(F("There was an interrupt lasting x cycles, x = "));
Serial.println(d - 3, DEC);
Serial.println(F(" The interrupt was caused by ...?"));
}
void loop(){}
ISR(TIMER2_OVF_vect){ // takes 25 = 28 - 3 cycles
}
For me it was instructional to find from the avr datasheet and elsewhere:
-
interrupts() takes only 1 cycle, and it will be in effect after NEXT machine instruction.
-
noInterrupts() is in effect immediately even if there would be an interrupt request during its own execution.
-
when returning from an interrupt one machine instruction is executed even in the case that another interrupt has occurred earlier and is pending.
One could modify the above program to execute in a loop and store all different interrupt execution times into an array. By examining the assembly code we find that almost all the interrupt vector entries are filled with "bad_request" (remarks about interrupt at the right are my additions):
0: 0c 94 6e 00 jmp 0xdc ; 0xdc <__ctors_end> reset
4: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
8: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
c: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
10: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
14: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
18: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
1c: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
20: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
24: 0c 94 99 00 jmp 0x132 ; 0x132 <__vector_9> timer2 overflow
28: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
2c: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
30: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
34: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
38: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
3c: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
40: 0c 94 10 01 jmp 0x220 ; 0x220 <__vector_16> timer0 overlow
44: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
48: 0c 94 ed 01 jmp 0x3da ; 0x3da <__vector_18> usart receive
4c: 0c 94 3b 02 jmp 0x476 ; 0x476 <__vector_19> usart udre
50: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
54: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
58: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
5c: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
60: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
64: 0c 94 96 00 jmp 0x12c ; 0x12c <__bad_interrupt>
I added a loop to record interrupt lengths in machine cycles and their counts during 10 seconds. Here are the results without timer2:
Duration Count
cycles pcs
67 1
83 38
98 9532
102 234
98 and 102 cycle interrupts are probably from timer0 overflow, but what would be an easy "automatic" way to find out 83 and 67...
PS. Something strange with timer0? Sum of interrupts should(?) be 10 000 and it is not. 10000-(9532+234) = 234 !