Traditionally, the microcontroller stack has only been
used as a storage space for return addresses of sub-
routines or interrupt routines, where all ‘push’ and ‘pop’
operations were hidden. For the most part, users had
no direct access to the information on the stack. The
PIC18 microcontroller diverges from this tradition
slightly. With the new PIC18 core, users now have
access to the stack and can modify the stack pointer
and stack data directly. Having such levels of access to
the stack allows for some unique and interesting
programming possibilities.
This application note describes specific information,
registers, and instructions related to accessing the
stack. An example is also included demonstrating a
very simple task manager, an essential element for a
real-time operating system (RTOS).M AN818
Manipulating the Stack of the PIC18 Microcontroller
Author: Ross M. Fosler The pointer to the top of the stack is provided in register
Microchip Technology Inc. STKPTR. The pointer is only five-bits wide, which
accounts for a stack depth of 32 words. However, the
first location is not counted, since it is not physically a
INTRODUCTION memory location in the stack. The first location always
Traditionally, the microcontroller s……