Host Engineering Forum
General Category => Do-more CPUs and Do-more Designer Software => Topic started by: mhw 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?
-
There may be a more elegant way, but you could:
MOVE:
X0
V0:3
MOVE:
X1
V0:2
MOVE:
X2
V0:1
MOVE:
X3
V0:0
-
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
-
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.
MOVER:
Source: X0
Range: 4
Dest: V0:0
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).
-
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.
-
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)
-
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)"
-
Thanks!
-
MAPIO would be the most straight forward.
So you're saying
MAPIO "2 X0 V100:3 X1 V100:2 X2 V100:1 X3 V100:0"?
Slick! 8)
-
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.
-
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.
-
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
-
Here you go:
ENCO V2000, V2001
-
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!