ASCII -> BCD conversion
This FP instruction converts the hexadecimal ASCII codes at s1_Start to BCD characters 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 d.
Input
Starting address
Number of bytes and conversion direction
Output
Converted bytes
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#30 16#31 16#32 16#33 16#34 16#35 16#36 16#37 16#38 16#39 |
ExampleSigned/unsigned 16-bit data, forward direction, 4 characters
Offset |
ASCII codes |
BCD equivalent |
Offset |
Converted BCD characters |
|||
s1_Start |
0 |
16#3231 |
21 |
Þ |
d |
0 |
16#3412 |
1 |
16#3433 |
3 |
1 |
||||
s2_Control |
16#0004 |
Example16-bit data, reverse direction, 4 characters
Offset |
ASCII codes |
BCD equivalent |
Offset |
Converted BCD characters |
|||
s1_Start |
0 |
16#3231 |
21 |
Þ |
d |
0 |
16#1234 |
1 |
16#3433 |
43 |
1 |
||||
s2_Control |
16#1004 |
Example32-bit data, forward direction, 8 characters
Offset |
ASCII codes |
BCD equivalent |
Offset |
Converted BCD characters |
|||
s1_Start |
0 |
16#3231 |
21 |
Þ |
d |
0 |
16#3412 |
1 |
16#3433 |
3 |
1 |
16#7856 |
|||
2 |
16#3635 |
65 |
2 |
||||
3 |
16#3837 |
87 |
3 |
||||
s2_Control |
16#0008 |
Example32-bit data, reverse direction, 8 characters
Offset |
ASCII codes |
BCD equivalent |
Offset |
Converted BCD characters |
|||
s1_Start |
0 |
16#3231 |
21 |
Þ |
d |
0 |
16#5678 |
1 |
16#3433 |
43 |
1 |
16#1234 |
|||
2 |
16#3635 |
65 |
2 |
||||
3 |
16#3837 |
87 |
3 |
||||
s2_Control |
16#1008 |
Example32-bit data, reverse direction, 7 characters
If an odd number of characters is being converted, 0 will be entered for bit position 0–3 of the last converted byte if the conversion direction is "forward". If the conversion direction is "reverse", 0 will be entered for bit position 4–7:
Offset |
ASCII codes |
BCD equivalent |
Offset |
Converted BCD characters |
|||
s1_Start |
0 |
16#3231 |
21 |
Þ |
d |
0 |
16#4567 |
1 |
16#3433 |
43 |
1 |
16#0123 |
|||
2 |
16#3635 |
65 |
2 |
||||
3 |
16#3837 |
87 |
3 |
||||
s2_Control |
16#1007 |
if the area specified using the index modifier exceeds the limit.
if ASCII characters other than 16#30–16#39 are specified
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
if s2_Control = 0
if the conversion direction is out of range
if the number of bytes specified in s2_Control is greater than 8
if the area specified using the index modifier exceeds the limit.
if ASCII characters other than 16#30–16#39 are specified
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
if s2_Control = 0
if the conversion direction is out of range
if the number of bytes specified in s2_Control is greater than 8
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*)
wASCIIInput: WORD:=16#3031;
wControlCode: WORD:=16#2;
(*specifies the operation:
digit 0: the number of bytes
to convert
digit 1,2: has to be zero
digit 3: nomal or reverse direction*)
wConversionResult: WORD:=0;
(*result after a 0->1 leading
edge from start:
[16#2033,0]*)
END_VAR
When the variable bStart is set to TRUE, the function is carried out. Two characters of the ASCII input value are converted in forward direction
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 6 ;
NETWORK_BODY
B(B_CONTACT,,bStart,5,2,7,4,);
B(B_VARIN,,wASCIIInput,11,3,13,5,);
B(B_VARIN,,wControlCode,11,4,13,6,);
B(B_VAROUT,,wConversionResult,23,3,25,5,);
B(B_F,FP_ASCII_TO_BCD!,,13,1,23,6,,?DEN?Ds1_Start?Ds2_Control?AENO?Cd);
L(1,3,5,3);
L(7,3,13,3);
L(1,0,1,6);
END_NETWORK_BODY
END_NET_WORK
END_BODY
if bStart then
FP_ASCII_TO_BCD(s1_Start := wASCIIInput,
s2_Control := wControlCode,
d => wConversionResult);
END_IF;