DATA_BLOCK DB 1
TITLE =
AUTHOR : Xaadem
VERSION : 0.1


  STRUCT 	
   OUT_FIL : REAL  := 2.000000e+001;	//Filter OUTPUT
  END_STRUCT ;	
BEGIN
   OUT_FIL := 2.000000e+001; 
END_DATA_BLOCK

FUNCTION FC 170 : WORD
TITLE =SCALING VALUES
//
AUTHOR : SEA
FAMILY : CONVERT
NAME : SCALE
VERSION : 2.0


VAR_INPUT
  IN : INT ;	// input value to be scaled
  HI_LIM : REAL ;	// upper limit in engineering units
  LO_LIM : REAL ;	// lower limit in engineering units 
  BIPOLAR : BOOL ;	// 1=bipolar; 0=unipolar
END_VAR
VAR_OUTPUT
  OUT : REAL ;	// result of the scale conversion
END_VAR
VAR_TEMP
  IN_REAL : REAL ;	// input value as a REAL number
  K1 : REAL ;	// low limit for input value
  K2 : REAL ;	// high limit for input value
  SPAN : REAL ;	// HI_LIM - LO_LIM
  TEMP1 : REAL ;	// temporary result
END_VAR
BEGIN
NETWORK
TITLE =
//
// set K1 and K2 constants based upon BIPOLAR
//
      SET   ; // if(BIPOLAR=0)
      A     #BIPOLAR; //   .
      JC    EL01; // {
      L     0.000000e+000; //   K1=0
      T     #K1; //     .
      JU    EI01; // } else {
EL01: L     -2.764800e+004; //   K1=-27648.0
      T     #K1; //     .
EI01: NOP   0; // }
      L     2.764800e+004; // K2=+27648.0
      T     #K2; //   .
//
// convert input (IN) to real
//
      L     #IN; // ACC1=IN
      ITD   ; // convert to double integer 
      DTR   ; // convert to real
      T     #IN_REAL; // IN_REAL-IN as a real
//
// determine SPAN = HI_LIM - LO_LIM
//
      L     #HI_LIM; // SPAN=HI_LIM-LO_LIM
      L     #LO_LIM; // .
      -R    ; // .
      T     #SPAN; // .
//
// If the input value is outside the K1 and K2 range, the output
// is clamped to the nearer of either the LO_LIM or the HI_LIM 
// and an error is logged. If the input value is exactly at a limit the
// output will be set to the computed limit with no error returned.
//
      L     #IN_REAL; // if(IN_REAL<K1)
      L     #K1; //   .
      >=R   ; //   .
      JC    EL02; // {
      L     8; //   error
      T     #RET_VAL; //     .
      L     #SPAN; //   if(SPAN<0)
      L     0.000000e+000; //     .
      <R    ; //     .
      JCN   EL03; //   {
      L     #HI_LIM; //     ACC1=HI_LIM
      JU    EI03; //   } else {
EL03: L     #LO_LIM; //     ACC1=LO_LIM
EI03: NOP   0; //   } 
      T     #OUT; //   OUT=ACC1
      JU    FAIL; //   error
EL02: POP   ; // } else {
      L     #K2; //   if(IN_REAL>K2)
      <=R   ; //     .
      JC    EI04; //   {
      L     8; //     error
      T     #RET_VAL; //       .
      L     #SPAN; //     if(SPAN<0)
      L     0.000000e+000; //     {
      <R    ; //         .
      JCN   EL05; //         .
      L     #LO_LIM; //       ACC1=LO_LIM
      JU    EI05; //     } else {
EL05: L     #HI_LIM; //       ACC1=HI_LIM
EI05: NOP   0; //     } 
      T     #OUT; //     OUT=ACC1
      JU    FAIL; //     error
EI04: NOP   0; //   }
      NOP   0; // }
//
// scale the input
//
      L     #K2; // TEMP1=K2-K1
      L     #K1; //   .
      -R    ; //   .
      T     #TEMP1; //   .
      L     #IN_REAL; // IN_REAL-K1
      L     #K1; //   .
      -R    ; //   .
      L     #TEMP1; // divide by TEMP1
      /R    ; //   .
      L     #SPAN; // multiply by SPAN
      *R    ; //   .
      L     #LO_LIM; // add LO_LIM
      +R    ; //   .
      T     #OUT; // OUT=scale(IN_REAL)
//
// set BR bit : no error-set BR bit to 1; with error-set BR bit to 0.
//
      L     0; // return error code 0
      T     #RET_VAL; //
      SET   ; // RLO = 1 (NO ERROR)
      JU    SVBR; // 
FAIL: CLR   ; // RLO = 0 (ERROR)
SVBR: SAVE  ; // BR = RLO
END_FUNCTION

FUNCTION FC 185 : VOID
TITLE =F I L T E R    
{ Know_How := '1' }
AUTHOR : Xaadem
NAME : FILTER
VERSION : 0.1


VAR_INPUT
  XE : REAL ;	//Input Word
  FAC_PERCENT : REAL ;	//Smoothing factor in %
  INIT : BOOL ;	//Initialisation flag, should be set during CPU start-up
END_VAR
VAR_OUTPUT
  XA : REAL ;	//Output of measuring value which is smoothed
END_VAR
VAR_IN_OUT
  AUX : REAL ;	//Auxiliary word
END_VAR
VAR_TEMP
  MD232 : REAL ;	//Temporary value
  MD204 : REAL ;	//Temporary value
  MD208 : REAL ;	//Temporary value
  MD212 : REAL ;	//Temporary value
  MD216 : REAL ;	//Temporary value
  MD220 : REAL ;	//Temporary value
  AUX1 : REAL ;	//Temporary value
END_VAR
BEGIN
NETWORK
TITLE =read XE input

      L     #XE; 
      T     #MD232; 
      NOP   0; 
NETWORK
TITLE =

      AN    #INIT; 
      JC    M001; 
NETWORK
TITLE =Initialisation !!

M003: L     #XE; 
      T     #XA; 
      NOP   0; 
NETWORK
TITLE =Initialisation !!

      L     #MD232; 
      T     #AUX; 
      NOP   0; 
NETWORK
TITLE =Jump to the end

      A     M     20.1; 
      JC    M002; 
NETWORK
TITLE =Old and new value are equal

M001: A(    ; 
      L     #MD232; 
      L     #AUX; 
      ==R   ; 
      )     ; 
      JNB   _001; 
      L     #MD232; 
      T     #XA; 
_001: NOP   0; 
NETWORK
TITLE =Old and new value are equal

      L     #MD232; 
      L     #AUX; 
      ==R   ; 
      SAVE  ; 
      BEC   ; 
NETWORK
TITLE =If Fac_Prozent > 0 ==> XA=Xe

      L     #FAC_PERCENT; 
      L     0.000000e+000; 
      <R    ; 
      JC    M003; 
NETWORK
TITLE =If Fac_Prozent > 100 ==> XA=Xe

      L     #FAC_PERCENT; 
      L     1.000000e+002; 
      >R    ; 
      JC    M003; 
NETWORK
TITLE =B=XE-XA

      L     #MD232; 
      L     #AUX; 
      -R    ; 
      T     #MD204; 
      NOP   0; 
NETWORK
TITLE =Move factor into temporary word

      L     #FAC_PERCENT; 
      T     #MD212; 
      NOP   0; 
NETWORK
TITLE =B*Factor

      L     #MD212; 
      L     #MD204; 
      *R    ; 
      T     #MD216; 
      NOP   0; 
NETWORK
TITLE =B*Factor / 100

      L     #MD216; 
      L     1.000000e+002; 
      /R    ; 
      T     #MD220; 
      NOP   0; 
NETWORK
TITLE =XANew = AUX+(B*Factor% / 100)

      L     #AUX; 
      L     #MD220; 
      +R    ; 
      T     #AUX; 
      NOP   0; 
NETWORK
TITLE =XAnew = AUXnew

      L     #AUX; 
      T     #XA; 
      NOP   0; 
NETWORK
TITLE =End of block

M002: A     M     20.1; 
      SAVE  ; 
      BEC   ; 
END_FUNCTION

ORGANIZATION_BLOCK OB 1
TITLE = "Main Program Sweep (Cycle)"
AUTHOR : Xaadem
VERSION : 0.1


VAR_TEMP
  OB1_EV_CLASS : BYTE ;	//Bits 0-3 = 1 (Coming event), Bits 4-7 = 1 (Event class 1)
  OB1_SCAN_1 : BYTE ;	//1 (Cold restart scan 1 of OB 1), 3 (Scan 2-n of OB 1)
  OB1_PRIORITY : BYTE ;	//Priority of OB Execution
  OB1_OB_NUMBR : BYTE ;	//1 (Organization block 1, OB1)
  OB1_RESERVED_1 : BYTE ;	//Reserved for system
  OB1_RESERVED_2 : BYTE ;	//Reserved for system
  OB1_PREV_CYCLE : INT ;	//Cycle time of previous OB1 scan (milliseconds)
  OB1_MIN_CYCLE : INT ;	//Minimum cycle time of OB1 (milliseconds)
  OB1_MAX_CYCLE : INT ;	//Maximum cycle time of OB1 (milliseconds)
  OB1_DATE_TIME : DATE_AND_TIME ;	//Date and time OB1 started
  Value : REAL ;	
  AIXX : REAL ;	
  OUTfilter : REAL ;	
END_VAR
BEGIN
NETWORK
TITLE =

      A     M      0.0; 
      =     L     32.0; 
      BLD   103; 
      CALL FC   170 (
           IN                       := IW   512,
           HI_LIM                   := 1.000000e+005,
           LO_LIM                   := 0.000000e+000,
           BIPOLAR                  := L     32.0,
           RET_VAL                  := MW     2,
           OUT                      := #Value);
      NOP   0; 
NETWORK
TITLE =

      A     M     20.0; 
      =     L     32.0; 
      BLD   103; 
      CALL FC   185 (
           XE                       := #Value,
           FAC_PERCENT              := 9.000000e+001,
           INIT                     := L     32.0,
           XA                       := #OUTfilter,
           AUX                      := #AIXX);
      NOP   0; 
NETWORK
TITLE =

      L     #OUTfilter; 
      L     2.000000e+001; 
      +R    ; 
      T     DB1.DBD    0; 
      NOP   0; 
END_ORGANIZATION_BLOCK

