BCD -> ASCII conversion
This FP instruction converts the BCD code at s1 to ASCII code if the trigger EN is TRUE. s2_Control specifies the number of bytes to be converted and the conversion direction.
The result is stored in the area specified by d_Start. ASCII code requires 8 bits (1 byte) to express 1 BCD character. Upon conversion to ASCII, the data length will thus be twice the length of the source data.
Input
BCD code
Number of bytes and conversion direction
Output
Starting address of the data area for the results. The size is n_Bits * 2 words.
Specifying the control code s2_Control
16-bit: 1–2 bytes to be converted
32-bit: 1–4 bytes to be converted
0: forward
1: reverse
The two characters that make up one byte are interchanged when stored. Two bytes are converted as one segment of data.
Forward direction:
Reverse direction:
ASCII HEX codes to express BCD characters:
BCD character |
ASCII HEX code |
---|---|
0123456789 |
16#3016#31 16#32 16#33 16#34 16#35 16#36 16#37 16#38 16#39 |
if the area specified using the index modifier exceeds the limit.
if the data specified by s1_Start is not BCD data
if the number of bytes specified by s2_Control is greater than the area specified by s1_Start
if the conversion result is greater than the data area specified by d_Start
if s2_Control = 0
if the conversion direction is out of range
if the number of bytes specified in s2_Control is greater than 4
if the area specified using the index modifier exceeds the limit.
if the data specified by s1_Start is not BCD data
if the number of bytes specified by s2_Control is greater than the area specified by s1_Start
if the conversion result is greater than the data area specified by d_Start
if s2_Control = 0
if the conversion direction is out of range
if the number of bytes specified in s2_Control is greater than 4
Offset |
BCD characters |
Offset |
Converted ASCII codes |
BCD equivalent |
|||
s1 |
0 |
16#1234 |
Þ |
d_Start |
0 |
16#3433 |
43 |
s2_Control |
16#0002 |
16#3231 |
21 |
||||
Offset |
BCD characters |
Offset |
Converted ASCII codes |
BCD equivalent |
|||
s1 |
0 |
16#1234 |
Þ |
d_Start |
0 |
16#3231 |
21 |
s2_Control |
16#1002 |
16#3433 |
43 |
||||
Offset |
BCD characters |
Offset |
Converted ASCII codes |
BCD equivalent |
|||
s1 |
0 |
16#5678 |
Þ |
d_Start |
0 |
16#3837 |
87 |
1 |
16#1234 |
1 |
16#3635 |
65 |
|||
s2_Control |
16#0004 |
16#3433 |
43 |
||||
16#3231 |
21 |
Offset |
BCD characters |
Offset |
Converted ASCII codes |
BCD equivalent |
|||
s1 |
0 |
16#5678 |
Þ |
d_Start |
0 |
16#3231 |
21 |
1 |
16#1234 |
1 |
16#3433 |
43 |
|||
s2_Control |
16#1004 |
16#3635 |
65 |
||||
16#3837 |
87 |
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;
wInput: WORD:=16#1234;
awResult: ARRAY [0..2] OF WORD:=[3(0)];
END_VAR
When the variable bStart is set to TRUE, the function is carried out. Two bytes from s1_Start are converted to an ASCII value in reverse direction.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 8 ;
NETWORK_BODY
B(B_COMMENT,,After conversion: ø^awResult[0]=16#3231ø^awResult[1]=16#3433ø^awResult[2] is not written,24,1,39,5,);
B(B_CONTACT,,bStart,4,4,6,6,R);
B(B_F,FP_BCD_TO_ASCII!,,12,3,22,8,,?DEN?D@'s1'?Ds2_Control?AENO?Cd_Start);
B(B_VARIN,,wInput,10,5,12,7,);
B(B_VAROUT,,awResult[0],22,5,24,7,);
B(B_VARIN,,16#1002,10,6,12,8,);
L(1,0,1,8);
L(1,5,4,5);
L(6,5,12,5);
END_NETWORK_BODY
END_NET_WORK
END_BODY
If (DF(bStart)) Then
FP_BCD_TO_ASCII(s1 := wInput, s2_Control := 16#1002,
d_Start => awResult[0]);
(* After conversion:
awResult[0]=16#3231
awResult[1]=16#3433
awResult[2] is not written *)
End_if;