A less Cumbersome way of manipulating several LEDS?


Closed Thread
Results 1 to 16 of 16
  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.

  8. #8
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

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

    Quote Originally Posted by HenrikOlsson View Post
    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.
    Because I need them to light up together....if I'm reading your suggestion correctly, only one LED can be on at a time?

  9. #9
    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?

    Sorry, I misunderstood that. I thought you wanted one on when the other was off. I guess I kind of took that for granted because I can't really see the point in using more than one pin. Just wire the LEDs in parallel or, which is probably better, in series. Depends on the powersupply voltage and forward voltage drop of the particular LED.

    Are these some high power blue LEDs? High current and high Vf? Is that the reason you need one pin for each LED? Please tell me what I'm missing :-)

    /Henrik.

  10. #10
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

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

    Quote Originally Posted by HenrikOlsson View Post
    Sorry, I misunderstood that. I thought you wanted one on when the other was off. I guess I kind of took that for granted because I can't really see the point in using more than one pin. Just wire the LEDs in parallel or, which is probably better, in series. Depends on the powersupply voltage and forward voltage drop of the particular LED.

    Are these some high power blue LEDs? High current and high Vf? Is that the reason you need one pin for each LED? Please tell me what I'm missing :-)

    /Henrik.
    Yes, I'm using high brightness blue leds (fwd voltage 3.2V, my PSU is 4V) in parallel drawing about 20mA, therefore two = 40ma...too much for one PIC pin...hence wanting to treat two pins as one virtual pin in code. But it's no big deal....thought I'd ask - and as ever there is something that exists that can be brought into play, except that I need to control the pin on/off via TRISC to avoid clicking so I can't personally use it.
    Last edited by HankMcSpank; - 2nd September 2011 at 10:17.

  11. #11


    Did you find this post helpful? Yes | No

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

    Maybe something like this? Not very swanky, but should work.

    Code:
     
    LED VAR WORD
     
    Main:
    LED = %XXXXX111111111 'ALL LEDS ON 
    GOSUB LED_OUT
    LED = %XXXXX000000000 'ALL LEDS OFF
    GOSUB LED_OUT
     
    Goto Main
     
    LED_OUT:
    PORTC.6 = LED.0
    PORTB.6 = LED.1
    PORTB.5 = LED.2
    PORTB.4 = LED.3
    PORTC.2 = LED.4
    PORTC.1 = LED.5
    PORTC.0 = LED.6
    PORTA.2 = LED.7
    PORTA.1 = LED.8
    RETURN

  12. #12
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

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

    Since you are driving LEDs, why not MIBAM ?
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  13. #13
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

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

    Quote Originally Posted by sayzer View Post
    Since you are driving LEDs, why not MIBAM ?
    I have a common dim function in place already (via a PWM driven mosfet)...I don't really need dimming per pin

  14. #14
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

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

    Hi Mark, Apologies...I missed your earlier contribution...that looks like something along the lines of what I need...I'll give it a try over the weekend - many thanks!

  15. #15
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

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

    You probably realize this... but you will need a pause, of some time delay, after each "GOSUB LED_OUT", in Mark's example. In order for your eye to see the LED's ON or OFF or they may just appear to be flickering at half brighteness.

    Also the "X's" in the example will not compile... need to use "0's". At least they don't compile using microcodestudio.

    Code:
    LED VAR WORD
     
    Main:
    LED = %0000000111111111 'ALL LEDS ON 
    GOSUB LED_OUT
    PAUSE 500 'or delay of your choosing
    LED = %0000000000000000 'ALL LEDS OFF
    GOSUB LED_OUT
    PAUSE 1000 'or delay of your choosing
     
    Goto Main
     
    LED_OUT:
    PORTC.6 = LED.0
    PORTB.6 = LED.1
    PORTB.5 = LED.2
    PORTB.4 = LED.3
    PORTC.2 = LED.4
    PORTC.1 = LED.5
    PORTC.0 = LED.6
    PORTA.2 = LED.7
    PORTA.1 = LED.8
    RETURN
    good luck!!
    Last edited by Heckler; - 3rd September 2011 at 00:31.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  16. #16


    Did you find this post helpful? Yes | No

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

    Heckler, you are right on all accounts.

    In the digital world "X" means "don't care". A 1 or 0 will not cause a state change.
    Sorry for the confusion. In PBP you can drop the leading zeros in a binary variable.
    This %000000111111111 would equal %111111111.

    Best regards

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