PDA

View Full Version : need help with switch matrix



Srigopal007
- 25th January 2005, 18:03
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

Darrel Taylor
- 25th January 2005, 18:49
Hi Srigopal007,

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

Try this on for size.


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

Srigopal007
- 25th January 2005, 22:57
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

Darrel Taylor
- 25th January 2005, 23:29
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

Srigopal007
- 26th January 2005, 18:59
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.