Indexing Port Pins


Results 1 to 7 of 7

Threaded View

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

    Default Indexing Port Pins

    Melanie posted an excellent article on indexing bits, bytes, words, etc. here
    http://www.picbasic.co.uk/forum/showthread.php?t=544

    Someone asked how to index port pins in a similar way, so here's one way.

    Port file register addresses are sequential. For instance on the 16F876A file
    register address for porta is at location 05h, portb is at 06h, and portc is at
    07h. All you need is a pointer to the base of the bit index.

    Example; SYMBOL PORT_PIN = PORTA ' <- start of bit index or pointer

    Now using something like this, you can index all port bits.
    Code:
    X VAR BYTE ' For loop & bit index pointer  
    ADCON1 = 7 ' All digital
    PORTA = 0   ' Clear all port pins
    PORTB = 0
    PORTC = 0
    
    TRISA = 0   ' Make them all outputs
    TRISB = 0
    TRISC = 0
    
    FOR X = 0 TO 23
      PORT_PIN.0[X] = 1  ' Set all porta, portb, and portc pins high
    NEXT X
    There aren't 8 pins on porta that can go high, but it's still an 8-bit file register
    so we have to treat it as 8-bit and index from 0 to 23 for 24-bits total.

    You can do this on any PIC to index all port pins individually just using porta
    as the starting index pointer.

    PBP doesn't perfom bounds checking, so you can get away with indexing
    out of bounds.

    Note that also means you could write to a register you may not intend to if
    you're not careful.

    You could drop porta, and just use portb as the starting address also.
    Code:
    SYMBOL PORT_PIN = PORTB
    
    X VAR BYTE ' For loop & bit index pointer  
    
    PORTB = 0
    PORTC = 0
    
    ' Make them all outputs
    TRISB = 0
    TRISC = 0
    
    FOR X = 0 TO 15 ' 16-bits total
      PORT_PIN.0[X] = 1  ' Set all portb, and portc pins high
    NEXT X
    Last edited by ScaleRobotics; - 5th December 2010 at 18:13. Reason: added code tags
    Regards,

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

Similar Threads

  1. Pointers To Port Pins ?
    By BitHead in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 23rd December 2009, 20:38
  2. Change On Interrupt, PIC16F884
    By elec_mech in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 14th November 2008, 17:25
  3. Another question RE port line indexing
    By muddy0409 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 7th August 2007, 15:23
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. PIC PORT 'special' pins
    By barkerben in forum General
    Replies: 1
    Last Post: - 18th January 2005, 21:40

Members who have read this thread : 3

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