Reset interrupt flag

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

User avatar
Jan Lichtenbelt
Posts: 785
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 122 times
Been thanked: 259 times
Contact:

Reset interrupt flag

Postby Jan Lichtenbelt » Thu Nov 12, 2015 1:46 pm

Why is the interrupt flag reset after the function call, instead of before the call? The earlier the flag is reset, the better repeated interrupts are prevented.

An example is given below.
AFTER:
if (ts_bit(intcon, IOCIF) && ts_bit(intcon, IOCIE))
{
FCM_A();
mxtmp=portb;
cr_bit(intcon, IOCIF);
iocbf=0;
}

BEFORE
if (ts_bit(intcon, IOCIF) && ts_bit(intcon, IOCIE))
{
cr_bit(intcon, IOCIF);
FCM_A();
mxtmp=portb;
iocbf=0;
}

With kind regards

Jan Lichtenbelt

User avatar
Benj
Matrix Staff
Posts: 14151
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4080 times
Contact:

Re: Reset interrupt flag

Postby Benj » Thu Nov 12, 2015 4:07 pm

Hello Jan,

Clearing the flag before the function call opens the possibility of an interrupt firing again whilst you are inside the function call which would likely cause stack corruption and other problems such as RAM corruption.

Clearing the flag after the function ensures that the code in the function has been processed correctly before we allow the interrupt to fire again.

Whilst repeated interrupts could be annoying they are generally easier to spot and deal with than stack corruption.

This is why we also recommend that any heavy processing be left outside of an interrupt macro, including things like delays etc.
These users thanked the author Benj for the post:
Jan Lichtenbelt (Thu Nov 12, 2015 4:22 pm)
Rating: 5.56%
 

User avatar
Jan Lichtenbelt
Posts: 785
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 122 times
Been thanked: 259 times
Contact:

Re: Reset interrupt flag

Postby Jan Lichtenbelt » Thu Nov 12, 2015 8:36 pm

How do I program a dead-end interrupt. Say on a high temperature the program must stop (after resetting some outputs). But jitter in the temperature may not cause additional interrupt(s). My suggestion would be to disable the interrupt in the interrupt macro. Or is there a better way?

(May be this is a Flowcode 6 subject?)

With kind regards

Jan Lichtenbelt