For and While in Studio 5000 Ladder?

TheWaterboy

Lifetime Supporting Member + Moderator
Join Date
May 2006
Location
-27.9679796,153.419016
Posts
1,927
Can the following be made into a SINGLE Ladder so it could be incorporated with more ladder code and then made into an AOI?
I have been trying and can't figure out how.
The FOR instruction calls a separate routine so that's out since an AOI can only have one routine. I can't devise an alternative method in ladder.

Code:
// Counts Alarm Bits
AlarmBitsTrue:=0;
For I := 0 to (NumOfArrayWords-1) DO
   Element :=Alarm_Buffer[I];
	While (Element <> 0) do
		Element := (Element & (Element -1)); 
		AlarmBitsTrue := AlarmBitsTrue +1;
	end_while; 
end_for;
	BitCount :=	AlarmBitsTrue;
If (BitCount > 0) then
	AlarmPresent:=1;
else
	AlarmPresent:=0;
end_if;
 
You're not going to do nested loops in an AOI. It looks like you're trying to count the total number of bits that are high in an array of words.

Do a FAL in INC mode, to move each array element into a temp word each scan. Then a FBC instruction on that temp word to give you the number of high bits in that word. Accumulate the result until the FAL completes.
 
How's this? Note: This is using v36 and it's associated "updated" instruction mnemonics.

Screenshot 2024-02-15 at 1.13.18&#8239;pm.jpg
 
FOR WHILE loops in ladder (JMP-LBL) can be coded but its frowned on because it extends the program scan time and can potentially trip watchdog timers.

When need necessitates it, I'll code it so that the logic in the loop is scanned once every program scan.
 
All
Sincere thinks for all the help,

@ASF - Thank you! I will try this ladder today and see how that works.

@destination unknown - Because I need it all in one AOI and you can only have ONE routine in a AOI

@jstolaruk - I was aware the JMP/LBL is not ideal but :
  1. The FOR operator wasn't a option
  2. The N & (N-1) test is REALLY fast for counting bits arranged like this.

@robertmee - I tried the FAL idea after a suggestion from another group but the scan time was incredibly high compared to the ST code. FAL took mS to complete while the ST code took µS. If the ladder doesn't work out I will revisit it and see if can optimize it.

@robertmee - Why can't I do nested loops in an AOI?

@cheeseface - It's not my first choice to use 2 AOIs for this as they will always be needed together. If I could embed an AOI in another AOI then maybe.

I don't really NEED to count the bits here, but it is a simple matter to do so and that might be handy to have at some future point when I want to nag a user that they have 10 alarms set and might want to get off their phone :)
 
I'll try again but the test array is a DINT[4] so .LEN = 127 assuming it works on contiguous memory .

It doesn't make sense it took so much longer since its purpose built top do just this, but it did when I last tried it.
 
How's this? Note: This is using v36 and it's associated "updated" instruction mnemonics.

It almost works, but it stops counting on the first DINT that has no bits set.

i.e. if I set bits in array[1], array[2] and array[3], it doesn't count them until I set a bit in array[0], then it counts them all. I think I know why, working through it...

This seems to work ...
fixed.jpg
 
Like Destination_Unknown said, if you are just looking at 4 DINTS, then perhaps the brute force approach of 129 line is the best:

Count=0
XIC Dint[0].0 count=count+1
XIC Dint[0].1 count=count+1

etc.
XIC Dint[3].31 count=count+1
 
I don't think it's the problem, but I am pretty sure that the EQU Element 0 on the lower branch is unnecessary.

Also, a value of -32768 in Element causes an underflow, if that causes a trap and/or PLC fault you might need to guard against it.

Never mind; those are probably unsigned integers?
 
Last edited:

Similar Topics

After migrating a SLC to a 1769-L36ERM, the Studio 5000 interface was almost unusable. I started with v24, and then changed to v30 with no...
Replies
8
Views
6,419
Hi guys, To make a short story of my problem, We had studio view 6.000 here installed on our laptop and alot of the automation have been done...
Replies
1
Views
2,536
Hello The plant is running and there is no shutdown nowadays therefore I can add 1734- AENTR and its card while PLC is in Run? I do not wanna...
Replies
8
Views
352
I have an issue with Power Flex 525 during running processing, the VFD stopped suddenly while the PLC and VFD connection ok, VFD does not have any...
Replies
1
Views
130
Hello! When trying to load the hardware configuration I get error 0050-133 2 2458, check the diagnostic buffer. When checking the buffer, it says...
Replies
4
Views
188
Back
Top Bottom