Lessons
2012 Microchip Technology Inc. DS41628B-page 61
Calls use one stack level. Remember that the PIC18 has a stack size of 31 levels,
whereas the enhanced core has 16 levels. Anytime a call is performed, the return
address will be pushed to the stack, then the program counter will go to the location in
program memory where the label is located.
It is important to note that stack depth should not be exceeded. For instance, perform-
ing 17 embedded call statements on the PIC16F1829 without returning at least once
will cause a Stack Overflow.
On the PIC18, a call instruction takes up two words of program space, however, a
PIC18 call can be anywhere in the program space. On the enhanced mid-range, a
call must first set the page select bits if the call is to be outside the currently
selected page.
3.6.5.1.2 return
A return restores the program counter to the last address that was saved into the
stack, and the Stack Pointer moves to the previous call in the list counter. The PC will
now be at the instruction immediately following the call. A Stack Underflow will be
caused if the program executes a return statement with no prior call.
In any case, the programmer can use the STVREN Configuration bit to cause a Reset
if a Stack Underflow/Overflow occurs. Both the call and return instructions take two
cycles.
3.6.5.1.3 xorwf
XORWF is used in this lesson to check if the ADC result is zero. Here is the truth table
of the XOR:
EXAMPLE 3-24:
This performs an exclusive-OR of the Delay2 register with ‘0’ to check if Delay2 has a
value of ‘0’. If so, the Z bit in the STATUS register will be set, since the answer is ‘0’.
3.6.5.2 PIC18
3.6.5.2.1 TSTFSZ
This is a quick test if a register is ‘0’ or not. Use this instruction on the PIC18 instead of
the XORWF used on the PIC16, since this saves a few instructions. For example:
TABLE 3-25: XOR TRUTH TABLE
Input
Output
A B
00 0
01 1
10 1
11 0
movlw d'0' ;load wreg with '0'
xorwf Delay2, w ;XOR wreg with the ADC result and save in wreg
btfss STATUS, Z ;if the ADC result is NOT '0', then simply return to MainLoop
TABLE 3-26: NEW INSTRUCTIONS FOR PIC18
Instruction English Purpose
tstfsz Test if a register is empty Quick check if zero (IF statement)
rcall Relative call Modular code