SLC Count Function

sidrvs

Member
Join Date
Feb 2010
Location
Miami
Posts
2
Any suggestions on how to increment a counter or other SLC instruction based on a string of integers being not equal to 0. I have determined a long way to do it but wanted to see if someone could help me streamline it.

Ex: N7:1 thru N7:40 <> 0
Increment the counter C5:x for each condition being true.

Thanks
 
If the SLC has indirect addressing then I woual set up a For/Next loop. Set a register to zero before the loop. Run the For/Next loop checking each location and incrementing the register for each greater than zero found. I wouldn't use a counter.
 
If the SLC has indirect addressing then I woual set up a For/Next loop. Set a register to zero before the loop. Run the For/Next loop checking each location and incrementing the register for each greater than zero found. I wouldn't use a counter.

Yup. That would be the most compact way, and assuming it's a 5/03 or up it has indirect addressing.

But what the hell, use the long method but put it into a subroutine. Same method as Bernie suggests - clear a register to zero as the first rung in the sub, then set up 40 branches that simply add "1" to the count register if the trigger is non-zero. At the end, you've got your count.

Granted it's not slick. But, in it's own sub, it's also not obtrusive in your code and you can ignore it. You can also use it more than once in the program if that's necessary.

Either way, definitely forget using a counter and just use another integer register to count up.
 
The brute force method (40 someting rungs) will execute much faster than a loop. Loops can be confusing to the novice who might not understand what is going on and add logic inside the loop, or mess with some part of it and cause an infinite loop (watchdog timeout).

If you are not concerned with those issues, then use a loop:
n=0
i=0
LBL 0
i=i+1
If N7:<>0 then n=n+1
if I<=40 JMP LBL 0

If you are concerned with the issues above, then add a subroutine and put forty compare rungs there as Paul T suggests. It will keep the calling program cleaner looking and cause only a tiny hit on scan time for the JSR.
 
Last edited:
The brute force method (40 someting rungs) will execute much faster than a loop. Loops can be confusing to the novice who might not understand what is going on and add logic inside the loop, or mess with some part of it and cause an infinite loop (watchdog timeout).

If you are not concerned with those issues, then use a loop:
n=0
i=0
LBL 0
i=i+1
If N7:<>0 then n=n+1
if I<=40 JMP LBL 0

If you are concerned with the issues above, then add a subroutine and put forty compare rungs there as Paul T suggests. It will keep the calling program cleaner looking and cause only a tiny hit on scan time for the JSR.


Yeah, I had wondered about the scan time issues for a loop too. I've rarely, rarely ever used a loop in a PLC program, so I don't have a feel for their impact on scan time.

I use indirects and indexed addresses as necessary, and I buffer IO as a matter of course. But I've never really done much with looping. I do tend to use a lot of subs, just to keep things cleaner and because once a sub is debugged, you can forget about it and move on to the next issue. Then, when you fix subsequent bugs it's very rare for the new bug "fix" to cause a problem in a previously debugged sub.

Edit - Bernie, was it you who showed a method for rapidly setting up a large number of similar rungs in Excel last week? This would fit that pretty well to create the 40 branches, or even 40 separate rungs.
 
Last edited:
One caution about using a counter. A CTU instruction is a transitional instruction. You can use a CTU instruction addressed to the same counter multiple times in a program, but only if you understand how it works and program accordingly. When a rung is false and a CTU instruction is executed it clears the .CU bit of the addressed counter and then does nothing else. When a rung is true and CTU exeuctes it first checks to see if the .CU bit is set. If it is set it does nothing. If the .CU bit is clear the instruction increments the .ACC of the addressed counter and then sets the .CU bit.

What this means in your application is that if you simply string together a bunch of CTU instructions and you have two or more consecutive non-zero integers the first one will be counted and then none of the ones after will be until after the next zero integer.

One way to get around this is to unconditionally unlatch the .CU bit after each instance of the CTU instruction. But an even better way would be to not use a counter and just ADD 1 to an integer.

As far as programming goes, I recommend just using 40 rungs in a subroutine and skip looping unless you are running out of program memory, which is not common.

Too bad the SLC doesn't support the FAL instruction.

A022610-1.jpg
 
Last edited:
No, I didn't show the multiple rung setup method though we have done it at work using Excel. We broke the rung up into 1 instruction per cell. Then used the auto increment on the relevant value. Then they were exported from Excel as space separated variables and re-imported into RSLogix. Or something like that. We had 50 rungs given that you can't index on strings. 50 EQU to a number and store into the appropriate string.
 

Similar Topics

I searched the threads and did not see an answer that applies to my question. The CTD for a SLC 500 and M 1000 counts down to -1 before the DN...
Replies
7
Views
6,803
Is there a quick way to determine this on an existing system using rslogix ? i have been looking but cant find anything?
Replies
6
Views
1,987
Hi Masters, I encountered a problem with Count Down of A-B SLC 5/01.Accum always be 0, but the Preset always be a plus. How the Accum can reach...
Replies
7
Views
7,204
I’m attempting to send a temperature from a SLC-5/02 to an EZiMarquee display. The vendor said to use a MSG instruction to send the data to the...
Replies
1
Views
81
Hello all. I have a few SLCs in my plant and of late we've seen a weird issue: The system will be running normally and then randomly the outputs...
Replies
2
Views
95
Back
Top Bottom