Custom array of ports using array?


Closed Thread
Results 1 to 23 of 23

Hybrid View

  1. #1
    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

  2. #2
    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 06:40.

  3. #3
    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

  4. #4
    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

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Nice job Paul & Ingvar..;o}

    Yet another version.
    Code:
        DEFINE OSC 4
    
    '   BASIC/ASM version = 172 words
        
        x   var word
        i   var byte
        RelayNum var PORTA
        
    '    High byte = FSR / Low byte = port pin (or multiple pins)
        RLY1 CON %0000010100000001 ' PORTA|Bit0
        RLY2 CON %0000011001000000 ' PORTB|Bit6
        RLY3 CON %0000011100010000 ' PORTC|Bit4
        RLY4 CON %0000100000000100 ' PORTD|Bit2
        
        ADCON1 = 7
        PORTA = 0 : PORTB = 0 : PORTC = 0 : PORTD = 0
        TRISA = 0 : TRISB = 0 : TRISC = 0 : TRISD = 0
        
    ASM
    RelayOn macro
        movf  _x+1,w ; high byte = pointer to port file reg
        movwf FSR    ; load FSR
        movf  _x,w   ; low byte = port bit to change
        xorwf INDF,f ; toggle port pin
        endm
    ENDASM
    
    ASM
    RelayOff macro
        movf  _x,w   ; reload port pin to toggle
        xorwf INDF,f ; do it
        endm
    ENDASM
                             
    main:
        for i = 0 to 3
            LOOKUP2 i, [RLY1,RLY2,RLY3,RLY4], x ' lookup file reg & XOR pattern
            @ RelayOn
            PAUSE 500
            @ RelayOff
            PAUSE 500
        next i
        
        END
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I agree, good stuff!

    Here's another one.
    Code:
    INCLUDE "VirtualPort.bas"
    
    Relays  VAR BYTE : Relays = 0
    
    ASM
    RelayPort  macro
        Vpin  0, PORTB, 6  ; RELAY1
        Vpin  1, PORTC, 4  ; RELAY2
        Vpin  2, PORTD, 2  ; RELAY3
      endm
    
      OutputPort  RelayPort    ; Set Port to OUTPUT
    ENDASM
    Then, here's a test loop to cycle thru the relays...
    Code:
    i  VAR BYTE
    
    Main:
        for i = 0 to 2
            Relays = DCD i
            @  WritePort _Relays, RelayPort
            PAUSE 500
        next i
    Goto Main
    Code size = 104 words, 65 without the PAUSE.

    or you can just...
    Code:
    Relays = %010
    @  WritePort _Relays, RelayPort
    to turn on RELAY2 by itself.
    Code size = 21 words.

    http://www.pbpgroup.com/Vport/VirtualPort.bas.txt

    HTH,
        DT

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


    Did you find this post helpful? Yes | No

    Smile Amazing yet again ...

    Bruce, Darrel,

    You guys are amazing yet again! I continue to learn and what good fun.

    Paul

    Edit - I am an ASL student (Assembly Second Language) student and removed some code from this post that was not correct.-pb
    Last edited by paul borgmeier; - 15th June 2006 at 09:53.

Similar Threads

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

Members who have read this thread : 2

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