AIN1

Begonnen von shooter, 13. Dezember 2012, 20:24:33

Vorheriges Thema - Nächstes Thema

0 Mitglieder und 2 Gäste betrachten dieses Thema.

shooter

wenn auch negativ signale wie bei wago -10/+10V input benutzt werden muss der letzte regel geandert worden in

IF sign THEN out:= out - out_max; END_IF
so with this change it will be AIN2 (done in my system

shooter

(* @NESTEDCOMMENTS := 'Yes' *)
(* @PATH := '\/Engineering\/signal processing' *)
(* @OBJECTFLAGS := '0, 8' *)
(* @SYMFILEFLAGS := '2048' *)
FUNCTION_BLOCK AIN2
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

Ain2 converts positiv and negativ 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-out_max; 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

shooter@home.nl

shooter

i have changed the calcs to be conform.
added a thinker to change the maxcode as this is also embedded in bit_0 and BIT_N

(* @NESTEDCOMMENTS := 'Yes' *)
(* @PATH := '\/Engineering\/signal processing' *)
(* @OBJECTFLAGS := '0, 8' *)
(* @SYMFILEFLAGS := '2048' *)
FUNCTION_BLOCK AIN2
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

Ain2 converts positiv and negativ 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-out_max; 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

pd 16 dec 2012
   added 2 complement and proposition to remove max code
   *)
END_FUNCTION_BLOCK