Structured Text Increment Integer

I have never seen a situation where the ladder code for an entire routine is objectively simpler than the equivalent structured text code. However, I regularly see situations where the structured text code is objectively simpler than the equivalent ladder. In fact, the compare and compute functions that most ladder editors provide is just a way to inject structured text to simplify situations that would otherwise be comically complex in ladder.

Take this thread's example. In ST, this is a simple if statement check for a falling edge by some means, then adding 1 if the if statement is true. Depending on the platform, you could use EDGENEG(), F_TRIG(), OSFI() (this is a falling edge one-shot in Studio5k), etc. I'll show EDGENEG() since it's an operator instead of a function and so simpler.

Code:
IF(EDGENEG(MachineState)) THEN 
    DownCount:= DownCount + 1;
END_IF

More platforms would probably use F_TRIG() which adds one or two lines to call the F_TRIG and is functionally more identical to the typical ladder solution

Code:
MyFTrig.CLK:= MachineState;
MyFTrig();     //alternatively combine these two lines into MyFTrig(CLK:= MachineState);, but I think that's harder to look at
IF(MyFTrig.Q) THEN 
    DownCount:= DownCount + 1;
END_IF

I suppose you could use a count up type function in ST too

Code:
MyFTrig.CLK:= MachineState;
MyFTrig();     //alternatively combine these two lines into MyFTrig(CLK:= MachineState);, but I think that's harder to look at
MyCountUp.CLK:= MyFTrig.Q;
MyCountUp.pValue:= ADR(DownCount);
MyCountUp()

That could be compressed to this:

Code:
MyFTrig(CLK:= MachineState);
MyCountUp(MyCountUp.CLK:= MyFTrig.Q, MyCountUp.pValue:= ADR(DownCount));

In ladder, most would probably do this sort of thing:

Code:
MachineState      SomeRandomBit        DownCount
----]\[-------------[ONS]-----------------[CTU]---

Both ST and Ladder in this case are super simple and easy to understand. The ladder rung will probably take up 20% of your vertical screen space while the ST is 3 to 5 lines out of a typical 50 displayable would be about 10%. As soon as you start complicating things in the condition, ST starts winning out as the ladder takes more and more vertical space until a single rung won't even fit on your screen while the ST might balloon to 15 lines and still fit comfortably on the screen.
 
Last edited:

Similar Topics

I have an expression in a structured text routine of a Logix controller that looks more or less like the following: ResultInteger := Integer1 *...
Replies
13
Views
374
Good evening. I display the step number of a SFC on a display. Sometimes, on a trip, it goes quickly through many steps and I need to prove to...
Replies
1
Views
111
I am trying to set up a piece of equipment with a Horner HE-X4R. I'd like to use structured text and so far I'm just trying to get a basic On/off...
Replies
0
Views
66
Good morning. I'm doing a rehab and I need to recycle some part of the old code that won't change and that I need. This is a calculation that...
Replies
22
Views
1,339
I'm writing some structured text that's handling a data structure that comes from a PC. The PC structure is in the "new" LREAL 64-bit floating...
Replies
3
Views
481
Back
Top Bottom