AnnexF/average_st.txt
author mjsousa
Sun, 19 Oct 2014 08:36:49 +0100
changeset 936 0f7bcc160568
parent 0 fb772792efd1
permissions -rwxr-xr-x
Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
Note that multiple de-referencing of structs (struct_ptr_ptr^^.elem) is not supported inside FB code (this would need BIG changes to the compiler!)
 FUNCTION_BLOCK AVERAGE
   VAR_INPUT
     RUN : BOOL ;      (* 1 = run, 0 = reset *)
     XIN : REAL ;     (* Input variable *)
     N   : INT ;      (* 0 <= N < 128 or manufacturer- *)
   END_VAR            (*      specified maximum value  *)
   VAR_OUTPUT XOUT : REAL ; END_VAR (* Averaged output *)
   VAR SUM  : REAL := 0.0; (* Running sum *)
       FIFO : DELAY ;      (* N-Element FIFO *)
   END_VAR
   SUM := SUM - FIFO.XOUT ;
   FIFO (RUN := RUN , XIN := XIN, N := N) ;
   SUM := SUM + FIFO.XOUT ;
   IF RUN THEN XOUT := SUM/N ;
   ELSE SUM := N*XIN ; XOUT := XIN ;
   END_IF ;
 END_FUNCTION_BLOCK