need help with switch matrix


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78

    Default need help with switch matrix

    hi everyone, I am trying to program a switch matrix using PortD on my PIC of choice. the switch matrix is as follows.. it;s a 3 column X 5 row. with the following schematic

    I have the TRISD registers set up so that the columns are outputs while the rows are inputs
    TRISD = %11111000 'pin0-2 output (columns); while pin3-7 input(rows)


    Col : 0 1 2
    ==============
    1 2 3 (ROW0)
    4 5 6 (ROW1)
    7 8 9 (ROW2)
    10 11 12 (ROW3)
    13 14 15 (ROW4)

    I have looked at the keyx.bas file on the melabs website and have found it quite useful but I think I am masking my bits incorrectly since I am not getting the correct results.

    here is the modified code from the keyx.bas from the melbas website. hope someone can help me figure this out. I think I ma getting my rows mixed up with my columns when it comes to masking the bits. Any help towards the right direction would be greatly appreciated.. thank you

    here is the code :


    col var word ' Keypad column
    row var word ' Keypad row
    key var word ' Key value
    i var word

    getkeyu:
    ' Wait for all keys up
    PORTD = 0 ' All output pins low
    TRISD = $f0 ' Bottom 4 pins out, top 4 pins in
    If ((PORTD >> 4) != $f) Then getkeyu ' If any keys down, loop

    Pause 50 ' Debounce

    getkeyp:
    ' Wait for keypress
    For col = 0 to 4 ' 4 columns in keypad
    PORTD = 0 ' All output pins low
    TRISD = (dcd col) ^ $ff ' Set one column pin to output
    row = PORTD >> 3 ' Read row
    If row != $1f Then ' If any keydown, exit
    key = (col * 3) + (ncd (row ^ $1f))
    endif

    Next col

    Goto getkeyp ' No keys down, go look again

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


    Did you find this post helpful? Yes | No

    Default

    Hi Srigopal007,

    Assuming you have Pull-Up resistors on PORTD.3,4,5,6 and 7

    Try this on for size.

    Code:
    col var word ' Keypad column
    row var word ' Keypad row
    key var word ' Key value
    i var word
    
    getkeyu:
    ' Wait for all keys up
        PORTD = 0                   ' All output pins low
        TRISD = $F8                 ' Bottom 3 pins out, top 5 pins in
        If ((PORTD >> 3) != $1f) Then getkeyu   ' If any keys down, loop
    
    Pause 50 ' Debounce
    
    getkeyp:
        ' Wait for keypress 
        For col = 0 to 2               ' 3 columns in keypad
            PORTD = 0                  ' All output pins low
            TRISD = (dcd col) ^ $ff    ' Set one column pin to output
            row = PORTD >> 3           ' Read row
            If row != $1f Then         ' If any keydown, exit
                key = (col * 5) + (ncd (row ^ $1f))
            endif
        Next col
    
    Goto getkeyp                     ' No keys down, go look again
    The key numbering is different than you expected too.
    <font size=3><pre>Col : 0 1 2<br>==============<br> 1 6 11 (ROW0)<br> 2 7 12 (ROW1)<br> 3 8 13 (ROW2)<br> 4 9 14 (ROW3)<br> 5 10 15 (ROW4)</pre></font>HTH,
    &nbsp;&nbsp;&nbsp;Darrel

  3. #3
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    Hi, I notice that the above posted solution only can take in one number at a time. what for instance you want to pick #5 and and #14 at the same time. that would be quite impossible. Is this a hardware limitation... or can the switch matrix handle two intputs at the same time. like #5 and #14.... if so how different/difficult would the code become?

    srig

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


    Did you find this post helpful? Yes | No

    Default

    Sure, that can be done. But, it can be a bit complicated.

    It's next to impossible to hit both buttons at exactly the same time. So, you have to put in some kind of time delay. If you detect 1 button press, wait a certain amount of time to see if another one is also pressed, otherwise just go with the first one as a single press.

    Of course, this means the program is Stopped while waiting to see if a second button is pressed. That can be a bit annoying with a perceived delay after each key press. It'll make things feel a bit sluggish. If you try to make it faster, the person pressing the buttons may not get the second button down fast enough, causing the program to do the wrong thing.

    If you limit the double keypress to only work with one button, maybe it's labeled "Function" or something. Then the program only has to wait for the second button if the "Function" button is pressed first. Otherwise it just accepts the single button immediately, without the delay problem.

    Food for thought.

    Best regards,
    &nbsp;&nbsp;&nbsp;Darrel

  5. #5
    Join Date
    Jul 2004
    Location
    CA
    Posts
    78


    Did you find this post helpful? Yes | No

    Default help with switches

    I was wondering if there are sample code out there that can give me something to look at as reference ... for something like this.. For example if one button is pressed in the switch matrix... and then later pressing another button while keeping the first one pressed.

    Key #1 is pressed .. and then later .. key#4 is pressed while key#1 is pressed.

    can someone help me out. I was thinking of putting them in an array and then constantly checking the array to see if it changes.. but that might be a dead end.

    Any help on this would be greatly appreciated.

Similar Threads

  1. Keypad/Membrane Switch
    By damien in forum General
    Replies: 2
    Last Post: - 13th October 2009, 10:02
  2. Keyboard Matrix with a Twist
    By k3v1nP in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 29th October 2008, 10:51
  3. Newbie - 16F628A and switch latching
    By malc-c in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 19th May 2006, 02:35
  4. Switch Sequence
    By Tissy in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 13th February 2005, 20:36
  5. Switch Polling vs Interrupts
    By eric blair in forum General
    Replies: 1
    Last Post: - 17th October 2003, 09:38

Members who have read this thread : 1

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