FUNCTION_BLOCK FB 1
TITLE =
VERSION : 0.1
VAR_INPUT
wInputBits : WORD ;
END_VAR
VAR_OUTPUT
wOutputBits : WORD ;
END_VAR
VAR
wErrorCode : WORD ;
END_VAR
VAR_TEMP
ErrorCode : STRUCT
Bit0p0 : BOOL ;
Bit0p1 : BOOL ;
Bit0p2 : BOOL ;
Bit0p3 : BOOL ;
Bit0p4 : BOOL ;
Bit0p5 : BOOL ;
Bit0p6 : BOOL ;
Bit0p7 : BOOL ;
Bit1p0 : BOOL ;
Bit1p1 : BOOL ;
Bit1p2 : BOOL ;
Bit1p3 : BOOL ;
Bit1p4 : BOOL ;
Bit1p5 : BOOL ;
Bit1p6 : BOOL ;
Bit1p7 : BOOL ;
END_STRUCT ;
lErrorCode : WORD ;
END_VAR
BEGIN
NETWORK
TITLE =Copy to input parameter to wErrorCode for testing
L #wInputBits;
T #wErrorCode;
NETWORK
TITLE =Method 1
//Copy wErrorCode into a structure of named bits using indirect addressing to
//calculate the address of the named structure. You can refer to the bits using
//your own defined name. If the structure is moved within the local data area,
//the code will still work.
LAR1 P##ErrorCode;
L #wErrorCode;
T W [AR1,P#0.0];
A #ErrorCode.Bit0p0;
= M 0.0;
A #ErrorCode.Bit1p0;
= M 1.0;
NETWORK
TITLE =method 2
//Copy wErrorCode into a local variable called lErrorCode which is also a word.
//Use indirect addressing to refer to the bits within the local copy of the error
//code using address register 1. You cannot refer to the bits by name, but you
//use [ar1,p#x.y]. If the structure is moved within the local data area, the
//code will still work.
L #wErrorCode;
T #lErrorCode;
LAR1 P##lErrorCode;
A [AR1,P#0.0];
= M 0.0;
A [AR1,P#1.0];
= M 1.0;
NETWORK
TITLE =method 3
//Use indirect addressing to refer to the bits of wErrorCode variable in the
//instance data block using address register 1. You cannot refer to the bits by
//name, but you use [ar1,p#x.y]. If wErrorCode is moved in the static data
//definition, the code will still work. Note that ar2 is added to the indirect
//address to support multiple instances.
L DIW 0;
LAR1 AR2;
L P##wErrorCode;
+AR1 ;
A DIX [AR1,P#0.0];
= M 0.0;
A DIX [AR1,P#1.0];
= M 1.0;
NETWORK
TITLE =Method 4
//Copy wErrorCode into an unused local data word, in this example, LW4, the then
//use L4.x or L5.y to refer to the bits. You cannot refer to the bits by name. If
//the local data area is changed, you may overwrite a variable.
L #wErrorCode;
T LW 4;
A L 4.0;
= M 0.0;
A L 5.0;
= M 1.0;
NETWORK
TITLE =Method 5
//Copy wErrorCode into a structure of named bits using the address of the
//structure from the editor in this example, LW0. You can refer to the bits by
//name. If the local data area is changed, you may overwrite a variable or lose
//the ability to refer to the bits by name.
L #wErrorCode;
T LW 0;
A #ErrorCode.Bit0p0;
= M 0.0;
A #ErrorCode.Bit1p0;
= M 1.0;
NETWORK
TITLE =copy MW0 to output for testing only
L MW 0;
T #wOutputBits;
END_FUNCTION_BLOCK