Introduction
Counters help keep track of how many items are made. They make sure we know the total count without mistakes.
COUNTER_NAME: CTU; PV: preset_value; CU: count_up_input; Q: output_done; CV: current_value;
Counter1: CTU;
Counter1.PV := 100;
Counter1.CU := SensorInput;
IF Counter1.Q THEN
// Do something
END_IF;CounterBelt: CTU; CounterBelt.PV := 50; CounterBelt.CU := ConveyorSensor; // Reset counter when needed IF ResetButton THEN CounterBelt.CV := 0; END_IF;
PROGRAM ProductionCounter VAR ItemCounter: CTU; SensorInput: BOOL := FALSE; ResetButton: BOOL := FALSE; i: INT; END_VAR // Set target count to 5 ItemCounter.PV := 5; // Simulate 7 items passing FOR i := 1 TO 7 DO SensorInput := TRUE; ItemCounter.CU := SensorInput; // Normally, CU triggers on rising edge, here simplified ItemCounter.CV := ItemCounter.CV + 1; SensorInput := FALSE; END_FOR // Check if target reached IF ItemCounter.CV >= ItemCounter.PV THEN // Output true when count reaches preset ItemCounter.Q := TRUE; ELSE ItemCounter.Q := FALSE; END_IF; // Print results WriteLn('Items counted: ' + INT_TO_STRING(ItemCounter.CV)); WriteLn('Target reached: ' + BOOL_TO_STRING(ItemCounter.Q)); END_PROGRAM
COUNTER C1; C1.PRE = 10; C1.CU = TRUE; C1.ACC = 7; C1.CU = FALSE; C1.CU = TRUE;
C1.ACC after running this code?COUNTER C1; C1.PRE = 5; C1.CU = TRUE; C1.CU = TRUE; C1.CU = FALSE; C1.CU = TRUE;
Pass the test and this pattern moves unknown