PDA

View Full Version : A less Cumbersome way of manipulating several LEDS?



HankMcSpank
- 1st September 2011, 16:54
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...



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.



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



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!

Darrel Taylor
- 1st September 2011, 18:39
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/showthread.php?t=12502&p=83158#post83158
Like 80% of everything I've done in this forum, the post got destroyed. But the file's still there.

HankMcSpank
- 1st September 2011, 19:25
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)

mister_e
- 1st September 2011, 19:27
how fast human eye is btw? vport is nowhere near to be that slow

Darrel Taylor
- 1st September 2011, 20:29
....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.


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

HankMcSpank
- 1st September 2011, 23:05
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)

HenrikOlsson
- 2nd September 2011, 06:13
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.

HankMcSpank
- 2nd September 2011, 09:39
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?

HenrikOlsson
- 2nd September 2011, 09:54
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.

HankMcSpank
- 2nd September 2011, 10:09
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.

mark_s
- 2nd September 2011, 16:00
Maybe something like this? Not very swanky, but should work.




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

sayzer
- 2nd September 2011, 17:45
Since you are driving LEDs, why not MIBAM ?

HankMcSpank
- 2nd September 2011, 20:22
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

HankMcSpank
- 2nd September 2011, 22:30
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!

Heckler
- 3rd September 2011, 00:25
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.


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!!

mark_s
- 3rd September 2011, 15:40
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