How to write port names into array ?


Closed Thread
Results 1 to 29 of 29

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,156


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    So,

    PORTB.6=AR[1] won't work?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    Hi,
    Well, yes, it'll work.
    It'll set the state of PortB.6 to what is stored in AR[1], either 1 or 0 at the time that the instruction executes.
    What it will NOT do is create a "mirror" where the state of PortB.6 automatically changes whenever AR[1] changes - which I suspect is what you're looking for.

    /Henrik.

  3. #3
    Join Date
    Feb 2013
    Posts
    1,156


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    well,

    say I can have

    ledpin1 var PORTB.1

    then when I issue

    high ledpin1

    it will make ledpin1 high, right?

    so why I can't have array member aliased as port, so when I'll issue

    high array[1]

    since this previously was set to as alias of portb.1

    portb.1 become high?

    More simple.

    I can have port name aliased to variable, and then I can change the value of variable and this change will change port state, too.

    So, I can't have port name aliased to array variable?

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    Hi,
    well,
    say I can have
    ledpin1 var PORTB.1
    then when I issue
    high ledpin1
    it will make ledpin1 high, right?
    Yes, that's correct, it'll make PortB.1 high because "all" you've done is assigned the "name" or "identifier" ledpin1 to bit 1 of the register PortB. So when you do HIGH ledpin1 it'll "translate" to HIGH PortB.1 - see, ledpin1 and PortB.1 are one and the same.

    so why I can't have array member aliased as port, so when I'll issue
    high array[1]
    since this previously was set to as alias of portb.1
    portb.1 become high?
    Beacuse PortB.1 is at one location in the memory map and Array[1] is at another - they are not one and the same.
    When you create an array you are actually allocating space in RAM for that array as opposed to the previous example where you simply assigned a different "name" to an already existing register. Array[1] and PortB.1 are two different locations.

    If you're example would work then one should be able to do
    PortB VAR PortA
    PortC VAR PortB
    PortD VAR PortC
    PortA = 0

    and expect all ports to clear - it doesn't work that way because PortA is not the same register as PortB just as Array[1] not the same PortB.1 - they are two different things. On the other hand
    myPort VAR PortA
    myPort = 0

    Will clear PortA because all you've done is to create an alternative name or identifier for an already existing register.

    You can have multiple names for a single location.
    You can not have one name for multiple location.

    Does that make sense?

    /Henrik.

  5. #5
    Join Date
    Feb 2013
    Posts
    1,156


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    Ok, I understood the limitations. But why they are present here, and aren't present elsewhere?

  6. #6
    Join Date
    Feb 2013
    Posts
    1,156


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    And is there a way to overcome them?

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    Hi,
    Ok, I understood the limitations. But why they are present here, and aren't present elsewhere?
    Where are they not present? Can you show me an example in Sinclair BASIC that does what you want?

    And is there a way to overcome them?
    Yes and no.
    There is, to my knowledge, no way to have a pin or port or any other register for that matter aliased into an array in a way that makes the pin/port automagically change when you write to the array. You need to write code that iterates thru the array and writes each port/pin individually and then call that routine each time you change the array. There might be a way (by specifying the absolute adress) to create a BIT array "overlaying" the port register adress space but then all the pins will be consecutive within the array, ie if Array[0] is PortB.0 then Array[1] will be PortB.1 and so on.

    If you could explain a little bit what the purpose of this operation is perhaps someone will provide a workable solution.

    /Henrik.

  8. #8
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    Clearly I have erred, first in drawing assumptions and, second by posting untested code based on those assumptions. I dislike being wrong, but I dislike being ignorant even more. Ha! Is this not true for others as well?

    So perhaps, it is germane to the discussion, will someone help me to understand my error? In the PBP manual, the statement SYMBOL is used to assign a second name to an existing variable - the process is described as aliasing; I presumed this meant then that both variable names would create pointers to the same RAM address - and be resolved during the assembly process. This misunderstanding was reinforced in the book PIC Basic Projects: 30 Projects Using PicBasic and PicBasic Pro by Dogan Ibrahim where he asserts on page 84:

    "In order to make programs more readable, we can assign meaningful names to variables..." He uses the example: SYMBOL Count = B0. The author goes on to note: "This statement does not occupy any location in microcontroller RAM memory".

    I accept Henrik's explanation; yet, I struggle to understand the purpose of SYMBOL and how it is used if not in this way.

    As an aside, I also note that Curiousone did not make the SYMBOL assignments I suggested in the code he posted.

Similar Threads

  1. Setup port pins into an array
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 21
    Last Post: - 25th April 2013, 17:24
  2. Assign different port bits to an array?
    By vamtbrider in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th November 2010, 03:40
  3. Why (or how to) make a port array for OW
    By Roy___ in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 23rd February 2009, 00:30
  4. array and port
    By piombazzo in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th November 2008, 21:58
  5. Replacing an ARRAY with EEPROM write to save space?
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th March 2005, 19:31

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