Sometimes you’ll have to replicate the same code many places in your program. This is a waste of time and your limited program memory since you have only 1024 words. So what’s the solution? Subroutines!

Let’s say you wanted to light some LED’s in the same pattern several times in your program (much fun application would be to move a robotic arm to some certain angle). Let’s talk about LED’S for now. See the following code.

; Lable the addresses
STATUS	equ 03h
TRISA	equ 85h
PORTA	equ 05h

; Setting input/output pins
	bsf	STATUS, 5	; changing to bank 1 in order to access TRISA
				; register.
	movlw   b'00000' 	; store the binary value in the W register.
	movwf	TRISA		; putting the literal value of W to the
				; following file register (in this case TRISA).
	bcf	STATUS, 5	; changing back to bank 0.
 ; The program
        call PATTERN1
        call PATTERN1

        movlw  b'10101'
        movwf  PORTA     ; Lights a combination of LED's,
                         ; you can guess which ones going to light right?
        return           ; returns to the main program


The instruction “call” calls the PATTERN1. Then the microcontroller stops executing the main program and starts PATTERN1 subroutine. After PATTERN1 is completed it sees the instruction “return” and this will cause the microcontroller return and execute the main program where it stopped. An application of this type of program would be to notify the user status of a machine. For example when the machine is doing something hazardous, or if it’s doing something so important that the user shouldn’t turn it off any case.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: