PDA

View Full Version : Where to place DISABLE/ENABLE INTERRUPT



Heckler
- 17th February 2016, 03:55
Hey Group,

I am writing some code that depends on interrupts for keeping time.
I am a bit confused in understanding where and how often I need to place the ENABLE and DISABLE INTERRUPT directive.

So if I have, say, several subroutines in my code and want to have INTERRUPTS ENABLED in each and all of them (or most of them) then do I need to place an ENABLE / DISABLE before and after each of the subroutines even if they are placed in my code IDE one after another? Or do I just need to ENABLE the whole block of subroutines? and then place a DISABLE at the end of the block?

for example...


ENABLE INTERRUPT

Sub1:
do some stuff
RETURN
'===========
Sub2:
do some other stuff
RETURN

DISABLE INTERRUPT


or do I need to do this...


ENABLE INTERRUPT

Sub1:
do some stuff
RETURN

DISABLE INTERRUPT
'===========
ENABLE INTERRUPT

Sub2:
do some other stuff
RETURN

DISABLE INTERRUPT


thanks in advance

HenrikOlsson
- 17th February 2016, 09:06
Hi,
The ENABLE/DISABLE INTERRUPT are commands or directives for the actual compiler, they're not commands that gets exectuted when the PIC executes your code so it doesn't matter if there's a RETURN before the DISABLE INTERRUPT. Both of your example will produce the same result and the same code, there's no difference as far as I'm aware.

/Henrik.

Heckler
- 19th February 2016, 23:09
Thanks Henrik,

I knew that ENABLE/DISABLE were compiler directives but the PBP manual wasn't clear to me on whether each block of code needed the directive or if you could group several unrelated subroutines and then place the directive just once at the beginning and end of the block.

Which leads me to ask is it necessary to have them in matched pairs?? I'm guessing not.
That if you have a block of subroutines at the end of your code you could just place an ENABLE at the beginning of the block and assume that all the following code will have interrupts enabled.

I guess it's time for me to dig into Darrel's INSTANT INTERRUPTS as I am seeing jitter in the timing of my interrupt service routine. I believe it is due to the interrupt not happening until the current statement finishes.

I toggle a PortPin in the ISR and look at it on an o-scope. Based on my settings for TIMER1 it should INT every 5 msecs but instead of the the square wave being fairly solid it wanders slightly.

here is the code for my ISR handler...


'************************************************* **
' interrupt service routine
' H & L size determine INT timing
' INT should happen every .005 seconds
'************************************************* **
disable interrupt
MyInt:
IntFlag=1 ' INT has fired so set this flag for Main code usage
TMR1H = $FB ' reset timer1h
TMR1L = $27 ' reset timer1l

toggle PortA.0 ' used for determining INT timing
PIR1.0 = 0 ' clear timer1 overflow flag
resume 'to Main
enable interrupt
'================================================= ============

HenrikOlsson
- 21st February 2016, 08:37
Hi Dwight,
If you don't want to DISABLE the interrupt then you don't insert the DISABLE directive hence they does not need to be in matched pairs. You place the ENABLE directive prior to any code where you want the interrupt flag to be polled between statements and you place the DISABLE before any block of code where you don't want the flag to be polled. Since it's not a runtime command it doesn't matter if it's subroutines or whatnot.

You won't regret Learning how to use DT-INTS. It's more complicated than ON INTERRUPT but much more powerful.

/Henrik.