CTUD (Functionblock)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

ALL

Firmware version:

1.00 / 1.00.00


CTUD is an Up/Down counter. This function block is a mixture between the CTU and CTD function blocks. It combines the functionality of the CTU and CTD to form a flexible Up/Down counter.

 

This function block will increment the "cv" value with one on each rising edge on the "cu" input, and it will decrement "cv" with 1 when a leading edge on the "cu"  is detected. When the "cv" variable reaches "pv", or is above, the output, "qu" will go high - otherwise it will be low. When the "cv" variable reaches 0, the output "q" will go high - otherwise it will be low. If a high signal is present on the "r" input, the counter will be reset ("cv" will be set to 0). If a high signal is present on the "ld" input, the value from "pv" will be copied to "cv".

 

Input:

cu : BOOL (true/false)

On the leading edge of this input, the "cv" will be incremented with 1. When the "cv" value is equal to, or above, the value in "pv", "qu" will be high.

 

cd : BOOL (true/false)

On the leading edge of this input, the "cv" will be decremented with 1. When the "cv" value is equal to 0, "qd" will be high

 

ld : BOOL (true/false)

Load input. When this input is high, the "cv" will set to the value in "pv".

 

r : BOOL (true/false)

Reset input. When this input is high, the counter value will be kept at 0.

 

pv : INT (0..32767)

Preset value for the counter.

         

Output:

cv : INT (0..32767)

The current value of the counter.

 

qu : BOOL (true/false)

Output from the counter. See description for "cu".

 

qd : BOOL (true/false)

Output from the counter. See description for "cd".

 

Declaration:

FUNCTION_BLOCK CTUD;
VAR_INPUT
  cu : BOOL R_EDGE; | Signal that increments the counter by 1 on a rising edge
  cd : BOOL R_EDGE; | Signal that decrements the counter by 1 on a rising edge
  ld : BOOL;       | Signal that sets 'cv' to the value in 'pv' when high
  r : BOOL;       | Signal that sets 'cv' to 0 when high
  pv : INT;       | Preset value for the counter
END_VAR;
VAR_OUTPUT
  qu : BOOL;       | Output from counter
  qd : BOOL;       | Output from counter
  cv : INT;         | Current counter value          
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR_INPUT
  input1 : BOOL; | Input that will reset the counter
  input2 : BOOL; | Input that will increment the counter with one on a rising edge
  input3 : BOOL; | Input that will load the counter with the preset value
  input4 : BOOL; | Input that will decrement the counter with one on a rising edge
END_VAR;
 
VAR_OUTPUT
  signal_u : BOOL; | Output that will follow the 'qu' output from the counter
  signal_d : BOOL; | Output that will follow the 'qd' output from the counter
END_VAR;
 
VAR
  counter : CTUD; // Declare an instance of the CTUD functionblock
END_VAR;
 
PROGRAM test;
 
BEGIN
  counter(
          pv := 100,   // Preset value for the counter
          r := input1, // The counter will be reset when input1 is high
          cu := input2, // The counter will increment 'cv' with 1 when rising edge on input2
          ld := input3, // The counter will be loaded with the preset value when input3 is high
          cd := input4 // The counter will decrement 'cv' with 1 when rising edge on input4
        );
 
  signal_u:=counter.qu; // The two outputs from the counter are copied to signal_u and signal_d
  signal_d:=counter.qd;
END;
END_PROGRAM;