Low Pin Count Demo Board User’s Guide
DS51556A-page 26 © 2005 Microchip Technology Inc.
3.2.8 Lesson 8: Function Calls
Lesson 8 shows the reversible LEDs but with the Delay Loop rewritten as a function.
New Instructions
CALL Invokes functions or subroutines
RETURN Terminates functions or subroutines
RETLW Terminates functions or subroutines
Functions or Subroutines are invoked with the CALL instruction and terminated with a
RETURN or RETLW instruction. RETURN jumps back to the original program at the
location following the CALL. RETLW also returns to the calling program, but loads Wreg
with a constant.
The mid-range PICmicro MCU device’s CALL stack can hold up to 8 return addresses.
If a ninth CALL is made, it will overwrite the first one and then the program will not be
able to RETURN all the way back.
Passing Arguments
Arguments to the subroutine may be passed in a number of ways. Wreg is a convenient
place to pass one byte and the FSR may be used to pass another byte, if not otherwise
used. If more data must be passed, a buffer must be allocated.
When the Delay function is pulled out to a subroutine, the ADC result is moved into
Wreg, then the CALL transfers control to the Delay subroutine. The RETURN transfers
control to the MOVLW following the CALL.
EXAMPLE 3-7: FUNCTION CALL EXAMPLE
MOVF ADRESH,w
CALL Delay ;call delay function
;returns here when done
...
GOTO xxx
; Delay function.
; Delay time is Wreg value * 771 uS
Delay
MOVWF Delay2
DelayLoop
DECFSZ Delay1,f
GOTO DelayLoop
DECFSZ Delay2,f
GOTO DelayLoop
RETURN