Frequently Asked Questions

What are the restrictions on function calls from an interrupt function?

Whenever interrupts are used, the programmer MUST ensure there will be enough stack space. Ensure the size of the stack required by the interrupt plus the size of the stack already used by main() wherever interrupts are enabled is less than 9. This can be seen at the top of the list file.

The compiler does not permit recursive calls to functions because the RISC instruction set does not provide an efficient means to implement a traditional C stack. All RAM locations required for a given function are allocated to a specific address at link time in such a way that RAM is re-used between functions not active at the same time. This prohibits recursion. For example, the main() function may call a function A() and A() may call B() but B() may NOT call main(), A() or B().

An interrupt may come in at any time, which poses a special problem. Consider the interrupt function called ISR() that calls the function A() just like main() calls A(). If the function A() is executing because main() called it and then the ISR() activates, recursion will have happened.

In order to prevent the above problem, the compiler will "protect" the function call to A() from main() by disabling all interrupts before the call to A() and restoring the interrupt state after A() returns. In doing so, the compiler can allow complete sharing of functions between the main program and the interrupt functions.

The programmer must take the following special considerations into account:

  1. In the above example, interrupts will be disabled for the entire execution of A(). This will increase the interrupt latency depending on the execution time of A().

  2. If the function A() changes the interrupts using ENABLE/DISABLE _INTERRUPTS then the effect may be lost upon the return from A(), since the entire INTCON register is saved before A() is called and restored afterwards. Furthermore, if the global interrupt flag is enabled in A(), the program may execute incorrectly.

  3. A program should not depend on the interrupts being disabled in the above situation. The compiler may NOT disable interrupts when the function or any function it calls requires no local RAM.

  4. The interrupts may be disabled, as described above for internal compiler functions called by the same manor. For example, multiplication invoked by a simple * may have this effect.

C-Aware IDE Demo
Textbook to Learn C for PIC
C Workshop Compiler