I am working on a project to control some lights, and seem to be having an issue with my nested subroutines. It seems like when I'm in one of the emergency light patterns, I call the appropriate delay subroutine, and the return opcode throws everything back to the main loop, bypassing the rest of the loop that the routine was called for. I know I haven't put in the code for pattern 1 yet, but that will be going in after I figure out this problem. (that one is going to be a lot more complex pattern). For my testing, I just have LEDs wired on port A pins 0-5, and buttons (and pulldown resistors) attached to port B pins 0-3. Is anyone here familiar with a solution to this problem? I only need to nest 2 layers deep, though 3 would be nice when I code the other light pattern (I plan on flashing the left set twice, then the right set twice, then all lights twice, like pattern 3 does once.)
Code:
init bsf 03h,5 ; enter config register
movlw b'11111111' ; set port B to be inputs
movwf 86h
movlw 00h ; set port A to be outputs
movwf 85h
bcf 03h,5 ; leave config register
movlw 00h
movwf 05h
MAIN btfsc 06h,2 ; Start main loop, and check for light pattern 1/3 bit
call PTRN1 ; Jump to patern 1/3 loop
btfsc 06h,3 ; Check for light pattern 2
call PTRN2 ; Jump to pattern 2 loop
movlw 00h ; Turn off all lights
movwf 05h
btfsc 06h,0 ; check if left signal should be on
bsf 05h,0 ; turn on left signal if it should be on
btfsc 06h,1 ; check if right signal should be on
bsf 05h,1 ; turn on right signal if it should be on
goto MAIN ; restart main loop
PTRN3 movlw 05h
movwf 0Bh
movlw b'11111111' ; LIGHT PATTERN 3
movwf 05h
call SHRTDLY
start3 decfsz 0Dh
goto fin3
xorwf 05h
call SHRTDLY
goto start3
fin3 call LNGDLY
return ; Return to main loop
PTRN1
lgt1 btfsc 06h,3 ; LIGHT PATERN 1, determine if should be PATERN 3
goto PTRN3 ; Goto Patern 3 if patern bit 2 is set
return ; return to main loop.
PTRN2
lgt2 btfsc 06h,2 ; Check if should be Pattern 3
goto PTRN3
movlw b'10101010' ; Start LIGHT PATTERN 2
movwf 05h
call LNGDLY
movlw b'11111111'
movwf 05h
call LNGDLY
return ; Return to main loop
SHRTDLY movlw 7Fh
movwf 0Ah
SD2 decfsz 08h,1 ; Short delay for strobe effect
goto SD2
decfsz 09h,1
goto SD2
decfsz 0Ah,1
goto SD2
return ; return to calling loop
LNGDLY decfsz 08h,1 ; Long delay for break in flash pattern
goto LNGDLY
decfsz 09h,1
goto LNGDLY
decfsz 0Ah,1
goto LNGDLY
decfsz 0Bh,1
goto LNGDLY
return ; Return to calling loop
end