PDA

View Full Version : Port control code Yelp!!



andyf
- 5th May 2005, 19:18
' I am sure someone will know this answer and likely laugh a lot at my code, the variables are all declared and the long winded code works great but its very long code.


mainloop:
HSERIN [WAIT("**"),STR in_ctl\11\"!"]
if (in_ctl[0]) = "B" then goto SWB
goto mainloop



SWB:
PIN = in_ctl[1] ' 0 - 7
STATE = in_ctl[2] ' H or L

if PIN="0" and STATE = "H" then HIGH PortB.0
if PIN="0" and STATE = "L" then LOW PortB.0

if PIN="1" and STATE = "H" then HIGH PortB.1
if PIN="1" and STATE = "L" then LOW PortB.1

if PIN="2" and STATE = "H" then HIGH PortB.2
if PIN="2" and STATE = "L" then LOW PortB.2

'And so on

Goto mainloop



' Here's the question
' Using PBP pro 2.45 is there a neater way to do this with less code, I expected to be able to do something like the following to reduce 16 lines of code down to just 2:

SWB:
PIN = in_ctl[1] ' 0 - 7
STATE = in_ctl[2] ' H or L

if STATE = "H" then PortB.[PIN]=1
if STATE = "L" then PortB.[PIN]=0

Goto mainloop

Dave
- 5th May 2005, 20:29
Andyf, Use this syntax to accomplish your task:

SWB:
PIN = in_ctl[1] ' 0 - 7
STATE = in_ctl[2] ' H or L

if STATE = "H" then PortB.0[PIN]=1
if STATE = "L" then PortB.0[PIN]=0

Goto mainloop

actually if you send a "0" or a "1" decimal for the state you can save another line of code by using the syntax:

SWB:
PIN = in_ctl[1] ' 0 - 7
STATE = in_ctl[2] ' H or L

PortB.0[PIN] = state - 48

Goto mainloop

Infact if you don't need to save the "state" for later reference you don't even need it at all by using the syntax:

SWB:
PIN = in_ctl[1] ' 0 - 7

PortB.0[PIN] = in_ctl[2] - 48

Goto mainloop

Dave Purola,
N8NTA

andyf
- 5th May 2005, 21:02
Thanks dave for the answer, tried the code, compiled it without errors but the bit does not change in the pic as in the longer version.

is it possible I need to put some brackets or quotes?
PortA.0([PIN]="1")

andyf
- 5th May 2005, 21:14
Just had a thought while making a coffee,

are the declarations correct to do it your way?

' Variable Defs
' ------------

SerData var byte
PIN var byte
STATE var byte
in_ct var BYTE[10] 'In coming control arrey


if STATE = "H" then PortA.[PIN]=1
if STATE = "L" then PortA.[PIN]=0

Dave
- 8th May 2005, 22:36
Andyf, Sorry I haven't returned a reply before this but I just got back from a wedding of one of my sisters kids. The wedding was in Traverse city which is about 4 hours away. I left Friday at noon and just got back Sunday at about 5:00pm. You have it right. the expression needs a Boolean operator to work so if you use one of the predefined keywords ie HIGH or LOW or 1 or 0 or a variable that would be set to 1 or 0 that would work. No need for brackets after the equals sign unless you have some kind of equation that requires hierarchal order. If that doesn't work I would check the Tris register for being setup properly.

Dave Purola,
N8NTA