Custom array of ports using array?


Closed Thread
Results 1 to 23 of 23

Hybrid View

  1. #1
    blainecf's Avatar
    blainecf Guest

    Default Custom array of ports using array?

    Is it possible to make an array, where each element equates to a non-consecutive port.

    Example:

    RELAY VAR BIT[10]

    ' (obviously, this doesn't work, but it does crash the compiler)
    SYMBOL RELAY[1] = PORTB.6
    SYMBOL RELAY[2] = PORTC.4
    SYMBOL RELAY[3] = PORTD.2
    ...

    ' cycle through relays
    for i = 1 to 3
    RELAY[i] = %1
    PAUSE 1000
    RELAY[i] = %0
    PAUSE 300
    next i

  2. #2
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    I don't see why that wouldn't work. Try removing the symbols. I don't see the need for them. I declared relay at the top.

  3. #3
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    I should say the Symbol command.

  4. #4
    Join Date
    Apr 2006
    Location
    Michigan
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    Plus I think an array should start with variable 0 not 1. So it would be 0 through 9.

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    blainecf,
    I am not sure this will work like you expect. When you declare an array variable, it is acually pointing to a series of consecutive RAM addresses. So later, when you use the variable, it goes to the address and uses the value currently at that address. What you are looking to do is create an array that is actually pointing to the addresses represented by the individual pins (which are not even consecutive).

    After playing with it a little and looking at the .lst file, I don't know if this can be done in PBP. Once you have created the array, any further references to the array are modifying/reading the contents, not the address. The only info in the manual I see is assigning a label to a specifc pin. Which is handled using different ASM commands.

    Maybe one of the real gurus could chime in here and set me straight if I'm wrong (Melanie? Darrel?). Or provide a workaround to do the job.

    Hope to get a better answer,
    Steve

  6. #6
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Last edited by paul borgmeier; - 13th June 2006 at 20:02.

  7. #7
    blainecf's Avatar
    blainecf Guest


    Did you find this post helpful? Yes | No

    Default Makes a case for functions with pass-by-reference

    Bummer,

    It's these little things that make programming clean and respectable.

    Still, it seems a shame that I can create an aliassed variable that is equivilent to a pin, but not an array element. Seems like the same type of technology.

    So close, yet so far.

    Thanks so much for your input, everyone.

  8. #8
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Paul:
    Thanks for "chiming in" with a solution. I just want to make sure I understand what your code is trying to do so I can learn from it. I suspected there was a way to shoehorn this idea into PBP.
    Code:
    Relay[1] = 14  'RB6  is 14 bits offset from RA0
    Relay[2] = 20  'RC4  is 20 bits offset from RA0
    Relay[3] = 26  'RD2  is 26 bits offset from RA0
    This array is establishing an offset from bit.0 on PORTA. I assume that most pics have PORTA-PORTX at consecutive regiester addresses (I only have a couple of datasheets to reference, but that's what I see)

    Code:
    for i = 1 to 3
         x = relay[i]
         porta.0[x]=1
         PAUSE 1000
         porta.0[x]=0
         PAUSE 300
    next i
    With the Relay[] array containing the correct offets, one can now use that with porta.0[x] to select the desire bit. Since porta.0 points to a bit, the offset [x] is in bits as well.
    Did miss this "feature" in the PBP manual? Can I extend this idea further in that the inclusion of [X] to any reference that points to a register will actually cause an offset reference in accordance to the base reference. ( ie, if it was a word variable that [x] was added to, [x] would actually reference a register x*2 bytes offset?).


    Steve

  9. #9
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile Neat Trick

    Steve,

    Yes, you are 100% correct in the logic - that is exactly how it works. This “trick” is not in the manual but first appeared on the PicBasic List in 2002 (http://list.picbasic.com/forum/messages/4011/4739.html? ).

    I remember being excited when this particular email came because it solved a problem I was having at the time. The links posted in my reply #6 above by Bruce and Melanie also clearly demonstrate this trick's use.

    As I understand it, it works for all SF/GP Register locations, not just PORT bits, hence the indexed locations must be sequential like you mentioned. I always check the datasheet if I am going to use it on sequential Ports to make sure they are indeed sequential (and they always have been for the PICs I have used).


    Paul
    Last edited by paul borgmeier; - 14th June 2006 at 05:40.

  10. #10
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    Since portpins rarely move(constants) you could use the LOOKUP statement to get the offset values. Should free up some RAM. The LOOKUP could ofcourse be placed in a subroutine if you need to access it from many different locations in your program. That way saving ROM.
    Code:
    x      var byte
    i       var byte
    
    main:
    for i = 0 to 3
    '               a  b  c  d
    '               0  6  4  2
         LOOKUP i, [0,14,20,26], x
         porta.0[x]=1
         PAUSE 1000
         porta.0[x]=0
         PAUSE 300
    next i
    /Ingvar

  11. #11
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile Sweet

    Nice improvement! - The code for what blaine originally wanted to do is getting pretty clean considering it is an undocumented feature.

    Best,

    Paul

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  3. Custom array of pins
    By bmagistro in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 28th March 2009, 23:15
  4. Crystalfontz LCD
    By jman12 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 9th February 2007, 15:04
  5. Making Array of two ports
    By John7 in forum General
    Replies: 3
    Last Post: - 5th March 2005, 02:20

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