Help me make a byte
bulkhead
Posts: 405
Hello, I am new to the bs2 and need some help making a byte. I am trying to construct it so that the first 5 bits represent the states of 5 I/O pins that I have connected to simple input switches. Could someone explain how this is done? I was considering a set of IF statements:
IF (IN0 = 1) THEN myByte = myByte + 1
IF (IN1 = 1) THEN myByte = myByte + 2
IF (IN2 = 1) THEN myByte = myByte + 4
etc.
Will this yeild the correct result or is this the ASCII value I am adding? Is there a simpler, more efficient way to do it? Thanks.
IF (IN0 = 1) THEN myByte = myByte + 1
IF (IN1 = 1) THEN myByte = myByte + 2
IF (IN2 = 1) THEN myByte = myByte + 4
etc.
Will this yeild the correct result or is this the ASCII value I am adding? Is there a simpler, more efficient way to do it? Thanks.
Comments
You may need to set myByte as a nibble (0 - 15)
·myByte VAR Nib
If(IN0=0) and (IN1=0) and (IN2=0) and (IN3=0) and (IN4=0) then myByte = 0
If (IN0 = 1) THEN myByte = 1
If (IN1 = 1) THEN myByte = 2
If (IN0 = 1) and (IN1 = 1) then myByte = 3
If (IN0 = 3) THEN myByte = 4
ect.·and then add···DEBUG DEC myByte, CR··to·display the output as decimal·format·1 - 5 digits
You may also want to add more if statements to cover several buttons pressed at the same time
as shown above
I hope this is what you had in mind
John
Presuming the pin ports are contiguous as you've shown, all you need to do it something like this,if you want them in the byte as is:
status var byte
status = INL & $07 'grab 8 pin ports and squelch the last three, leaving 5
If you want it right justified, use this instead:
status = (INL & $07) >> 3
END
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->