Codes Ys

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Part 1

PROGRAM MAIN
VAR
timer: TP;
result: BOOL;
initFlag: BOOL;
END_VAR
VAR_INPUT
in1 AT %IX1.0: BOOL;
END_VAR
VAR_OUTPUT
out1 AT %QX1.0: BOOL;
END_VAR

(* =============================================== *)

IF (in1 = TRUE) THEN


timer.PT := t#2s;
timer(IN:=TRUE);
initFlag:= TRUE;
END_IF

IF (initFlag = TRUE) THEN


timer();
END_IF

IF (initFlag = TRUE AND timer.Q = FALSE) THEN


result :=NOT(result);
out1 := result;

timer(IN:=FALSE);
timer(IN:=TRUE);
END_IF
IL:

LD TRUE
AND in1
ST timer.IN
CAL timer(PT:=T#5000ms)
LD timer.Q
ST out1

Function-block header:

bLED : ARRAY [1..6] OF BOOL ;


bLEDButton : ARRAY [1..6] OF BOOL;
tTimersOn : ARRAY [1..6] OF TON := [(PT := T#1S)];
tTimersOff : ARRAY [1..6] OF TON := [(PT := T#1S)];
fbLEDButtonTimerReset : ARRAY [1..6] OF R_TRIG;
fbLEDOnReset : ARRAY [1..6] OF R_TRIG;
fbLEDOffReset : ARRAY [1..6] OF R_TRIG;
Function block body

fbLEDButtonTimerReset[1](CLK := bLEDButton[1]);
IF fbLEDButtonTimerReset[1].Q THEN
tTimersOn[1](IN := FALSE);
tTimersOn[1].IN := TRUE;
tTimersOff[1](IN := FALSE);
END_IF

IF bLEDButton[1] THEN
tTimersOn[1]();
tTimersOff[1]();
fbLEDOffReset[1](CLK := tTimersOff[1].Q);
IF fbLEDOffReset[1].Q THEN
tTimersOn[1](IN := FALSE);
tTimersOn[1].IN := TRUE;
END_IF
fbLEDOnReset[1](CLK := tTimersOn[1].Q);
IF fbLEDOnReset[1].Q THEN
tTimersOff[1](IN := FALSE);
tTimersOff[1].IN := TRUE;
END_IF

bLED[1] := NOT tTimersOn[1].Q;


END_IF

PROGRAM PLC_PRG
VAR
xLed1 : ARRAY [1..6] OF BOOL; (* Array of LEDs *)
xLed1M : ARRAY [1..6] OF BOOL; (* Array of LEDs to remember what to blink*)
xLedButtons: ARRAY [1..6] OF BOOL; (* Array of buttons to toggle LED state *)
xLedButtonsM: ARRAY [1..6] OF BOOL; (* Array of button press memmories to use
pulse on rised edge of button press *)
i:INT;
xButtonBlink: BOOL; (* Button to press to start blinking *)
xButtonBlinkM: BOOL; (* Memory of the button state to detect raising edge *)
xBlinkLock: BOOL; (* Lock programm during blinking *)
xBlinkLockM: BOOL; (* Memory of the lock to detect raising and falling edge *)
fbTP1:TP; (* timer to enable lock for 6 seconds *)
fbBLINK1: BLINK;
END_VAR
(* Start lock timer of raising edge of blink button *)
fbTP1(IN := xButtonBlink AND NOT xButtonBlinkM, PT := T#6s);
xButtonBlinkM := xButtonBlink;

xBlinkLock := fbTP1.Q;

(* If locked start blinking *)


IF xBlinkLock THEN
(* If raised edge, of first cycle of blinking save LED array *)
IF NOT xBlinkLockM THEN
xLed1M := xLed1;
END_IF
fbBLINK1(ENABLE := TRUE, TIMEHIGH := T#1s, TIMELOW := T#1s);
FOR i :=1 TO 6 DO
IF xLed1M[i] THEN
xLed1[i] := fbBLINK1.OUT;
END_IF
END_FOR
ELSE
(* If falling edge of lock, restore LED array and reset blink FB *)
IF xBlinkLockM THEN
xLed1 := xLed1M;
fbBLINK1(ENABLE := FALSE);
END_IF

(* Track button clicks and toogle state of led *)


FOR i := 1 TO 6 DO
IF xLedButtons[i] AND NOT xLedButtonsM[i] THEN
xLed1[i] := NOT xLed1[i];
END_IF;
xLedButtonsM[i] := xLedButtons[i];
END_FOR
END_IF
xBlinkLockM := xBlinkLock;

You might also like