beautypg.com

Watlow LogicPro User Manual

Page 198

background image

Chapter 11: User Defined Function Blocks

LogicPro User’s Guide

182

Watlow Anafaze

Doc.# 28002-00 Rev 3.00

typedef struct {

int A;

int B;

int C;

bool EN;

bool Q;

}ADD1Struct;

This structure holds all variables that are passed to the
Function Block. New members can be added to the end of this
structure. These new members will then retain their value
even after the function exits. Each instance of the UDFB will
have its own separate instance of the structure, so interaction
between several instances of the same Function Block can be
prevented.

Boolean variables are created within this structure to match
the boolean variables defined for the block. These variables
are used for Function Block instances. The user has the option
of updating these variables. As a general rule they should be
set to match the Function Block variables. ADD1

C code can

be written as in the following example:

#include “PCADD1.h”

int ADD1(bool EN,bool *Q,ADD1Struct *ADD1StructA)

{

long Result;

/* Declare long variable to hold result */

/* even if addition result is out of integer range */

/* Check that block is enabled */

if (EN)

{

/* Perform addition */

Result = (long) ADD1StructA->A + ADD1StructA->B;

/* Check that result is within int range (-32768 to 32767) */

if ((Result >= -32768) && (Result <= 32767))

{

/* Set Bool output to signify successful completion */

*Q = 1;

/* Assign Result to integer output */

ADD1StructA->C = Result;

/* Copy bools to structure for access as Function Block

instance variables */

ADD1StructA->EN = EN;