Counting bits in a word

fturner

Member
Join Date
Jun 2002
Location
LA Calif
Posts
4
Happy New Year all! Does anyone have a good method for counting the bits that are 1 (on) in a single word, using Rockwell RSLogix500. I have a method that works, but it seems more cumbersome than it should be. I know of at least one other manufacturer that has a specific instruction available for this task (Aromat), but I can't find anything like it for the processor I'm using (5/05). Thanks in advance.
 
Two methods come to mind.

The first method is to use a bit shift instruction. Inititially copy the entire word to a temporary register and initialize a counting register to zero, then procede to shift the bits in the temporary register out one at a time and examine the status of the bits leaving the function block. If the bit is true, increment a counter (I prefer to add a value of one to a register as opposed to using a counter since I don't have to worry about false to true transitions). This method will take several scans unless you use jumps to loop thru the code. If you do use jumps, don't forget to add an always false instruction to the bitshift function before you jump or it won't trigger (it needs to see a false to true transition).

The second method is a brute force method but is actually pretty efficient if you compare instruction execution times. Initialize your counting register to zero and then in the next 16 rungs use a normally open contact from each bit in the register to execute an ADD instruction that adds a value of one to your counting register. You will need 16 ADD instructions, one for each contact. This can be optimized if needed by putting this logic in a subroutine and only executing the logic if the word being evaluated is not equal to zero.
You could probably use indirect addressing and looping to simplify the amount of code, but if you are only checking 16 bits, keep the logic simple and easy to understand. And with cut and paste it doesn't take much time to program.
 
That's very nice, Allen!! Real compact and easy to grasp. I like it.
And if you can wait 16 scans for the result, you can take out the jump and label and put a GEQ( N7:0, 16) in series with the clear instructions. That will clear these registers only after all the bits are examined. Be sure to save N7:1 into another address before resetting, though.

Keith
 
FBC

It seems to me that the FBC instruction could be used to accomplish this. It could give the count AND tell you which bits are on in one single-scan operation.
 
Allen,

Could you repost your example with the Show Symbol & Address on please. I am missing something trying to understand your example. Or just post the file...


Thanks
John
 
Here it is with addresses and symbols:

[attachment]

The thing that might be confusing to you is the indirect address of the bit, which is N7:5/[N7:0], although using symbols can be entered as N7:5/[POINTER].



Gerry is right!!! FBC can be used to count all the bits in a word. I'd forgetten about that instruction, and have never used it that way before.

The code might look something like this:

 
R6:0.IN
--------+---{U} // SET THE BLOCK TO SCAN FOR ALL
| // MISMATCHES IN A SINGLE SCAN
| R6:0
+---(RES} // RESET THE SOURCE POINTER
|
| R6:1
+---(RES) // RESET THE RESULT POINTER


+----------- FBC ---+
---| SOURCE: #N7:5 | // SAME SOURCE AS IN MY EXAMPLE.
| REFERENCE: #N7:6 | // LOGIC MIGHT BE NEEDED TO ENSURE N7:6 IS ZERO.
| RESULT: #N10:0 |
| CONTROL: R6:0 |
| LENGTH: 16 | // INTERESTED IN 16 BITS IN SOURCE WORD.
| POSITION: 0 |
| CONTROL: R6:1 |
| LENGTH: 16 |
| POSITION: 0 | // THIS VALUE, R6:1.POS WILL CONTAIN
+-------------------+ // THE NUMBER OF BITS SET IN THE SOURCE WORD.


`
If you are interested in keeping track of which bits are set, then you'd need to do a FLL of N10:0 prior to executing the rung.

The only disadvantage of the FBC method is that you need to reserve one word of Result for each bit you are interested in counting, which increases your overhead.

And in the SLC, you can't have a Length longer than 128 (i.e., 8 words of Source bits), so that's another limitation.

bit_count.gif
 
Last edited:
Allen,

You were correct in your assumption that the indrirect addressing was the part that I did not know everything that I did not know about it yet.

I'll have to read up on it a bit...

Thanks

John
 
Allen,
Thank u for the code I was able to take a push from what u posted and improve some logic that has been causing some minor grief.
Had to compare 24 bit across 2 words then output the result to 2 output cards 1 bit at a time for voice messages on a conveyor.
Learnt lots about how indirect addressing works with Binary and Interger files.
Managed to fault the processor several times.
Have tried to attach the result of a planted seed.
 

Similar Topics

Hello! I have an application where I can only have 3 pumps running out of 6. I load the pumpRunning bits into an array. What I need to do is...
Replies
6
Views
3,163
Hello, I am in need of a solution to a problem I've failed to come up with a solution to that doesn't take entirely too long for my needs. I...
Replies
40
Views
15,771
This may be a foolish question, but is their a simple way to count the number of active bits in PLC-5? I can think of some complex ways to do...
Replies
8
Views
5,517
I'm having a mental block this morning. Want to do something very simple. I've got a number of enable bits in a byte and I've got a real which I...
Replies
3
Views
3,179
I have 4 bits B3:0/0 thru B3:0/3 that each represent a unit being on. I want to count the number of bits set to one and return an integer between...
Replies
2
Views
3,667
Back
Top Bottom