Omron NJ Series PLC Function Block (FB) Programming Guide

1. Function Block(FB) Introduction

The Function Block (FB) is an important component in the programming of Omron NJ series PLCs, allowing reusable code logic to be encapsulated as independent modules. Function blocks can contain input variables, output variables, input/output variables, and internal variables, written in Structured Text (ST) language, greatly enhancing the readability and maintainability of the code.

2. Function Block Variable Types

In the Omron NJ series PLCs, function blocks support the following variable types:

Variable Type

Description

Declaration Keyword

Input Variable

Data passed to the function block from external sources, cannot be modified within the function block

VAR_INPUT

Output Variable

Data output from the function block after processing, available for external programs

VAR_OUTPUT

Input/Output Variable

Variables that can be used as both input and output, the function block can read and modify

VAR_IN_OUT

Internal Variable

Temporary variables used only within the function block, not exposed externally

VAR

Note: Omron NJ series PLCs use tags (name) instead of memory addresses to access variables, making programming more intuitive and easier to maintain.

3. Function Block Example: Motor Control Function Block

Below is a detailed example of a motor control function block, illustrating how to use ST language to write a function block containing various variable types.

3.1 Function Block Definition

FUNCTION_BLOCK FB_MotorControlVAR_INPUTbStart : BOOL; (* Start signal *) bStop : BOOL; (* Stop signal *) rSpeedSetpoint : REAL; (* Speed setpoint *)END_VARVAR_OUTPUT bRunning : BOOL; (* Running status *) bFault : BOOL; (* Fault status *) rActualSpeed : REAL; (* Actual speed *)END_VARVAR_IN_OUT diSpeedFeedback : DINT; (* Speed feedback, readable and writable *)END_VARVAR tOnDelay : TON; (* On delay timer *) tOffDelay : TOF; (* Off delay timer *) rAcceleration : REAL; (* Acceleration calculation *) bInternalStart : BOOL; (* Internal start flag *)END_VAR

3.2 Function Block Implementation Logic

// Motor control logic// Start condition: there is a start signal and no stop signal or faultbInternalStart := bStart AND NOT bStop AND NOT bFault;// Use on delay timer for soft starttOnDelay(IN := bInternalStart, PT := T#2S);// Use off delay timer for soft stoptOffDelay(IN := NOT bInternalStart, PT := T#3S);// Running status determinationbRunning := tOnDelay.Q OR (bInternalStart AND NOT tOffDelay.Q);// Speed processing: convert speed feedback value to actual speedrActualSpeed := INT_TO_REAL(diSpeedFeedback) / 100.0;// Acceleration calculation (using internal variable)rAcceleration := (rSpeedSetpoint – rActualSpeed) / 2.0;// Fault detection: if the deviation between set speed and actual speed is too large, report a faultIF ABS(rSpeedSetpoint – rActualSpeed) > 50.0 AND bRunning THEN bFault := TRUE;END_IF;// If needed, speed feedback can be updated through input/output variablesIF bRunning THEN diSpeedFeedback := diSpeedFeedback + 1; // Example: simulate speed feedback changeEND_IF;

4. Function Block Invocation

Once the function block is defined, it can be called in the main program or other function blocks:

PROGRAM MAINVARMotor1 : FB_MotorControl; (* Motor1 control instance *) bStartBtn : BOOL; (* Start button *) bStopBtn : BOOL; (* Stop button *) rSetSpeed : REAL := 100.0; (* Set speed *) nSpeedFeedback : DINT; (* Speed feedback *)END_VAR// Call the motor control function blockMotor1( bStart := bStartBtn, bStop := bStopBtn, rSpeedSetpoint := rSetSpeed, diSpeedFeedback := nSpeedFeedback);

5. Best Practices

  • Use meaningful names for variables and function blocks to improve code readability
  • Use comments wisely to explain complex logic
  • Limit the size of function blocks; each function block should accomplish a single, clear function
  • Use internal variables to store intermediate results, avoiding complex expression nesting
  • Perform validity checks on input parameters to enhance code robustness

Leave a Comment