Create Custom Instruction in PLC Ladder Diagram Models
You can create user-defined instructions for your ladder models by using the Custom Instruction block. You can store
these blocks containing custom instructions in a user-defined library named
plcuserlib.slx. You can also import, simulate, and export your ladder
instructions by using your custom blocks.
Create User-Defined Custom Instruction
To create a user-defined instruction, use a Custom Instruction block that you add to the Simulink® PLC Coder™ Ladder Library.
To open the Ladder Library, at the MATLAB® command line, enter:
The Ladder Library opens all the blocks required for building the Ladder Diagram in Simulink.
To create a Simulink library, in the Library tab, click New > Library. From the Simulink start page, select Blank Library and click Create Library.
Drag a Custom Instruction block from the Ladder Library to the new library that you created. See Custom Instruction.
To build your own ladder logic model, double-click your Custom Instruction block to see the block parameters. Use the Help menu to view their descriptions.
In Instruction Name text field, give a name to your instruction. Specify the inputs and outputs required for your instruction block. Click Apply, and then click OK.
To look inside the mask, click in the Custom Instruction block. The blocks inside the mask enable the instruction to simulate with other PLC Ladder instructions. The user-defined logic is included in the Instruction_Enable block.
Save the library as
plcuserlib.slx. You can add multiple instruction blocks to this library.
Calculate Square Root by Using Custom Instruction Block
This example shows how to calculate square root of an input signal by using a Custom Instruction block.
To open the Simulink Start Page, on the MATLAB Home tab, click Simulink.
Select Blank Library and click Create Library.
Save the library as
plcuserlib.slxto a folder on the MATLAB path.
To open the PLC Ladder Library, at the MATLAB command line, enter:
Drag the Custom Instruction block from
plcladderlibto your user-defined library
Double-click the Custom Instruction block to open the Block Parameters.
Specify the Instruction Name as
SQR. Make sure that the Number of Inputs is
1and Input Types is specified as a cell array of allowed data types. Make sure that the Number of Outputs is
1and Output Types is specified as a cell array of allowed data types. Click OK.
Click in the SQR block and double-click the Instruction_Enable subsystem.
Inside the Instruction_Enable subsystem, add a Sqrt block from the Simulink / Math Operations Library. Double-click this block and select
signedSqrtfrom Main>Function, and then click OK.
Connect the input and output ports to the input and output ports of Sqrt block by using Data Type Conversion blocks.
Navigate to the top level of the library. Click Lock Links and Unlock Library in the Library tab, and then save the library. Simulink PLC Coder can now use the SQR instruction when
plcuserlib.slxis on the MATLAB path. You can drag this instruction to your models from the library that you have created and saved.
To verify if Simulink PLC Coder has identified the newly created instruction, at the MATLAB command line, enter:This command lists the instructions that Simulink PLC Coder can use. The supported instructions displayed in the output includes the SQR instruction.
The example in the image shows the use of the SQR instruction inside an Add-On Instruction block.
The Custom Instruction block does not support instructions:
With data type
struct(composite) as arguments.
That require internal data storage (states).
plcladderinstructions | Custom Instruction |