PDA

View Full Version : can you make name variables?



peterdeco1
- 12th March 2005, 11:55
Hello Everyone. I am attempting to turn on 3 ports to enable some IC's. Is there a valid PBP command to do this?:

chip1 = porta.1
chip2 = porta.2
chip3 = porta.3

if porta.4 = 0 then let chip = chip1
if porta.5 = 0 then let chip = chip2
if porta.6 = 0 then let chip = chip3

high chip

by making chip high, the corresponding port (a.1, a.2, a.3) goes high. The reason for wanting to do this is, in my example above, "high chip" is actually a large routine of about 50 lines of code. If I copy the routine and have 3 of them for a.1, a.2, a.3 I run out of code space. So having "chip" variably represent a port would really solve the problem. Thanks Everybody. - Peter

anj
- 12th March 2005, 21:19
Gday Peter
I posted re this problem on 1/3/2004 and again on 6/3/2004 "Accessing Ports"
You cannot swap the symbol reference to a port programatically, but you can reference a port using array techniques.
Melanie did a write up on this after my initial enquiries, but it appears to have disappeared. It didnt work in some circumstances, but would work for what you are doing. Ie simple port access.



chip = porta.1
X = 0
if porta.4 = 0 then let X = 0
if porta.5 = 0 then let X = 1
if porta.6 = 0 then let X = 2
high chip[X]


Also, with this technique, you can reference between ports
ie


MyPort = Porta.0
...
MyPort[X] where X = 0..7 gives PortA
X = 8..15 gives PortB etc
X = 16..23 gives PortC

Andrew

peterdeco1
- 13th March 2005, 09:47
Hello and thank you Andrew. I tried chip = porta.0 and it won't compile. Then I tried:

x var byte
chip var porta.0
let x = 3
high chip [x]

Doesn't work - porta.0 goes high regardless of the value of x. So now I tried:

high chip + x

It works! If I replace porta.0 with chip + x in my program it should solve my problem. My 50 line block of code has a lot of adcin & shiftout commands. That's why I run out of program memory when I duplicate the block numerous times. Thank you Andrew. - Peter

anj
- 13th March 2005, 20:20
Gday Pete

"I tried chip = porta.0 and it won't compile."

Oops sorry, my mistake when cut and pasting.
( You know what they say about shortcuts )
The "chip var porta.0" syntax you arrived at is correct, and same as my referenced example.

"high chip [x] doesnt work"

I used "xport(k) = 1 ' set pin high" syntax
as it uses less code than the high command ( apparently )
I was testing using LEDs, so i know that it worked in that mode.
However your syntax shld also work.
Based on yr cut and paste, there is a space between chip and [X].
If this is so, remove it and retry. Shld work then.

Andrew

peterdeco1
- 19th March 2005, 10:38
Sorry for the delay Andrew. It works - Thank you. - Peter

NavMicroSystems
- 19th March 2005, 15:18
chip1 = porta.1
chip2 = porta.2
chip3 = porta.3

if porta.4 = 0 then let chip = chip1
if porta.5 = 0 then let chip = chip2
if porta.6 = 0 then let chip = chip3

high chip



How about:

TRISA=%11110001
if porta.4 = 0 then porta.1=1
if porta.5 = 0 then porta.2=1
if porta.6 = 0 then porta.3=1

or even shorter:

TRISA=%11110001
porta.1= ~porta.4
porta.2= ~porta.5
porta.3= ~porta.6

anj
- 19th March 2005, 22:09
Gday Ralph
The problem Peter is trying to overcome is explained in the bottom para of his initial post, and is the same one i hit.
When doing the same function multiple times, it is more efficient to build a subroutine. However, if one of the "variables" in that subroutine is a port, yr stuffed.
By referencing the reqd port the way described, you can set a std integer variable before entering the subroutine, thus bypassing the problem.

However, it doesnt always work ( according to Melanie ). But it does work for simple on/off type stuff.

Andrew

NavMicroSystems
- 20th March 2005, 01:01
When doing the same function multiple times, it is more efficient to build a subroutine.

Andrew, that's absolutely right,
but if you don't let us know what the SUB requires as input and what it
deliveres as output,
we cant offer much help.

anj
- 20th March 2005, 02:24
Gday Ralph
As per Peters original request

If I copy the routine and have 3 of them for a.1, a.2, a.3 I run out of code space.
Having been down the track he went down, i understood his post as meaning he currently had a routine that he would have to copy three times unless he could pass by reference, a port as a variable ( which you cant do ).
As such all other inputs/outputs were irrelevant at the time, just how to ref a port.
If he has other probs with his routines, i dont know, i merely advised of a different method i have used to get around referencing ports in subroutines.

Andrew