News:

  • June 26, 2026, 05:18:16 PM

Login with username, password and session length

Author Topic: input to binary  (Read 20486 times)

mhw

  • Hero Member
  • *****
  • Posts: 250
input to binary
« on: September 17, 2014, 12:53:39 PM »
I am wanting to read 4 physical inputs as a binary number. X0 on, X1 off, X2 off, X3 off = 8
Likewise with the outputs 2 = Y0 off, Y1 off, Y2 on, Y3 off.
How can I do this?

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Re: input to binary
« Reply #1 on: September 17, 2014, 01:35:20 PM »
There may be a more elegant way, but you could:
Code: [Select]
MOVE:
X0
V0:3

MOVE:
X1
V0:2

MOVE:
X2
V0:1

MOVE:
X3
V0:0
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3609
  • Darth Ladder
Re: input to binary
« Reply #2 on: September 17, 2014, 01:40:55 PM »
I think you can MOVER a bit range of a different size than the target, not sure.    If you can't, I'd do

MATH V0 X0:W & 15
« Last Edit: September 17, 2014, 05:35:26 PM by Controls Guy »
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Re: input to binary
« Reply #3 on: September 17, 2014, 01:57:01 PM »
I think you can MOVER a bit range of a different size than the target, not sure.
You can do that, but he is wanting X0 to be the MSB instead of the LSB. I figured that the Byte swap would make it more complicated than multiple MOVE's.   

Code: [Select]
MOVER:
Source: X0
Range: 4
Dest: V0:0

Quote
If you can't, I'd do MATH V0 X0:W AND 15
This sounds better, if he works out the byte swap (or re-wire it).
« Last Edit: September 17, 2014, 01:58:38 PM by plcnut »
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: input to binary
« Reply #4 on: September 17, 2014, 02:08:35 PM »
Inputs, use MATH by looking at X0..X7 as a UNSIGNED BYTE, X0:UB (note, you can use this in any instruction that takes a numeric value not just MATH, even in Data View or Trend View!).

MATH V100 "X0:UB & 0xF"
will read X0..X7 as unsigned BYTE then BIT-WISE-AND it with the hex value 0xF (or 0000_0000_0000_1111 in binary).  If you want another bit, just tweak the mask.  If you want more than 8 bits, cast as UNSIGNED WORD X0:UW.

Outputs are a little harder, because WRITING you have to maintain the state of the upper 4 bits when writing the lower 4 bits.  There are multiple ways to do this, but I would use MATH on Y0:UB

MATH Y0:UB "(Y0:UB & 0xF0) | (V101 & 0xF)"
where V101 has the 4 bit values we want to output in its 4 LSBits.  So we bitwise AND it with hex 0xF (V101 & 0xF).  So we mask of those values to make them the lower 4 bits of Y0:UB.  But we want to maintain the current 4 MSBits of Y0:UB (i.e. Y4..Y7), so we grab the 4 MSbits that are already there by bitwise-ANDing Y0:UB with 0xF0 (or 0000_0000_1111_0000, (Y0:UB & 0xF0)).  We're still not done.  We want to MERGE those two "nibbles" (half bytes) into a single BYTE, so we use bitwise-OR "|" those two "nibbles" and write that result back out to Y0:UB.

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3609
  • Darth Ladder
Re: input to binary
« Reply #5 on: September 17, 2014, 04:28:57 PM »
You can do that, but he is wanting X0 to be the MSB instead of the LSB. I figured that the Byte swap would make it more complicated than multiple MOVE's.

Oh OK, didn't notice that.  Anytime I ever tried to do this type thing, I just made sure the wires were in the right order.

It was cool doing a BCD thumbwheel with no ladder at all, just reference VX0!   8)
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: input to binary
« Reply #6 on: September 17, 2014, 04:38:17 PM »
Oh OK, didn't notice that.  Anytime I ever tried to do this type thing, I just made sure the wires were in the right order.
Neither did I.  MAPIO would be the most straight forward.  But MATH could do it...

MATH V100 "IF(X0, 0x8, 0x0) | IF(X1, 0x4, 0x0) | IF(X2, 0x2, 0x0) | IF(X0, 0x1, 0x0)"

mhw

  • Hero Member
  • *****
  • Posts: 250
Re: input to binary
« Reply #7 on: September 17, 2014, 04:53:17 PM »
Thanks!

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3609
  • Darth Ladder
Re: input to binary
« Reply #8 on: September 17, 2014, 05:41:16 PM »
MAPIO would be the most straight forward.

So you're saying

Code: [Select]
MAPIO "2 X0 V100:3 X1 V100:2 X2 V100:1 X3 V100:0"?

Slick!   8)
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

mhw

  • Hero Member
  • *****
  • Posts: 250
Re: input to binary
« Reply #9 on: September 23, 2014, 10:45:31 AM »
I have another conversion question. Is there an easy way to convert a "on" bit position in a word to a decimal value. I am using a radio button in Cmore to make a selection. This turns a single bit "on" in a word. I use that bit in logic but also want it to trigger a message in the HMI. So for example I select the 8th button "Bin 8" it will turn on the 8th bit of the word tag. But this will result in a decimal value of 256. I can put "Bin 8" in message 256 but this is not intuitive to me.

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Re: input to binary
« Reply #10 on: September 23, 2014, 10:51:37 AM »
MOVE V2000:8 to V2001:0
or
MATH Result: V2001 Equation: IF(V2000:8,2,v2001)
or
comparative contacts, or using v2001:8 as a contact or....

EDIT: Changed typos and edited MATH for clarity.
« Last Edit: September 23, 2014, 11:29:06 AM by plcnut »
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

mhw

  • Hero Member
  • *****
  • Posts: 250
Re: input to binary
« Reply #11 on: September 23, 2014, 11:20:19 AM »
I probably did not explain myself clearly. Using your math example the result that I was looking for is:
MATH Result: V2001 Equation: IF(V2000==4,3,IF(V2000==8,4,IF(V2000==16,5,V2000)))
I was hoping there was a shorter/easier way that I was missing.
Thanks,
Mike

plcnut

  • Hero Member
  • *****
  • Posts: 814
    • premiersi.com
Re: input to binary
« Reply #12 on: September 23, 2014, 11:38:47 AM »
Here you go:
ENCO V2000, V2001
Circumstances don't determine who we are, they only reveal it.

~Jason Wolthuis
Premier Systems Integration, LLC
http://premiersi.com

mhw

  • Hero Member
  • *****
  • Posts: 250
Re: input to binary
« Reply #13 on: September 23, 2014, 12:19:12 PM »
Quote
ENCO V2000, V2001
Almost perfect. That was what I was looking for, but missed it by one bit. The message box in the Cmore will not recognize a 0 message. I can use the ROTL to get the result that I am looking for.
Thanks!
« Last Edit: September 23, 2014, 12:27:59 PM by mhw »