Three pins will easily support up to six Charlieplexed switches (requires one diode per switch).
Three pins will easily support up to six Charlieplexed switches (requires one diode per switch).
Just an update (since you've all been so kind to chime in with your ideas...many thanks!)
Last night got home & thought ok....ok, so let's learn about timers - so I went to Darrel's site & saw the timer based flashing LED example....absolutely perfect for one of my other needs -so I went off on a slight (but related) tangent!!
I have a tri-colour LED - ie a green & red LED (which obviously also gives amber if they're both switched on at the same time).....so three possible colours. But with four operational modes, I also need a visual representation to the player when in the the fouth mode - that'd be a flashing LED!
Previously, I was checking regularly within the main code to see if the player had selected the 4th mode - and if so, I'd flash an led. But as Darrel has mentioned to on his site, it's tricky using this method to have a flashing LED with stable, regular interval flashes.
So Darrel's solution was a breath of fresh air (thanks Darrel - again)
I then got waylaid trying to work out some wayof getting my program, to 'remember' whether switch3 was last selected in 'mode 3' or 'mode 4' (I have an involved switching arrangement, where any of the 3 switches will turn the device on...once on, you can freely press another switch to move to ther other modes - but for user practicality, if the unit was switched on in mode3, then say switch 2 was pressed, when going back to switch 3, I'd reckon the player would want whichever mode he was last in with switch3 to be recalled - eeeuggh ...this is getting wordy & hard to describe so I'll digress!).
Darrel...good idea about 'any two...no matter which two' ....I'll mull that one over (might be better than me having to learn about timers for my situation!!). The wierd thing is that even though some ideas seem fine, when you come you actually use them ....they're not so good. My earlier example about holding swith3 longer to enter mode 4 - it just doesn't fit in with the intended applicated - fast switching for a guitarist going hell for leather!
Mike....all my switches use diodes.
Last edited by HankMcSpank; - 6th August 2010 at 11:57.
Hi Hank',
Got a schematic we could look at? Maybe there's a way to recover an extra pin that you haven't thought of.
How are you sampling and managing your switches? Polling them in a loop or polling them via periodic interrupt?
Are you using two pins for that tri-color LED or are you using any pins for LED mode indicators? If so, you might consider reclaiming those pins and using one of them for the fourth switch, then mux' the mode LEDs with the switches.
Regards, Mike
Last edited by Mike, K8LH; - 6th August 2010 at 15:03.
Here's the switching part....
(I know some consider it bad form, but I use the PIC's internal weak pullups, for no other reason than it saves space - and believe me, I need all the space I can get!)
...there are a few spare pins Pin on the 16f690, Pin 4 is somewhat restricted in its use, so I stay clear of it, Pin 9 is 'forsaken' so I can use that bit of track/pcb real estate to 'conduit' the +4.1V onwards to the rest of the PCB, Pins 6, 15, 16 & 19 are essentially 'landlocked' from a PCB track routing perspective
Yes, I'm using two pins (pins 2 & 3) for driving the LED (must remember to order common anode LEDs next time, so I can sink current from the main 9V VCC rail vs the PIC's 4V.1 rail ...puts less demand on the 100mA LDO regulator I've used!)
Re how am I sampling the switches? - using DT's mighty fine interupt routine.
There is no room for a fourth switch - likewise no room for illuminated switches - I'm only using 6mm square momentary push switches because they're the only ones I can shoehorn in.
The problem of course is the more 'modes' I offer (and it would be lovely to offer 6 or 7), is providing the guitarist with meaningful (rememberable) visual indication of the modes with just one LED ...green, amber & red is fine. ....squeezing in a 'flashing LED' for the extra mode is pushing it - any more modes, that it's going to be hell to remember what the LED is telling you! (& there's no room for a lcd, 7 seg display etc)
To give you some idea of my challenge, the pcb 'real estate' is about 40mm x70mm x 5.5mm high (1.5" x 3" x 0.2" high) - in that space I have to fit 2 x 20SOIC, 1 x 8 SOIC, 1 POT, 4 switches (the fourth switch is not at all related to this circuit), one single coil guitar pickup (that I make myself), one chunky electrolytic....and this is all on single sided pcb (along with a whole heap of 1206 SMD components) - it's crammed!
Last edited by HankMcSpank; - 6th August 2010 at 15:40.
Oh my! Some pretty nasty self-imposed restrictions there Hank'.
There are smaller switches (see below, just left of the machined socket). And if you think about it, that tri-color LED actually has four states (off, red, green, and yellow).
Good luck on your project.
** added **
Someone just wrote to ask about the driver for that mux'd switch & LED drawing so I'll add it here if nobody minds. And sorry for the C code (I don't have PBP)...
The routine sets the four pins to inputs to read the switches. Standard 'parallel' switch state logic filters out all but a "new press" state while ignoring the other three states (for each switch). 1-of-N logic (only 1 mode on at any time) dictates that we save the "new press" pattern as the new "mode". Since a pressed switch could short out a pin that is set to output '1' we cannot turn off the inactive LEDs by outputting a '1' and so we leave those pins tri-stated (RC3..RC0 output latch bits are maintained at '0').
Code:/* * * isr debounce & switch state management (1-of-N switch logic) * * using 8 to 32 msec interrupt intervals * * * * swnew ____---____-----___ new switch sample * * swold _____---____-----__ switch state latch * * delta ____-__-___-____-__ changes, press or release * * newhi ____-______-_______ filter out new release bits * * */ trisc |= 0x0F; // set RC3..RC0 to inputs swnew = ~portc; // sample active lo switches swnew &= 0x0F; // on RC3..RC0 pins swnew ^= swold; // changes, press or release swold ^= swnew; // update switch state latch swnew &= swold; // filter out new release bits if(swnew) // if "new press" mode = swnew; // save 1000,0100,0010, or 0001 trisc ^= mode; // light the correct mode LED
Last edited by Mike, K8LH; - 6th August 2010 at 16:29.
Maybe I've got something wrong, but wont there be an awful lot of current flowing through those diodes when the buttons are pressed? Or at least, as much as the PIC I/O pin can deliver.
"I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams
Hi Mike, yes, there maybe smaller switches out there, but I couldn't find them - in this scenraio the shaft of the switch needs to extend at least 7 mm vertically....to clear the case top & leave enough protuding for a finger to press on (& I'm not the one imposing the restrictions - it's the guitar that is ...the unit slides under the strings!)
Kamikaze47 - I'm using the PIC's own weak pullups internally (to save using pullup resistors externally) - so for all it looks to be, it's not a 'short' of 4.1V to ground through the diode when the switch is pressed, but a path as follows...
4.1V->PIC Internal Weak Pullup->Diode-> ground. (most of the voltage is dropped across the internal weak pullup)
(and not all the PIC's pins have weak pullups internally ...this was something else that imposed a restriction on me!)
Last edited by HankMcSpank; - 6th August 2010 at 16:17.
Bookmarks