Picaxe 40X2 Keypad Help...

nick12ab

Senior Member
It's a matrix keypad so what you've got to do is use four inputs (preferably with internal pull-ups) and three bi-directional IOs. Then make each one low then input in sequence and find out which button is pressed. Code coming up from me tomorrow. Don't use high/low, use input/low commands as with high/low, if two keys are pressed at once, that's a high/low short.Alternatively, use the resistor method and 1 adc, but this can have problems if two buttons are pressed at the same time.
 

ChedderCheeser

Senior Member
Ok. Also, this is what I had at first...

Code:
main:

	high b.6
	if pinB.5 = 1 then
		gosub ONE
	else
		gosub RST
	end if
	low b.6
	
	goto main

ZERO:
	low D.5, D.4, C.7, C.5, C.4, D.3
	return
	
ONE:
	low C.7, D.3
	return
	
TWO:
	low C.7, D.4, D.6, C.5, C.4
	return
	
THREE:
	low D.3, D.6, D.4, C.7,  C.4
	return
	
FOUR:
	low D.3, C.7, D.5, D.6
	return
	
FIVE:
	low D.3, C.4, D.5, D.6, D.4
	return
	
SIX:
	low D.3, C.4, C.5, D.4, D.5, D.6
	return
	
SEVEN:
	low D.3, C.7, D.4 
	return
	
EIGHT:
	low D.3, C.4, C.5, C.7, D.4, D.5, D.6
	return
	
NINE:
	low D.3, C.7, D.4, D.5, D.6
	return
	
DECI:
	low D.2
	return
	
RST:
	high D.2, D.6, D.5, D.4, C.7, C.5, C.4, D.3
	return
I connected the the pins to pins B.7-B.1. I had resistors on the four row? pins... I think it was the row pins. maybe it was columb.. idk. The sub-routines are for my 7 segment display. When I press button 1 on the keypad, it displays 1! BUT, if I press 3, it also says 1. And I didnt code it to do that... Is this just a wiring issue? Also, thanks for the code tomorrow... :D
 

westaust55

Moderator
Other alternatives, which can take some of the work load away from the PICAXE chip include:
1. e-Labs EDE1144 keypad encoder chip (expensive)
2. Use a 74HC922 or 923 encoder
 

ChedderCheeser

Senior Member
Thanks for that. Also.. I have a USB keyboard... Can this be used for.. what i'm trying to do? Get a keypad input... I know that there are PS/2 keyboards.. but.. can I use a USB one? Maybe even a USB number pad? Idk...
 

nick12ab

Senior Member
Here's some code:
Let's say that the columns are on B.0-B.2 and the rows are on B.3-B.6 and the columns are scanned. Port B is recommended as pull-up resistors are available internally in the microcontroller so we'll also use those. Code tested in simulator but should also work on a real chip. What it does is send the keypad variable to the terminal if a key is pressed. 15 is used if no key is pressed and it's coded to not send that (15 chosen because it uses less bits of memory than 16).
Code:
#picaxe 18M2
#no_end
#no_data
#terminal 9600
symbol col1 = B.0
symbol col2 = B.1
symbol col3 = B.2
symbol row1 = pinB.3
symbol row2 = pinB.4
symbol row3 = pinB.5
symbol row4 = pinB.6
symbol keypadvar = b4
pullup 111000
setfreq m8
main:
    low col1
    if row1 = 0 then : keypadvar = 1 : goto skip : end if
    if row2 = 0 then : keypadvar = 4 : goto skip : end if
    if row3 = 0 then : keypadvar = 7 : goto skip : end if
    if row4 = 0 then : keypadvar = 11 : goto skip : end if
    input col1
    low col2
    if row1 = 0 then : keypadvar = 2 : goto skip : end if
    if row2 = 0 then : keypadvar = 5 : goto skip : end if
    if row3 = 0 then : keypadvar = 8 : goto skip : end if
    if row4 = 0 then : keypadvar = 0 : goto skip : end if
    input col2
    low col3
    if row1 = 0 then : keypadvar = 3 : goto skip : end if
    if row2 = 0 then : keypadvar = 6 : goto skip : end if
    if row3 = 0 then : keypadvar = 9 : goto skip : end if
    if row4 = 0 then : keypadvar = 12 : goto skip : end if
    keypadvar = 15
skip:    
    input col2
    input col3
    if keypadvar <> 15 then
        serout C.3,N9600_8,(#keypadvar,13,10)
        pause 10
    end if
    goto main
e-Labs EDE1144 keypad encoder chip (expensive)
Was there really any point in suggesting something that costs more than the microcontroller it will be connected to considering that the microcontroller is perfectly capable of doing it itself.

Ron Hackett used the resistor method in his book 'PICAXE Microcontroller Projects for the Evil Genius' and guess what... He used an 08M to be a slave processor to manage that one ADC input and tell the master processor using TWO signal lines! I'm sorry if I'm sounding mean but I just can't get over that.
 

hippy

Ex-Staff (retired)
I know that there are PS/2 keyboards.. but.. can I use a USB one? Maybe even a USB number pad? Idk...
The KBIN command only works with 5-pin mini-DIN PS/2 keyboards.

If the keyboard is designed to work as USB and PS/2 and came with a USB to PS/2 adapter it may work but you cannot simply plug a USB only keyboard into such an adapter. It would be better to use a keyboard fitted with a mini-DIN plug so there's no doubt it will work.
 

westaust55

Moderator
Was there really any point in suggesting something that costs more than the microcontroller it will be connected to considering that the microcontroller is perfectly capable of doing it itself.

Ron Hackett used the resistor method in his book 'PICAXE Microcontroller Projects for the Evil Genius' and guess what... He used an 08M to be a slave processor to manage that one ADC input and tell the master processor using TWO signal lines! I'm sorry if I'm sounding mean but I just can't get over that.
Sure the PiCAXE can do the work internally. As I stated the E-Labs chips reduce workload on the PICAXE and only need 2 pins, 1 for interrupt and 1 for serial data.
The 74HC922/923 in conjunction with. Shift register only needs 3 pins and again can be interrupt driven.

Interrupt means program does not have to wait for a key press.
Both chips avoid situation with 2 keys pressed at the same time and debounce the keys.

I use both mentioned chips and the 1-wire ADC method depending how the program is to run.

Considering Chedder is also asking about USB keyboards where specialty chips would likely be needed all suggestions not sen as out of place. Didn't see a request for the most economical solution.
Expensive can be relative.
 

nick12ab

Senior Member
Didn't see a request for the most economical solution.
Didn't see a request for the most uneconomical solution.

Using that separate driver chip results in:
  • More board space
  • More money spent
Another PICAXE could do it if you insist on using as many chips as possible but the keypad scanning operation only needs to happen when the data is wanted.
 

westaust55

Moderator
but the keypad scanning operation only needs to happen when the data is wanted.
True in some applications, but in other cases the user may want the program to accept data at any time.
That data can be either put into a buffer or be used later or instantly to control the PICAXE program to change operation.

Other than "how do I use this keypad" we have no idea how the OT proposes to use the keypad or the project/application.
 

nick12ab

Senior Member
we have no idea how the OT proposes to use the keypad or the project/application.
Who's OT?

I assume you mean the thread starter so...
ChedderCheeser, what do you want to use the keypad for?

P.S. if background receive is required, use a 14M2 and the hardware serial (background) on the master to receive data from the 14M2.
 

ChedderCheeser

Senior Member
Well, I was going to use it to control a 7 segment display. Thats why I had the subroutines for the display in the code that I posted. :) I do like the idea of having a seperate chip... Saves me some I/O pins that I might need... Maybe I could use my 20M2 to do it and still have extra I/O. First of all, I need to fix my programmer. :p Its on a breadboard... so... yeah. :D Anyway, Thanks for all the help. I think I will fisure it out soon. Also, can someone tell me why ... in an earlier post, I posted some code. Can someone tell me why the display showed '1' when I pressed '1' OR '3' on the keypad? THX!!!
 

nick12ab

Senior Member
Well, I was going to use it to control a 7 segment display
For this application, it would be better to use a solution that is reasonably fast (if not multiplexing) or very fast (if multiplexing) so an external chip unless it uses hardware comms won't be desirable so the best would probably be either scan or ADC with resistors.

in an earlier post, I posted some code
Which code? Post #3?

I'm surprised no one has mentioned Wilf's work...
An overkill for twelve buttons - see I don't need to use the number keys since twelve isn't much!
 
Top