-2

In microcontroller, how do the compiler distinguish between the interrupt function and any other function, as an example in Rb0 interrupt in the pic16f877, when the flag value changes it calls the interrupt function, why it didn't call any other function in the code.

  • 2
    Because usually the function which handles the interrupt is marked/tagged as such. How this is done is platform dependent. – DukeOfMarmalade Mar 11 '19 at 13:03
  • I am not familiar with the Rb0 interrupt Bakr, but I just had a quick search, and it seems like the 'interrupt' keyword is used to mark a function, and in the function the the external interrupt flag INTF is checked, if it is 1 then the Rb0 interrupt has occured. 'INTE' needs to be set to 1 to enable external interrupt. Hope it is some help. – DukeOfMarmalade Mar 11 '19 at 13:11
  • I think your question is too broad, as the type of interrupt and microcontroller may well be important. If you care specifically about your example then you should rephrase your question about that. – UKMonkey Mar 11 '19 at 13:28
  • I ask generally not this type specifically, if i had : void a and void b, how could the complier know that void b is the inrerrupt function. – Bakr Hesham Mar 11 '19 at 13:30
  • @BakrHesham the compiler cannot know which one is an interrupt function and which one is not. How could that be possible? Usually there is a special keyword or a pragma or whatever platform specific way to tell the compiler that some function is an interrupt function. – Jabberwocky Mar 11 '19 at 13:33
  • @BakrHesham and if your mark a function as "interupt" function (see my previous) it will not necessarily be called magically by what ever hardware interrupt (although this may also be platform specific), you need to configure this somehow (also totally platform specific). – Jabberwocky Mar 11 '19 at 13:38
  • very much compiler and target specific, which compiler and version are you using? – old_timer Mar 11 '19 at 21:56

1 Answers1

2

I think you mean Interrupt Service Routine when you refer to interrupt function. An ISR is a C function which has been flagged to be inserted in the hardware Interrupt Vector Table.

Here's an example:

void __interrupt Rb0_Isr(void);

or (link)

unsigned int  interruptcnt;
unsigned char second;

void timer0 (void) interrupt 1 using 2  {
    if (++interruptcnt == 4000)  {    /* count to 4000 */
        second++;                       /* second counter    */
        interruptcnt = 0;               /* clear int counter */
    }
}

As you see, it depends on the platform and on the compiler.

EDIT: thanks to @ThomasMatthews for the remark. ISRs can be implemented in C++ as well. If the question is: Is it possible to use a class method as the Interrupt Service Routine? the answer is yes but a bit more complicated. See the topic: C++ ISR using class method?

sebastian
  • 412
  • 1
  • 4
  • 14