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

Post Reply
User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Reset interrupt flag

Post by Jan Lichtenbelt »

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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Reset interrupt flag

Post by Benj »

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.

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

Re: Reset interrupt flag

Post by Jan Lichtenbelt »

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

Post Reply