A less Cumbersome way of manipulating several LEDS?


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default A less Cumbersome way of manipulating several LEDS?

    Hi all,

    I seek an easier way of controlling leds!

    At the minute, due to my restrictive board layout, I've several leds scattered across all the PIC's port/pins, for example...

    Code:
    LED0  VAR    PortC.6
    LED1  VAR    PortB.6
    LED2  VAR    PortB.5
    LED3  VAR    PortB.4
    LED4  VAR    PortC.2
    LED5  VAR    PortC.1
    LED6  VAR    PortC.0
    LED7  VAR    PortA.2
    LED8  VAR    PortA.1
    at the minute, to say switch some leds on/off, I'm doing something like this.

    Code:
    high led0
    high led1
    low  led2
    low  led3
    high led4
    ...which ok isn't too bad, but gets rather long winded - especially when you want a few different permutations.

    Forgive the naive line of questioning, but is there anyway, I can group the 'port diverse' LED mappings all together into something like a virtual Port (or logical byte/word), where I could for example do something like this....

    Code:
    LEDs = %00010011
    if so, bear in mind I have 9 LEDs ...how would that work out with the above in mind?!

    In a similar vein, can I group just two specific leds together for ease of handling? (I always will want two specific LEDS to light/extinguish together - but want to treat them as one entity vs having to duplicate the commands to each port/pin per LED)

    Many thanks!
    Last edited by HankMcSpank; - 1st September 2011 at 17:03.

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


    Did you find this post helpful? Yes | No

    Default Virtual Ports

    Code:
    LEDs  VAR WORD
    ;---------------------------------------------------------------------------
    INCLUDE "VirtualPort.bas"
    ASM  
    MyPortPins  macro               ; these define which bits go with which pins
        Vpin   0, PORTC,6
        Vpin   1, PORTB,6
        Vpin   2, PORTB,5
        Vpin   3, PORTB,4
        Vpin   4, PORTC,2
        Vpin   5, PORTC,1
        Vpin   6, PORTC,0
        Vpin   7, PORTA,2
        Vpin   8, PORTA,1
      endm
    ENDASM
    ;---------------------------------------------------------------------------
    Init:
    @   OutputPort  MyPortPins             ; Set Pins to Output
        LEDs = %000010011
    @   WritePort  _LEDs, MyPortPins    
        PAUSE 1000 
        LEDs = 0
    ;---------------------------------------------------------------------------
    Main:
        LEDs = LEDs + 1
        @  WritePort  _LEDs, MyPortPins    ; write pattern to Virtual Port
        PAUSE 250
    GOTO Main
    I haven't tried it on the 16F1's, but I think it should work.

    The VirtualPort.bas file is in this thread ... http://www.picbasic.co.uk/forum/show...3158#post83158
    Like 80% of everything I've done in this forum, the post got destroyed. But the file's still there.
    DT

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Virtual Ports

    Excellent Darrel - many thanks....does it add much in the way of delay? (ie if I was sequencing 8 leds fast would this start slowing stuff up)

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: A less Cumbersome way of manipulating several LEDS?

    how fast human eye is btw? vport is nowhere near to be that slow
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default Re: Virtual Ports

    Quote Originally Posted by HankMcSpank View Post
    ....does it add much in the way of delay? (ie if I was sequencing 8 leds fast would this start slowing stuff up)
    VirtualPort will be faster than using lists of HIGH/LOW since it doesn't set the TRIS bits each and every time.
    That in itself may be a problem when running at 32Mhz and the old R-M-W issue may come into play.
    Let me know if it does and I'll change it to use the LAT registers on the 16F1's.

    In a similar vein, can I group just two specific leds together for ease of handling? (I always will want two specific LEDS to light/extinguish together - but want to treat them as one entity vs having to duplicate the commands to each port/pin per LED)
    The pin list macro can have multiple pins assigned to each bit.
    In this example both RC6 and RB3 would turn on and off according to bit0 of the value passed to WritePort.
    Code:
    MyPortPins  macro               ; these define which bits go with which pins
        Vpin   0, PORTC,6
        Vpin   0, PORTB,3
        Vpin   1, PORTB,6
        Vpin   2, PORTB,5
    DT

  6. #6
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Virtual Ports

    Hi Darrel,

    That's fantastic (ie two pins handled as one virtual pin...I'm figuring that the maximum number of Virtual Port bits is 8? if so, wouldn't you just know that I have 9 LEDs!)

    re VirtualPort and the TRISC settings ...hmm, maybe I can't use your funky routine after all - one thing I observed when using the HIGH/LOW command, was excessive audible clicking attributed to the port.pin switching state (I work with low level guitar signals, which often pass in close proximity to a PIC, guitar signals often get treated with enormous gain so I'm perhaps more aware/sensitive to such clicks than others!) The only way I was able to stop such clicking was to to use TRISC command instead of the high/low command, for example, this would result in clicking....

    TRISC.6 = 0
    HIGH PORTC.6

    whereas this doesn't...

    PortC.6 = 1
    TRISC.6 = 0

    ....whacky. (so to turn an LED on/off, I don't use high/low, but just set the pin as output or input)
    Last edited by HankMcSpank; - 1st September 2011 at 23:10.

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


    Did you find this post helpful? Yes | No

    Default Re: A less Cumbersome way of manipulating several LEDS?

    In a similar vein, can I group just two specific leds together for ease of handling? (I always will want two specific LEDS to light/extinguish together - but want to treat them as one entity vs having to duplicate the commands to each port/pin per LED)
    Why not wire them in "anti-parallel" then? One is on when pin is low, the other when pin is high and both are off when TRIS is 1.

    /Henrik.

Members who have read this thread : 0

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