-
Notifications
You must be signed in to change notification settings - Fork 5
/
AIN1.EXP
executable file
·88 lines (73 loc) · 2.04 KB
/
AIN1.EXP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(* @NESTEDCOMMENTS := 'Yes' *)
(* @PATH := '\/Engineering\/signal processing' *)
(* @OBJECTFLAGS := '0, 8' *)
(* @SYMFILEFLAGS := '2048' *)
FUNCTION_BLOCK AIN1
VAR_INPUT
in : DWORD;
END_VAR
VAR_INPUT CONSTANT
sign_bit : INT := 255;
error_bit : INT := 255;
error_code_en : BOOL;
error_code : DWORD;
overflow_bit : INT := 255;
overflow_code_en : BOOL;
overflow_code : DWORD;
Bit_0 : INT;
Bit_N : INT := 31;
out_min : REAL;
out_max : REAL := 10.0;
code_min : DWORD;
code_max : DWORD := 16#FFFFFFFF;
error_output : REAL;
overflow_output : REAL := 10.0;
END_VAR
VAR_OUTPUT
out : REAL;
sign : BOOL;
error : BOOL;
overflow : BOOL;
END_VAR
VAR
tB: DWORD;
END_VAR
(*
version 1.3 10. mar. 2009
programmer oscat
tested by tobias
Ain1 converts signals from A/D converters or other digital sources to an internal real value.
*)
(* @END_DECLARATION := '0' *)
(* extract error bit *)
error := ((SHR(in,error_bit) AND 16#0000_0001) = 1) OR (error_code_en AND error_code = in);
IF error THEN
out := error_output;
RETURN;
END_IF;
(* strip off the data input *)
tb := SHR(SHL(in, 31 - bit_N), 31 - bit_N + Bit_0);
(* extract overflow bit *)
overflow := ((SHR(in,overflow_bit) AND 16#0000_0001) = 1) OR (overflow_code_en AND overflow_code = in) OR (tb < code_min OR tb > code_max);
IF overflow THEN
out := overflow_output;
RETURN;
END_IF;
(* extract sign bit *)
sign := (SHR(in,sign_bit) AND 16#0000_0001) = 1;
(* convert in to out *)
out := (DWORD_TO_REAL(tb - code_min) * (out_max - out_min) / DWORD_TO_REAL(code_max - code_min) + out_min);
IF sign THEN out := out * -1.0; END_IF;
(* revision history
hm 23. feb 2008 rev 1.0
original version
hm 16. mar 2008 rev 1.1
added type conversions to avoid warnngs under codesys 30
hm 22. apr. 2008 rev 1.2
corrected error in formula when code_min was set
corrected error when sign bit was used
optimized code for better performance
hm 10. mar. 2009 rev 1.3
real constants updated to new systax using dot
*)
END_FUNCTION_BLOCK