Main Content

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 Instruction

To create a user-defined instruction, use a Custom Instruction block added to the Simulink® PLC Coder™ Ladder Library.

  1. To open the Ladder Library, at the MATLAB® command line, enter:

    plcladderlib

    The Ladder Library opens all the blocks required for building the Ladder Diagram in Simulink.

  2. To create a new Simulink library, in the Library tab click New > Library. From the Simulink start page, select Blank Library and click Create Library.

  3. Drag a Custom Instruction block from the Ladder Library to the new library that you created.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  1. To open the Simulink Start Page, on the MATLAB Home tab, click Simulink.

  2. Select Blank Library and click Create Library.

  3. Save the library as plcuserlib.slx to a folder on the MATLAB path.

  4. To open the PLC Ladder Library, at the MATLAB command line, enter:

    plcladderlib

  5. Drag the Custom Instruction block from plcladderlib to your user-defined library plcuserlib.slx.

  6. Double-click the Custom Instruction block to open the Block Parameters.

  7. Specify the Instruction Name as SQR. Check that the Number of Inputs is 1 and Input Types is specified as a cell array of allowed data types. Similarly, check that the Number of Outputs is 1 and Output Types is specified as a cell array of allowed data types. Click OK.

  8. Click in the SQR block and double-click the Instruction_Enable subsystem.

  9. Inside the Instruction_Enable subsystem, add a Sqrt block from the Simulink / Math Operations Library. Double-click this block and select signedSqrt from Main>Function, and then click OK.

  10. Connect the input and output ports to the input and output ports of Sqrt block by using Data Type Conversion blocks.

  11. 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.slx is on the MATLAB path. You can drag this instruction to your models from the library that you have created and saved.

  12. To verify if Simulink PLC Coder has identified the newly created instruction, at the MATLAB command line, enter:

    plcladderinstructions
    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.

Limitations

The Custom Instruction block does not support instructions:

  • With data type array and struct (composite) as arguments.

  • That require internal data storage (states).

See Also

| | |

Related Topics