pjc30943 wrote:I'm curious though why this did not work:
Register.SREG = Register.SREG Or INT_MASK
It's not that it didn't work - just that it is unnecessary (since interrupts are enabled by default). Moreover, if you want to unconditionally enable interrupts use this:
More often, though, you'll be enabling interrupts after having disabled them. In that case, however, you want to conditionally re-enable interrupts like this:
Code: Select all
Dim sreg as Byte
sreg = DisableInt()
[ do some stuff here that shouldn't be interrupted ]
Call EnableInt(sreg)
The beauty of this code is that interrupts are enabled only if they were initially enabled. This is important if this code in is a subroutine that is called from some code where interrupts have already been disabled and should remain so.
The real culprit in your original code was the
reti that you inserted manually. With that present, the resulting AVR assembly language code for the ISR appeared as shown below, annotated with the original source code. The prologue and epilogue code is added automatically by the compiler to ensure that the integrity of the CPU registers is maintained across the execution of the ISR. The
reti that you added manually caused the ISR to return prematurely, leaving 15 bytes on the stack and leaving registers modified. Moreover, the two bytes used for the return address were actually the values that should have been restored to r30 and r31. That is undoubtedly the proximate cause of the "resetting" behavior that you observed. In the unlikely case that the incorrectly used return address just happened to be the correct value, the stack imbalance would eventually cause a stack overflow, again leading to the "resetting" behavior.
Code: Select all
'ISR INT0()
' prologue code
push r1
push r0
in r0, 0x3f
push r0
eor r1, r1
push r18
push r19
push r20
push r21
push r22
push r23
push r24
push r25
push r26
push r27
push r30
push r31
'Call PutPin(loopTogglePin, zxOutputToggle)
ldi r22, 0x04
ldi r24, 0x07
call putPin
'#asm
' reti
'#endasm
reti <--- this is the culprit
' epilogue code
pop r31
pop r30
pop r27
pop r26
pop r25
pop r24
pop r23
pop r22
pop r21
pop r20
pop r19
pop r18
pop r0
out 0x3f, r0
pop r0
pop r1
reti
'End ISR