1. C language function block calls CALLM

1. C language function block calls CALLM

1.1. Instruction Description

When the CALLM instruction is valid, it will call a function block and start a function block subroutine.

When the CALLM instruction is invalid, the program will automatically skip this CALLM instruction and execute the next instruction.

Attention:

  1. When using the CALLM instruction to call function blocks, it is important to handle the entry and exit judgments properly to prevent timing or logical errors that may cause program execution errors.

  2. The design of function blocks is detailed in the "Design of Function Blocks" section.

  3. Pay attention to the security of arrays and pointers, otherwise it may cause serious security issues such as PLC crashes, and even cannot be recovered by dialing.

  4. When using floating-point numbers, the input parameters must be even addresses (except for the F1 model of STM, for other models where the input parameters are floating-point numbers or floating-point numbers using non double words, the addresses must be even, otherwise it may cause a hard fault).

1.2. Example(sort)

function block:

// Data initialization
void Data_Init(WORD data, WORD size)
{
    short i = 0 ;
    for ( i = 0 ; i < *size ; i++)
        data[i] = (i ^ 0x4321);        // each data are all OR 0x4321
}
// Sort data from small to large
void Data_Sort(WORD data, WORD size)
{
    short i = 0;
    short j = 0;
    short temp = 0;
    for ( i = 0 ; i < *size ; i++)
    {
        temp = data[i];
        for (j = i ; j>0 && data[j-1]>temp ; j--)
            data[j] = data[j-1];    // Continuously bubbling forward
        data[j] = temp;
    }
}

Command table:

NETWORK 000

LDP M0 // When the rising edge of M0 approaches, execute the function blockData_Init,let D[0]=0x4321,D[1]=0x4320,D[2]=0x4323,D[3]=0x4322,D[4]=0x4325,D[5]=0x4324,D[6]=0x4327

MOV K7 D100 // set length to D100

CALLM Data_Init D0 D100 // call Data_Init,parameter data=D0, size=D100

POP

LDP M1 // When the rising edge of M1 approaches, execute the function blockData_Sort, Sort D [0] to D [6] from small to large,result D[0]=0x4320,D[1]=0x4321,D[2]=0x4322,D[3]=0x4323,D[4]=0x4324,D[5]=0x4325,D[6]=0x4327

CALLM Data_Sort D0 D100 // call Data_Sort,parameter data=D0, size=D100

POP

LDW< D1 D2

AW< D2 D3

AW< D3 D4

AW< D4 D5

AW< D5 D6

OUT Y000 // if D1<D2<D3<D4<D5<D6,Y000 set 1

POP

CALLM11

图1 CALLM11

CALLM12

图2 CALLM12

1.3. Example (finding the standard deviation of a set of floating points)

function block:

// Calculate the average value
void Averange(FLOAT in, WORD size, FLOAT out)
{
    *out = 0.0;
    short i = 0;
    for ( ; i < *size ; i++)
        *out += in[i];
    *out /= *size;
}
// Calculate standard deviation
void StandardDeviation(FLOAT in, WORD size, FLOAT out)
{
    float ave = 0.0;
    Averange(in, size, &ave);
    short i = 0;
    *out = 0.0;
    for ( ; i < *size ; i++)
        *out += (in[i] - ave) * (in[i] - ave);
    *out /= *size;
    *out = sqrt(*out);
}

command table:

NETWORK 000

LDP M0

MOV K10 D100 // set length to D100

CALLM StandardDeviation D0 D100 D200 // call StandardDeviation,parameter in=D0, size=D100,out=D200

CALLM21

图3 CALLM21