4.2.2 Reducing Stack Depth
Another encountered issue was the stack. PIC® microcontrollers use a hardware stack to store function calls. Since this is a hardware implementation, there is a finite limit to the number of stack positions. With the overhead of our interrupts plus the UI software executing, the stack could exceed the maximum size and cause a stack overflow.
To resolve this, the menu for the UI was re-engineered to reduce the stack depth, which helped, but did not fully resolve the issue. A compile-time option called Compiled Stack in the XC8 compiler options was used to optimize the stack usage to within the limits of the hardware. Refer to Section 5.2.4.2.1 “Compiled Stack Operation”of the “MPLAB® XC8 C Compiler User’s Guide for PIC® MCU” (DS50002737) for more information.
