Need 4 modes with just 3 switches - need all modes to be selectable QUICKLY!


Closed Thread
Results 1 to 40 of 68

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Mike, K8LH View Post
    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.

    Are you using two pins for that tri-color LED? Or are you using any pins for LED mode indicators?

    How are you sampling and managing your switches? Polling them in a loop or via periodic interrupt?

    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 14:40.

  2. #2
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    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
    Attached Images Attached Images  
    Last edited by Mike, K8LH; - 6th August 2010 at 15:29.

  3. #3
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Mike, K8LH View Post
    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.
    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 15:17.

  5. #5
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    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!)
    Ahh, right. I was assuming you were setting that pin high. Makes sense now.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  6. #6
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    What's the purpose of diodes D2-D4 and their connection to RA2, please?

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Mike, K8LH View Post
    What's the purpose of diodes D2-D4 and their connection to RA2, please?
    RA2 (pin 17) on a 16F690 is the external interupt pin. So when a switch is pressed, RA2 is grounded & an interupt occurs - I then check in the interupt subroutine to see which of the three 'switch' connected pins was actually pressed (ie which one of the three switch pin also went low) - the diodes isolate the 'switched' ground from the other two pins not pressed.

    It essentially allows you to implement three physical pins (or more) to be 'psuedo'-interupt pins (vs just the one ext interupt that the 16f690 has)

    All credit to forum members on this thread for filling me in on how to do it....

    http://www.picbasic.co.uk/forum/show...9900#post89900

    Quote Originally Posted by Kamikaze47 View Post
    Ahh, right. I was assuming you were setting that pin high. Makes sense now.
    RA2 (pin 17) is set high...but high via an internal weak pullup (at the risk of teaching grannie to suck eggs, note the lack of the word 'resistor' on the end of that phrase - apparently not a resistor, but some silicon junction internal to the PIC providing the resistance here)

    Just for the record, any similar noobs like me, who may find this thread via a search, here are the 16F690 register settings needed to bring the weak pullups into play (obviously change the 1 & 0s of wht WPUA & WPUB registers to suit your own external connections)

    Code:
    OPTION_REG.7 = 0         'enable weak pullups.
    WPUA         = %00000110  'activate weak pullups on RA1 & RA2.
    WPUB         = %01100000  'activate weak pullups on RB5 & RB6.
    Last edited by HankMcSpank; - 6th August 2010 at 18:41.

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


    Did you find this post helpful? Yes | No

    Default

    The schematic I drew in the thread you linked to above used a PIC without all the extra interrupt inputs, so it has switches ORed to 2 interrupt pins.

    The 16F690 you're using has IOC for all 3 of your switch inputs. So you could get rid of the diodes if you want to save parts & realestate on your board.
    Regards,

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

  9. #9
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,

    I'd not investigated IOC, cos I wasn't sure how to implement! (like I say - n00b alert!) I like the idea of saving 3 x diodes worth of pcb space!

    So, would it go something like thus....

    Code:
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
        INT_Handler    IOC_INT,  _Switch_Interupt,   PBP,  yes  
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    @   INT_ENABLE   IOC_INT     ; Enable 'Int On Change' interrupts
    ********************************************************************
    
    (main body of code)
    
    '********************************************************************
    Switch_Interupt:
    
    if portB.4 = 0 then  mode = 1
    if portB.5 = 0 then  mode = 2
    
    blah blah...
    
    @ INT_RETURN
    Quote Originally Posted by Mike, K8LH View Post
    That's what I thought you might be doin' (OR'ing active lo switch signals to trigger INT).

    Is there some compelling reason you're using interrupts for the switches instead of just polling them at regular intervals?
    Nothing other than wanting my unit's 'mode' to change the instant a switch is pressed. (what originated this thread, was I'd noticed that even holding a button down for a mere 200ms - to determine say mode 3 or mode 4 from one switch - seemed like a lifetime when playing a guitar, so polling three switches is just not gonna cut it - my code uses a whole heap of pauses in the main body of the program, this would hinder such polling)
    Last edited by HankMcSpank; - 6th August 2010 at 19:30.

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