RSLogix 500 Program Problem

Archie said:
I was working on a message rotator in a SLC 5/04 when I came into a problem that seemed like it would be very easy, but turned out to be quite tricky in ladder.

I have a word (B3:0) that will have a single bit active and I need to find which corresponding bit number is active and store it in N7:0. For example

If B3:0 = 0000000000000001 , then N7:0 should be 0
If B3:0 = 0000000000000010 , then N7:0 should be 1
If B3:0 = 1000000000000000 , then N7:0 should be 15

A simple LOG base 2 would solve it easily, but I could only find a LN and LOG base 10 in the instruction set. I am thinking there is a very simple solution that evades me.


...... OK, just as I finished typing this I came up with the answer, but I would still be interested in seeing how others solve it.


I'm literally floored that no one has yet suggested that you use the ENC (encode) instruction. The instruction is available in RSLogix500 and it does exactly what you are describing.

But, since using the forum search has come up, I have posted on this topic several times and shown dirrerent ways of doing this and also what to do to make sure only one bit is set in the word.

To calculate log base two of a number use
X = LN(Y)/LN(2)
or save a step and use
X = LN(Y)/.69314718
However, that is a math intensive method.

But right here I showed another siimple way of computing log base 2 of an integer using a couple of bitwise AND and OR instructions.
http://www.plctalk.net/qanda/showpost.php?p=140998&postcount=6
 
Last edited:
Didn't think about the ENC instruction. The only disadvantage to it would be finding multiple bits that are active.

I think there is much more clarity in troubleshooting either the ENC or FBC instruction compared to the LOG equations. We're talking about a message display...a technician goes to figure out the root cause of message "7" Which is going to be easier on him. Running into the equation or running into a ENC/FBC instruction?
 
Or read B3:0 directly

B3:0/0 = 1
B3:0/1 = 2
B3:0/2 = 4
B3:0/3 = 8
B3:0/4 = 16
B3:0/5 = 32
B3:0/6 = 64
B3:0/7 = 128
B3:0/8 = 256
B3:0/9 = 512
B3:0/10 = 1024
B3:0/11 = 2048
B3:0/12 = 4096
B3:0/13 = 8192
B3:0/14 = 16384
B3:0/15 = 32768
 
I give up. You can lead a horse to water....

I think bubba would just us a mask move into N7:0 ??

B3:0/0 = 1
B3:0/1 = 2
B3:0/2 = 4
B3:0/3 = 8
B3:0/4 = 16
B3:0/5 = 32
B3:0/6 = 64
B3:0/7 = 128
B3:0/8 = 256
B3:0/9 = 512
B3:0/10 = 1024
B3:0/11 = 2048
B3:0/12 = 4096
B3:0/13 = 8192
B3:0/14 = 16384
B3:0/15 = 32768
TW, you should have quit while you were ahead.
 
Peter Nachtwey said:
TW, you should have quit while you were ahead.
I'm not following you Peter. How could that be harder for a tech to follow than an equation. Just read the actual integer, B3:0, as a status code is what I'm saying
 
Archie said:
I was working on a message rotator in a SLC 5/04 when I came into a problem that seemed like it would be very easy, but turned out to be quite tricky in ladder.

I have a word (B3:0) that will have a single bit active and I need to find which corresponding bit number is active and store it in N7:0. For example
Ok Peter, I think I see what your talking about Peter. I got off track thinking of a status code for a message display. Archie never says that. He is taking a binary position and turning it into a number. I got off on a tangent :)

The ENC or FBC are the best solutions I see
 
Alaric said:
I'm literally floored that no one has yet suggested that you use the ENC (encode) instruction. The instruction is available in RSLogix500 and it does exactly what you are describing.

I learned something new! The ENC was exactly what I needed. (At the time anyway. I completely went down a different path to solve the message rotator)

I was going to start a new thread, but since we have discussed bit positions and searching, this will make a good place to bring it up.

My orginal problem was that I needed to look at which bits were true in a gorup of 5 words and generate a number for each bit, wait 3 seconds, then find the next true bit number.

For example, lets just look at one word:

B3:0 = 0000000000001010

N7:0 would =1 for 3 seconds, then it would go to 3 for 3 seconds, then back to 1.

This has to span 5 words. If B3:1=0000000000000001, then N7:0 would hold the value 16 for 3 seconds.

I came up with a solution that works, but I am not happy with it. I am sure this has been discussed many times, but my forum searches have not turned up a satisfying solution. If this has been discussed, then I would be apreciative if someone can give me links to the threads.
 
Last edited:
Archie said:
I learned something new! The ENC was exactly what I needed. (At the time anyway. I completely went down a different path to solve the message rotator)

I was going to start a new thread, but since we have discussed bit positions and searching, this will make a good place to bring it up.

My orginal problem was that I needed to look at which bits were true in a gorup of 5 words and generate a number for each bit, wait 3 seconds, then find the next true bit number.

For example, lets just look at one word:

B3:0 = 0000000000001010

N7:0 would =1 for 3 seconds, then it would go to 3 for 3 seconds, then back to 1.

This has to span 5 words. If B3:1=0000000000000001, then N7:0 would hold the value 16 for 3 seconds.

I came up with a solution that works, but I am not happy with it. I am sure this has been discussed many times, but my forum searches have not turned up a satisfying solution. If this has been discussed, then I would be apreciative if someone can give me links to the threads.

You can use a combination of the ENC, DCD, or bitwise AND, NOT and XOR instruction to step through the bits. ENC returns the position of the least significant bit. Use DCD to create a word to XOR with the original to mask the least significant bit so you can use ENC to get the next least significant bit.

Bitwise, X & NOT(X-1) returns a number with only the least significant bit set.
1011 & NOT(1011-1) = 1011 & NOT(1010) = 1011 & 0101 = 0001

You can XOR that result with the original to get a value where the least significant bit is cleared but all the other bits are preserved.

Combine those and a few other tricks and I think you'll come up with something you are happy with. Just remember that it should be something Bubba can understand.

Here is a link to a web page full of various bit tricks.
 
Last edited:
GIT, Instead of taking a picture and developing the film, try this:

printscreenpicture.gif
 

Similar Topics

Hi I have a machine which has recipes in the plc ,using indirect addressing, we have a screen where we enter the recipes and then save the recipe...
Replies
23
Views
4,137
Hi guys I have a machine that works fine, but today we had an issue where the operator speeds the line up to 20cpm the feeder but didnt adjust...
Replies
11
Views
3,173
Is there a way to do a mass copy of comments and tag names from one program to another? They are 100% identical, one goes online (uploaded from...
Replies
1
Views
3,148
I was trying to modify a program online the other day using RSLogix 500 . The PLC is a SLC5/05. I was online with the PLC, and could change...
Replies
2
Views
5,480
I had an idea to improve some of my standard programs, and make them easier to modify/expand in the future. Is there a way to address the current...
Replies
9
Views
3,670
Back
Top Bottom