Introduction
An up-down counter helps count events going up or down, like counting items on a conveyor belt or tracking button presses.
COUNTER_NAME: CTUD (CU, CD, R, PV, CV, QU, QD); Where: CU = Count Up input (boolean) CD = Count Down input (boolean) R = Reset input (boolean) PV = Preset Value (integer) CV = Current Value (integer, output) QU = Count Up done output (boolean) QD = Count Down done output (boolean)
MyCounter: CTUD(CU:=StartButton, CD:=StopButton, R:=ResetButton, PV:=10, CV=>CurrentCount, QU=>CountUpDone, QD=>CountDownDone);Counter1: CTUD(CU:=Sensor1, CD:=Sensor2, R:=Reset, PV:=100, CV=>CountValue, QU=>UpDone, QD=>DownDone);PROGRAM UpDownCounterExample VAR StartButton : BOOL := FALSE; StopButton : BOOL := FALSE; ResetButton : BOOL := FALSE; CurrentCount : INT := 0; CountUpDone : BOOL := FALSE; CountDownDone : BOOL := FALSE; END_VAR // Define the counter MyCounter: CTUD(CU:=StartButton, CD:=StopButton, R:=ResetButton, PV:=5, CV=>CurrentCount, QU=>CountUpDone, QD=>CountDownDone); // Simulate button presses // Step 1: Press StartButton to count up StartButton := TRUE; // Call counter logic // MyCounter(); // Removed as CTUD is a function block, not a callable function StartButton := FALSE; // MyCounter(); // Step 2: Press StartButton again StartButton := TRUE; // MyCounter(); StartButton := FALSE; // MyCounter(); // Step 3: Press StopButton to count down StopButton := TRUE; // MyCounter(); StopButton := FALSE; // MyCounter(); // Step 4: Press ResetButton to reset counter ResetButton := TRUE; // MyCounter(); ResetButton := FALSE; // MyCounter(); // Output current count END_PROGRAM
up-down counter in PLC programming?CU = 1 (pulse)
CD = 0
R = 0
Initial count = 3
Limit = 5
CU = 0
CD = 1 (pulse)
R = 1 (pulse)
Initial count = 2
Limit = 5
Pass the test and this pattern moves unknown