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,124


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    So what I want to do, I'll write a code in some imaginary basic, a bit short code that will do a running led light:

    DIM LEDS(3) 'define array with 3 entries
    LET LEDS(1)=PORTA.2
    LET LEDS(2)=PORTB.3
    LET LEDS(3)=PORTC.1 'enter data into array

    FOR A=1 TO 3
    HIGH LEDS(A)' make pin high
    pause 500 ' wait some time
    LOW LEDS(A) 'make pin low
    NEXT A

  2. #2
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    So I wrote it in this way, but it does not works:

    Code:
    AC VAR BIT[5]
    AR VAR BIT[7]
    
    
    AC[5]=PORTC.5
    AC[4]=PORTC.4
    AC[3]=PORTB.3 
    AC[2]=PORTB.1
    AC[1]=PORTB.5 
    
    AR[1]=PORTB.6
    AR[2]=PORTC.7
    AR[3]=PORTB.2 
    AR[4]=PORTB.0 
    AR[5]=PORTB.4 
    AR[6]=PORTB.7
    AR[7]=PORTC.6 
    
    'make all leds connected to ports on.
    
    low ac[1]
    low ac[2]
    low ac[3]
    low ac[4]
    low ac[5]
    high ar[1]
    high ar[2]
    high ar[3]
    high ar[4]
    high ar[5]
    high ar[6]
    high ar[7]
    
    'but it does not works
    'but it works with code below
    low PORTB.3
    HIGH PORTC.7
    
    
    
    end

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,607


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    Hi
    It doesn't work that way, it won't work that way and it can't work that way.

    When you do AR VAR BIT[7] you are reserving space in RAM for 7 consecutive bits. You can't them simultanously mirror each bit in RAM to a ANOTHER adress (which the ports are) - it won't and can't work.

    Aliases must all point to the SAME location, like
    Code:
    AR VAR BIT[7] 
    myBit VAR AR[2]
    In the above case AR[2] and myBit are the one and same memory location, ie. two names for the same location, you can't do it the other way around. Ie you can't have one name for two locations which is basically what you're trying to do.

    When you do
    Code:
    AR VAR BIT[7]
    
    AR[1]=PORTB.6
    You are reading the state of PORTB.6 and store the result of that in AR[1].

    The only way I know of that is similar to what you're trying is to use a port offsets and a lookup table.

    /Henrik.

  4. #4
    Join Date
    Feb 2013
    Posts
    1,124


    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?

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,607


    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.

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


    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?

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


    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.

  8. #8
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: How to write port names into array ?

    Quite interesting, why it can't ?

    The code example with LET & DIM is from Sinclair Basic, written in 1982 and running on Z80. It can handle such things, and picbasic pro - can't ?

Similar Threads

  1. Setup port pins into an array
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 21
    Last Post: - 25th April 2013, 16:24
  2. Assign different port bits to an array?
    By vamtbrider in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th November 2010, 02:40
  3. Why (or how to) make a port array for OW
    By Roy___ in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 22nd February 2009, 23:30
  4. array and port
    By piombazzo in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th November 2008, 20: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, 18: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