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