integer value = bits on

drewcrew6

Member
Join Date
Apr 2002
Location
allentown, Pa
Posts
418
Is there a clean way of taking an integer value of 0 to 15 and turning on the same bits?

if input value is 1 then bit 1 is on
if input value is 2 then bits 1+2 are on
if input value is 3 then bits 1,2 and 3 are on
""
""
""
if input value is 15 then all 15 bits are on

I've done it with a lookup table and also clearing the bits on change then using an add n7:0 + n7:0 + 1 = (bits to be on) for "input value" amount of times,starting with a 1 in n7:0.

They both seem sloppy for what I would like to do.


Thanks
Drewcrew6
 
By the use of N7:0 I'll assume an A-B processor. This logic is not necessarily workable, it's just a transcription of my quick scribble paper version.

RESULT_WD = N7:0
INT_VAL = N7:1 (the input data to be converted)
CONV_CMP = B3/0
STRT_OS = ANY
===============================================

STRT_OS CONV_CMP
--| |------------------+----(U)------
|
| CLR
+--+------------+
| |
| RESULT_WD |
| |
+------------+

CONV_CMP N7:RESULT_WD/[INT_VAL] R6:0/EN
--]/[-----------------]/[-------------(U)----

CONV_CMP BSL
--]/[----------------+------------------+
| FILE = RESULT_WD |
| CNTRL = R6:0 |
| BIT = B3/1 |
| LEN = 16 |
+------------------+

CONV_CMP N7:RESULT_WD/[INT_VAL] CONV_CMP
--]/[-----------------] [--------------(L)----



==================================================
Once the cycle starts, the BSL will continually transition from false to true and so shift a constant '1' in from the right. When the leading '1' hits the position in RESULT_WD designated by INT_VAL, the cycle stops and conversion is complete. It'll likely take some tweaking but, in principle it should work. I reckon 16 scans max to convert if INT_VAL = 15.
 
Matsubishi

Drewcrew6
Matsubishi have an "Deco" comande this comande decode the number in a register in another register it act the same way you ask it has this format
Deco Dx Mx n
Deco :Command
Dx :the register which contain the integer
Mx :The word in which the integer will be decoded into it's bits
n :the number of used words
Also As it seems to me that LG are following the steps of Matsubishi thier plc's also can do it.
I hope that could help.
THX
 
Steve Bailey said:
If n = the number in the register, 2^n - 1 gives you the bit pattern you want.

Pretty nifty technique, Steve. I'm a little suspicious though of how it would react when the exponent is 15 and the resulting value is 32768. When I can get reconnected to my 'bench' SLC I'll try this out.

From the SLC instruction help for XPY:

When an invalid (+ or -) floating point number is detected in the source, or an overflow or underflow condition occurs during calculation, the resulting destination is set to Not a Number and the overflow bits are set.
 
This works in a GE Fanuc Series 90-30.

|
|ALW_ON +-----+ +-----+
+--] [-----------+EXPT_+-----------------------+ SUB_+-
| | REAL| | REAL|
| | | | |
| CONST -+I1 Q+-%R0607 %R0607 -+I1 Q+-%R0607
| +2.000000 | | | |
| | | | |
| %R0605 -+I2 | CONST -+I2 |
| +-----+ +1.000000 +-----+
|
|
|
|ALW_ON +-----+ +-----+
+--] [-----------+REAL_+-----------------+ AND_+-
| | TO_ | | WORD|
| | DINT| | |
| %R0607 -+IN Q+-%R0609 %R0609 -+I1 Q+-%M0017
| +-----+ | |
| | |
| CONST -+I2 |
| FFFF +-----+

 
Thanks

A mistake in the original post it should have included all
16 bits (o-15).
So what about all 16 bits that a value is -1 or 65535 in unsigned.

Other then that it works perfect from the quick test.

Sorry yes it is an allen bradley.


Drewcrew6
 
Last edited:
drewcrew6 said:
Other then that it works perfect from the quick test.
What works?

Steve Bailey: I live in Integer land so I never considered using floats. I checked the 2^y method and it WILL fault the processor when y=15 and integers are used.
 
drewcrew6..

You need to explain a little thing, when you need it to set the bit pattern including bit 16: how are you thinking you would like to represent the value 1?
0000000000000010

or

0000000000000011



and how about 16?

1111111111111110

or

1111111111111111

Come up with the answer and I can amend the first program I posted to do as you like.
 
Okay here is what I'm looking for


int value-------binary result--------int result
0-------------0000000000000000-----------0
1-------------0000000000000001-----------1
2-------------0000000000000011-----------3
3-------------0000000000000111-----------7
4-------------0000000000001111----------15
5-------------0000000000011111----------31
6-------------0000000000111111----------63
7-------------0000000001111111---------127
8-------------0000000011111111---------255
9-------------0000000111111111---------511
10------------0000001111111111--------1023
11------------0000011111111111--------2047
12------------0000111111111111--------4095
13------------0001111111111111--------8191
14------------0011111111111111-------16383
15------------0111111111111111-------32767
16------------1111111111111111-------- -1


This is a personal thing so its not a big deal

I did not fault on my test rig just checked it again.

Hopefully this clears the confusion

Thanks

Drewcrew6
 
Amatures

I believe this is what you are looking for. 1 rung of logic.

BST DCD N9:6 N9:7 NXB SUB N9:7 1 N9:8 BND

I tested this on a SLC504.

Using a Decode function, DCD N9:6 (Source) into N9:7 (Dest)
Sub N9:7 minus 1 and there is your answer (N9:8)
 
Why use the lookup table?
Not that it saves any logic, but maybe you could have done this:

---- Input >= 1 ------(Output 1)
---- Input = -1 ---|

---- Input >= 2 ------(Output 2)
---- Input = -1 ---|
.
.
.
Etc.

15 copy/pastes later, you're in business.

AK
 
Re: Amatures

Mark Buskell said:
I believe this is what you are looking for. 1 rung of logic.

BST DCD N9:6 N9:7 NXB SUB N9:7 1 N9:8 BND

I tested this on a SLC504.

Using a Decode function, DCD N9:6 (Source) into N9:7 (Dest)
Sub N9:7 minus 1 and there is your answer (N9:8)

You are right and have found the best way, on a SLC anyway, but only because the SLC's DCD instructions uses only the 4 lsbs of the parameter.

akreel said:

Why use the lookup table?
Not that it saves any logic, but maybe you could have done this:
---- Input >= 1 ------(Output 1)
---- Input = -1 ---|

---- Input >= 2 ------(Output 2)
---- Input = -1 ---|
.
.
.
Etc.

15 copy/pastes later, you're in business.

I prefer short and sweet. Lots of rungs means lots of typing errors.
I would use Mark's version of Steves's alogrithm if the PLC supports it and the table if it doesn't.
 

Similar Topics

The analog array has 16 individual bits and each bit is an alarm. SCADA is having issues getting the BIT bool value. However they can get the...
Replies
9
Views
991
Hello, I need a little help. I’m looking for a way to program integer values to activate a bit for an alarm. for example... If any Integer[0] thru...
Replies
41
Views
7,856
Have beginner knowledge of digital controls, and intermediate/advanced knowledge of socket clients and listeners (written logging proxies, socket...
Replies
9
Views
3,726
Hi everyone :* I am using using unity pro v.13 i want to do the mathematical function of floor() which makes for example 1.2 to 1 and makes 2.9...
Replies
7
Views
3,042
Hi, Can someone help me explain in detail this integer Value. N7[4+((N151_0 AND -16)/16)].[N151_0 AND 15] Based on my understanding in the...
Replies
6
Views
2,579
Back
Top Bottom