Encode bit state -> hexadecimal
This FP instruction encodes the contents of data specified by s1_Start according to the contents of s2_Control if the trigger EN is TRUE. The encoded result is stored in the 16-bit destination area specified by d_Start starting with the specified bit position. Invalid bits in the area specified for the encoded result are filled with zeros.
FP_ENCODE is the inverse instruction of FP_DECODE.
Input
Starting 16-bit area to be encoded (source)
Control data to specify the starting bit position and number of bits to be encoded
Output
16-bit area for storing encoded data (destination)
The variables s1_Start, s2_Control and d_Start have to be of the same data type.
Example with s2_Control=16#0005
16# |
0 |
0 |
0 |
5 |
|
↑ |
↑ |
Number of bits to be encoded: 25=32 bits |
|||
Starting bit position of the data to be encoded: bit position 0 |
The encoded result 8 (decimal) is stored in d_Start when the trigger EN is TRUE.
Description of the process
The contents of the 2^nL bit segment at the beginning of the area specified by s1_Start is encoded (where nL is the number of bits to be encoded).
The encoded result is stored as decimal data in the eight bits starting from the bit specified as the nH bit (where nH is the conversion start bit). Invalid bits in the specified area for the result are filled with zeros.
Description of s2_Control
s2_Control specifies the starting bit position of the destination data and the number of bits to be encoded using hexadecimal data.
The bits marked "-" are invalid. |
|||||
Set value 16#0–16#F |
(3) Starting bit position of encoded destination data |
Set value 16#0–16#8 |
(4) Number of encoded bits |
||
16#0 |
0 |
16#1 |
2 |
||
16#1 |
1 |
16#2 |
4 |
||
16#2 |
2 |
16#3 |
8 (1 byte) |
||
16#3 |
3 |
16#4 |
16 (1 word) |
||
16#4 |
4 |
16#5 |
32 (2 words) |
||
16#5 |
5 |
16#6 |
64 (4 words) |
||
16#6 |
6 |
16#7 |
128 (8 words) |
||
16#7 |
7 |
16#8 |
256 (16 words) |
||
16#8 |
8 |
||||
16#9 |
9 |
||||
16#A |
10 |
||||
16#B |
11 |
||||
16#C |
12 |
||||
16#D |
13 |
||||
16#E |
14 |
||||
16#F |
15 |
Encoded example
When encoding 16-bit data (nL=4), the encoded results are as shown below.
Data to be encoded (4 bits) |
Encoded result |
|
---|---|---|
Hex. |
Dec. |
|
0000 0000 0000 0001 |
16#0000 |
0 |
0000 0000 0000 0010 |
16#0001 |
1 |
0000 0000 0000 0100 |
16#0010 |
2 |
0000 0000 0000 1000 |
16#0011 |
3 |
0000 0000 0001 0000 |
16#0100 |
4 |
0000 0000 0010 0000 |
16#0101 |
5 |
0000 0000 0100 0000 |
16#0110 |
6 |
0000 0000 1000 0000 |
16#0111 |
7 |
0000 0001 0000 0000 |
16#1000 |
8 |
0000 0010 0000 0000 |
16#1001 |
9 |
0000 0100 0000 0000 |
16#1010 |
10 |
0000 1000 0000 0000 |
16#1011 |
11 |
0001 0000 0000 0000 |
16#1100 |
12 |
0010 0000 0000 0000 |
16#1101 |
13 |
0100 0000 0000 0000 |
16#1110 |
14 |
1000 0000 0000 0000 |
16#1111 |
15 |
if the area specified using the index modifier exceeds the limit.
if 1 £ nL £ 8, where nL is the number of bits to be encoded/decoded.
if 1 £ nH + nL £ 16, where nH is the conversion start bit and nL is the number of bits to be encoded/decoded.
if the data to be encoded is all zero.
if the area specified using the index modifier exceeds the limit.
if 1 £ nL £ 8, where nL is the number of bits to be encoded/decoded.
if 1 £ nH + nL £ 16, where nH is the conversion start bit and nL is the number of bits to be encoded/decoded.
if the data to be encoded is all zero.
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
bStart: BOOL:=FALSE;
(*activates the function*)
wInput_value: WORD:=2#0000000001000000;
wControlCode: WORD:=16#0003;
(*specifies the encodation*)
wOutput_value: WORD:=0;
(*result after a 0->1 leading
edge from start:
2#0000000000000110*)
END_VAR
When the variable bStart is set to TRUE, the function is carried out.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 5 ;
NETWORK_BODY
B(B_CONTACT,,bStart,6,1,8,3,);
B(B_VARIN,,wInput_value,10,2,12,4,);
B(B_VARIN,,wControlCode,10,3,12,5,);
B(B_VAROUT,,wOutput_value,20,2,22,4,);
B(B_F,FP_ENCODE!,,12,0,20,5,,?DEN?Ds1_Start?Ds2_Control?AENO?Cd_Start);
L(1,2,6,2);
L(8,2,12,2);
L(1,0,1,5);
END_NETWORK_BODY
END_NET_WORK
END_BODY
IF bStart then
FP_ENCODE(s1_Start := wInput_value,
s2_Control := wControlCode,
d_Start => wOutput_value);
END_IF;