A subroutine helps you to block the program. The instructions used in the main program determine the execution status of the specific subroutine. When the main program calls a subroutine and executes it, the subroutine executes all instructions until the end. The system then returns control to the main program in the calling subroutine network.
Subroutines are used to segment and block programs into smaller, more manageable blocks. You can take advantage of this when debugging and maintaining in your program. These areas and the entire program are simply debugged and trouble-shooted by using smaller blocks. The program block can only be called up when needed, so that the PLC can be used more efficiently, since all blocks may not have to perform each scan.
Finally, if the subroutine only references parameters and local memory, the subroutine can be moved. To move subroutines, avoid using any global variables/symbols (I, Q, M, SM, AI, AQ, V, T, C, S, absolute addresses in AC memory). If the subroutine has no call parameters (IN, OUT, or IN_OUT) or only local variables in L memory, you can export the subroutine and import it into another project.
To use a subroutine in a program, you must perform the following three tasks:
Creating a subroutine
Define parameters (if any) in the subroutine local variable table
Calling a subroutine from the appropriate POU (from the main program or another subroutine)
When the subroutine is called, the entire logical stack is saved, the top of the stack is set to one, all other stack locations are set to zero, and control is passed to the calling subroutine. When the subroutine completes, the stack reverts to the value that was retained at the point of the call, and control returns to the calling routine.
The subroutine and the calling routine share the accumulator. Due to the use of the subroutine, no save or restore operation is performed on the accumulator.
Calling a subroutine with parameters
A subroutine may contain parameters for the handover. The parameters are defined in the local variable table of the subroutine. The argument must have a symbolic name (up to 23 characters), a variable type, and a data type. You can transfer 16 parameters to a subroutine or 16 parameters from a subroutine.
The variable type field in the local variable table defines whether the parameter is handed over to the subroutine (IN), handed over to or handed off the subroutine (IN_OUT), or handed off the subroutine (OUT). The table below shows the parameter types of the subroutine. To add a parameter entry, place the cursor on the variable type field (IN, IN_OUT, or OUT) of the type you wish to add. Right click on the unipolar mouse to get the options menu. Select the “Insert” option and then select the “Next Line” option. Another parameter entry of the selected type is displayed below the current entry.
The Jump to Label (JMP) instruction performs a branch operation on the specified label (n) in the program. The stack top value is always a logic 1 when the jump is accepted. The label (LBL) instruction marks the location of the jump destination (n). You can use the “jump” instruction in the main program, subroutine or interrupt routine. The “jump” and its corresponding “tag” instruction must always be in the same code segment (main program, subroutine or interrupt routine). You cannot jump from the main program to a subroutine or interrupt the tag in the routine. Similarly, you cannot jump from a subroutine or interrupt routine to a tag other than the subroutine or interrupt routine. You can use the “jump” instruction in the SCR segment, but the corresponding “tag” instruction must be in the same SCR segment.
The Interrupt Enable (ENI) instruction globally enables all additional interrupt event processes. The Interrupt Disable (DISI) instruction globally disables all interrupt event processes. When transitioning to RUN mode, the interrupt is disabled at the beginning. Once in RUN mode, you can enable all interrupt processes by executing the global interrupt enable instruction. Execution of the interrupt disable instruction will disable the processing of the interrupt; however, the active interrupt event will continue to wait in the queue.