PDA

View Full Version : Port bit manipulation useing variables



shawn
- 20th November 2010, 15:31
Hello,
I Am writing a Modbus RTU program to talk with an ILon 100 internet Server. I am trying to streamline my code. I am using modbus single coil write function to write the value of PortB single bit values. The program below works but is there a better way than Case Select. When I say better I mean less code and faster. This is not the whole code but is the jist of the Case Select.



WriteSingleCoil:
W0 = buffer[3]*255 + buffer[4] 'Combines MFrame 2 byte address into 1 word.
W0 = w0 - 10 'Sets up bit address for portB
If buffer[5] > 0 Then
Coilstate = 1
else
Coilstate = 0
endif
Select Case W0
Case 1
PORTB.0 = Coilstate
Case 2
PORTB.1 = Coilstate
case 3
PORTB.2 = Coilstate
case 4
PORTB.3 = Coilstate
case 5
PORTB.4 = Coilstate
case 6
PORTB.5 = Coilstate
case 7
PORTB.6 = Coilstate
Case 8
PORTB.7 = Coilstate
Case else 'test to see if program is getting here
portb = 255
End Select
For B0 = 1 To 8 ' Send the response to Master
B1 = buffer[B0]
Gosub charout
Next B
Return

Is there a way to do something like this.


PortB.w0 = Coilstate

I tryed this but it does not work. I'm going to try to use an aliass next but Im not sure if thats a good choice either.
Thanks
Nick

aratti
- 20th November 2010, 15:54
Is there a way to do something like this.
PortB.w0 = Coilstate

Yes

PortB.0[W0] = Coilstate


Cheers

Al.

shawn
- 20th November 2010, 20:23
Hey
Thanks for your answer but I don't completly follow.

PortB.0[W0] = Coilstate

What is the .0 for.
I want to do something like this.

PortB.[W0] = Coilstate

But It does not compile.
Where W0 is the pin or bit of port B to change.

Thanks
Nick

aratti
- 20th November 2010, 20:42
Nick, PortB.0[W0] = Coilstate is a workaround to obtain PortB.[W0] = Coilstate and have the code compiling.

Cheers

Al.

shawn
- 21st November 2010, 04:41
Thanks Al, that worked