Reputation: 119
I'm trying to create alternating outputs on my PLC (Mistubishi Melsec Q00UJCPU) in structured text. out1 and out2 - are outputs. IN1 - input.
Here's the code
IF IN1=TRUE THEN;
timer1(IN:= TRUE, PT:=T#0s , Q:=timer1.Q);
END_IF;
IF timer1.Q THEN;
out1:=FALSE;
out2:=TRUE;
timer1(IN:=FALSE, PT:=T#1s);
timer2(IN:= TRUE, PT:=T#500ms , Q:=timer2.Q);
END_IF;
IF timer2.Q THEN;
out2:=FALSE;
out1:=TRUE;
timer2(IN:=FALSE, PT:=T#1s);
timer1(IN:=TRUE , PT:=T#500ms , Q:=timer1.Q);
END_IF;
Same code works in Codesys, but is not working in GX Works2. What could be wrong with it? And are there so many differents in Codesys and GX Works ST programming? Thanks!
Upvotes: 0
Views: 1514
Reputation: 3080
Your code contain a lot of mistakes.
what is this Q:=timer1.Q
? First, you have to use =>
on output variables oа ф function block, and you cannot set output Q
of the timer to the same output Q
of the same timer.
When you do this
IF timer2.Q THEN
out2:=FALSE;
out1:=TRUE;
timer2(IN:=FALSE, PT:=T#1s);
timer1(IN:=TRUE , PT:=T#500ms);
END_IF;
Line timer1(IN:=TRUE , PT:=T#500ms);
will only work once because you turn timer2 off line before.
Here is how you can do alternating timer without even using timer with native code. Because Timers themselves written in ST.
FUNCTION_BLOCK ALTERNATOR
VAR_INPUT
IN: BOOL;
T1: TIME;
T2: TIME;
END_VAR
VAR_OUTPUT
Q1:BOOL;
Q2:BOOL;
END_VAR
VAR
tStart:TIME;
tET: TIME;
xM:BOOL;
END_VAR
IF IN THEN
IF NOT xM THEN
tStart := TIME();
END_IF
IF NOT Q1 AND NOT Q2 THEN
Q1 := TRUE;
END_IF
tET := TIME() - tStart;
IF Q1 AND tET >= T1 THEN
Q1 := FALSE;
Q2 := TRUE;
tStart := TIME();
tET := T#0s;
END_IF
IF Q2 AND tET >= T2 THEN
Q2 := FALSE;
Q1 := TRUE;
tStart := TIME();
tET := T#0s;
END_IF
ELSE
Q1 := FALSE;
Q2 := FALSE;
END_IF
xM := IN;
END_FUNCTION_BLOK
This is very promptly wцкшеутб am sure there is room for optimization of this code.
In the program you can use this alternation
PROGRAM PLC_PRG
VAR
fbT: ALTERNATOR;
xStart: BOOL;
xOut1: BOOL;
xOut2: BOOL;
END_VAR
fbT(IN := xStart, T1 := T#2s, T2:= T#1s, Q1 => xOut1, Q2 => xOut2);
END_PROGRAM
Upvotes: 1
Reputation: 2442
Is the GX Works2
IEC61131
? If so it should use the same standard as Codesys. Does the GX Works2 code compile? As pboedker said above your code is probably generating some compiler errors. Probably something like below would work better.
InstRTrig(clk:=IN1);
InstFTrig(clk:=IN1);
IF InstRTrig.Q THEN
timer1(IN:= TRUE, PT:=T#1s , Q:=timer1.Q);
END_IF;
IF InstFTrig.Q THEN
out1:=FALSE;
out2:=FALSE;
timer1(IN:=FALSE, PT:=T#1s);
timer2(IN:= FALSE, PT:=T#500ms);
END_IF;
IF timer1.Q THEN
out1:=FALSE;
out2:=TRUE;
timer1(IN:=FALSE, PT:=T#1s);
timer2(IN:= TRUE, PT:=T#500ms);
END_IF;
IF timer2.Q THEN
out2:=FALSE;
out1:=TRUE;
timer2(IN:=FALSE, PT:=T#1s);
timer1(IN:=TRUE , PT:=T#500ms);
END_IF;
Upvotes: 1