PDA

View Full Version : What am I doing wrong?



tazntex
- 12th July 2010, 19:44
On Bruce's website he has an example of a sixteen key serial keypad, I am trying to modify it for my use on PortB.
On PortB using a 16f628a, I have my rows on PortB.1-3 and my Columns are 4-7, so using this particular piece which by the way works very well using it as Bruce's example shows on his web page:
FOR row = 0 TO 3 ' 4 rows in keypad
PORTB = 0 ' All output-pins low
TRISB = (DCD row) ^ $ff ' Set one row pin to output
col = PORTB >> 4 ' Read columns
IF col != $f THEN gotkey' If any keydown, exit
NEXT row

gotkey: ' Change row and column to key number 1 - 16
key = (row * 4) + (NCD (col ^ $f))
'NOTE: for 12-key keypad, change to key = (row * 3)
It had to be adjusted to compensate for for the keypad shifting left one bit

FOR row = 1 TO 3 ' I changed this since my row is actually using bit 1-3 is this correct???
'row is a variable and 1 to 3 would mean bit 1-3 correct and how can I sum up
'to account for bit 0 not being used?
PORTB = 0 ' All output-pins low
TRISB = (DCD row) ^ $fe ' I changed this to $fe so that PortB.0 will be low when the bits are flipped "^$fe"
col = PORTB >> 4 ' Read columns
IF col != $f THEN gotkey' If any keydown, exit
NEXT row

gotkey: ' Change row and column to key number 1 - 16
key = (row * 3) + (NCD (col ^ $f)) 'As noted above I am only using three rows instead of four
Either way I am coming out with duplicate numbers like key = 5 etc.

What am I doing wrong?
Thanks again

mackrackit
- 12th July 2010, 22:29
Not sure but

key = (row * 4) + (NCD (col ^ $f))
'NOTE: for 12-key keypad, change to key = (row * 3)

And please use code tags, makes it easier to read...

tazntex
- 12th July 2010, 22:47
Thanks Mackrackit, I tried that but that doesn't work right either. I first thought since the orginal code had "row = 0 to 3, and I am using TRISB = (DCD row) ^ $fe that it would seem that I would have to state "row = 1 to 3" the $fe in TRISB = (DCD row) ^ $fe allows for when I flip the bits to set only one to output bit 0 becomes an input, I am using bit 0 for my interrupt in which when no key is pressed it goes to sleep and right before I go back to the main program I do this:
CMCON=%00000111
vrcon = 0
pauseus 10
TRISB = $fe ' B7-B1 in, B0-out
PORTB = %00000001 ' All output-pins low except bit 0
pauseus 10
intcon.1 = 0 'clears flag
resume
then my main program is this:

enable
cycle:
pause 1
@ Sleep
@ nop
@ nop
goto cycle

I must admit I am confused about row = 1 to 3, that does mean bit 1-3 in the variable Row doesn't it? if so I thought perhaps I could shift by one bit and account for the change, key = ((row >> 1) * 3) + (NCD (col ^ $f) but that doesn't work either. Anyhow I haven't abandoned the ship yet:)

Again, Many thanks for your reply

mackrackit
- 12th July 2010, 23:10
I am still probably not seeing the problem

FOR row = 0 TO 3 ' 4 rows in keypad
In your case

FOR row = 0 TO 2 ' 3 rows in keypad

Or maybe it is wired wrong... :confused:

tazntex
- 12th July 2010, 23:26
I double checked everything with my Fluke:) my rows are on Portb. 1,Portb.2,and Portb.3 my columns are on Portb.4 - 7. I had everything setup like the orginal example on Bruce's site and it worked great but I wanted to use Portb.0 for my interrupt, thats when I moved everthing over one bit. It works, about eight functions work correctly but now I can send on two different buttons things like two decimal 7's or 5's , etc. instead of decimal 1-12. When I break the code down on paper to calculate what the output would be it concurs the same thing so thats why I was wondering how I could adjust the code, as far as I can see it has to do with the line "for row = 1 to 3" thats why I asked if 1 to 3 does refer to bit 1-3 in the variable row, and if so how can I compensate for three rows that are now shifted by one bit. I tried to see if ((row >> 1) *3) + (ncd (col ^$fe)) but about the same results.

Thanks again.

mackrackit
- 12th July 2010, 23:52
OK, now I think I see what you are doing. Me a little slower than normal today...


row = PORTB & %00001110
Should isolate bit 1 ,2, and 3.

tazntex
- 13th July 2010, 13:15
Well I tried that also, but that doesn't work either. I believe I mentioned earlier that I in my mind I was thinking since I moved the rows and columns over by one bit why don't I just create another variable such as:

row1 = row >> 1

then I could still use the same formula:

key = (row * 3) + (NCD (col ^ $f)) ' (row * 3) because I have three rows and when I return the priority bit
'of the columns after I flip the state of the bits ^$f that will tell me which
' column is actually the one being used at that moment of time as an output.

But I tried it and still have not gained any ground.

hmmm, back to the drawing board.

Thanks again...

tazntex
- 13th July 2010, 15:43
If I substitute this:

if row = 1 && col.0 = 0 then key1 = 1
if row = 1 && col.1 = 0 then key1 = 2
if row = 1 && col.2 = 0 then key1 = 6
if row = 1 && col.3 = 0 then key1 = 7
if row = 2 && col.0 = 0 then key1 = 5
if row = 2 && col.2 = 0 then key1 = 10
if row = 2 && col.3 = 0 then key1 = 11
if row = 3 && col.0 = 0 then key1 = 3
if row = 3 && col.1 = 0 then key1 = 4
if row = 3 && col.2 = 0 then key1 = 8
if row = 3 && col.3 = 0 then key1 = 9

for this:
key = (row1 * 3) + (NCD (col ^ $f))

everything works great but isn't there a simpler way?

Thanks