can you make name variables?


Closed Thread
Results 1 to 9 of 9
  1. #1

    Default can you make name variables?

    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

  2. #2
    anj's Avatar
    anj Guest


    Did you find this post helpful? Yes | No

    Default

    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.

    Code:
    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
    Code:
    MyPort = Porta.0
    ...
    MyPort[X]  where X = 0..7 gives  PortA
                     X = 8..15 gives PortB  etc
                     X = 16..23 gives PortC
    Andrew

  3. #3


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    anj's Avatar
    anj Guest


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Sorry for the delay Andrew. It works - Thank you. - Peter

  6. #6
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by peterdeco1

    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
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  7. #7
    anj's Avatar
    anj Guest


    Did you find this post helpful? Yes | No

    Default

    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

  8. #8
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by anj
    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.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  9. #9
    anj's Avatar
    anj Guest


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. How come they make electronics hard as poss
    By George in forum Off Topic
    Replies: 9
    Last Post: - 13th July 2007, 23:54
  2. Changing declared variables names on the fly
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th December 2006, 06:34
  3. Variables not appearing in Watch Window MPLabv7.3
    By jd76duke in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th June 2006, 14:36
  4. Problems with variables
    By egberttheone in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th December 2004, 18:37
  5. Bit variables... typo?
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 29th November 2004, 18:11

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts