uptown47
Lifetime Supporting Member
Hi all
I'm trying to convert an S5 program to S7 and part of the old S5 program calls FB242. This was a special function block that had the following description:
FB 242 - Multiplier
Use function block FB242 to multiply one fixed point binary number (16 bits) by another. The product is represented by two fixed-point binary numbers (16 bits each). The result is also scanned for zero. An eight-bit number must be transferred to a 16-bit word prior to multiplication.
The following are accepted and returned by the function FB242
Z1 - Multiplier (word)
Z2 - Multiplicand (word)
Z3 - Zero (bool) comes on if result is zero
Z32 - Product, high order word
Z31 - Product, low order word
I've started writing the code for this in its own function within S7 then I can just call it everytime I need it. I've got so far but then got stuck.
I convert the two words passed into function into Double Words so I can use a *D to multiply them. I get the result but how do I split it into a High order word and Low order word??
I've attached the source of my block so far.
A big thanks if you can help me out
Cheers
I'm trying to convert an S5 program to S7 and part of the old S5 program calls FB242. This was a special function block that had the following description:
FB 242 - Multiplier
Use function block FB242 to multiply one fixed point binary number (16 bits) by another. The product is represented by two fixed-point binary numbers (16 bits each). The result is also scanned for zero. An eight-bit number must be transferred to a 16-bit word prior to multiplication.
The following are accepted and returned by the function FB242
Z1 - Multiplier (word)
Z2 - Multiplicand (word)
Z3 - Zero (bool) comes on if result is zero
Z32 - Product, high order word
Z31 - Product, low order word
I've started writing the code for this in its own function within S7 then I can just call it everytime I need it. I've got so far but then got stuck.
I convert the two words passed into function into Double Words so I can use a *D to multiply them. I get the result but how do I split it into a High order word and Low order word??
I've attached the source of my block so far.
FUNCTION "EQUIVALENT OF FB242" : VOID
TITLE =
VERSION : 0.1
VAR_INPUT
MULTIPLIER : WORD ;
MULTIPLICAND : WORD ;
END_VAR
VAR_OUTPUT
RESULT_HIGH_WORD : WORD ;
ZERO_RESULT : BOOL ;
RESULT_LOW_WORD : WORD ;
END_VAR
VAR_TEMP
MULTIPLIER_DWORD : DINT ;
MULTIPLICAND_DWORD : DINT ;
RESULT_DWORD : DINT ;
END_VAR
BEGIN
NETWORK
TITLE =ZERO THE TEMP DOUBLE WORD VALUES
A( ;
L L#0;
T #MULTIPLIER_DWORD;
SET ;
SAVE ;
CLR ;
A BR;
) ;
JNB _001;
L L#0;
T #MULTIPLICAND_DWORD;
_001: NOP 0;
NETWORK
TITLE =MAKE THE INITIAL WORD VALUES INTO DOUBLE WORD FOR MULTIPLICATION
A( ;
L #MULTIPLIER;
T #MULTIPLIER_DWORD;
SET ;
SAVE ;
CLR ;
A BR;
) ;
JNB _002;
L #MULTIPLICAND;
T #MULTIPLICAND_DWORD;
_002: NOP 0;
NETWORK
TITLE =RESULT OF MULTIPLICATION AS DOUBLE WORD
L #MULTIPLIER_DWORD;
L #MULTIPLICAND_DWORD;
*D ;
T #RESULT_DWORD;
NOP 0;
NETWORK
TITLE =CHECK IF RESULT IS ZERO. IF IT IS SET THE ZERO_RESULT BIT.
L #RESULT_DWORD;
L L#0;
==D ;
= #ZERO_RESULT;
END_FUNCTION
A big thanks if you can help me out
Cheers