Keypad to PS2 16F88

Thread Starter

radioguy90

Joined Jun 27, 2012
7
I have a project to take a 4x4 keypad and when a key is pressed the make/ break codes for some specific PS2 keyboard letters and functions are generated.

After being overwhelmed by the thought of writing code that scans rows and coumns I have decided to use the 74C922 and interfcae that to a 16F88.

My question; what is a good reference source that would assist me in writing the code to take the 4 bit binary output from the 74C922 and generate the ASCII Make and Break codes to simulate a PS2 keyboard for 16 specific letters/functions?

I have been looking at various textbooks and have been overwhelmed. I need something that explains storing the 4 bit binary data, reading the binary data and calling a sub-routine to generate the make and break codes. Explaining functions such as RTLW, Read Data, move to register etc. would be extremely helpful


I know the output matrix for the74C922, and the scan codes for the make and break hex code for the 16 keys I want.

Have looked at similar resources on the forum but am looking for something that would guide me to be able to understand the arcane codes and logic sequence.

Any suggestions would be helpful.


radioguy90
 

MrChips

Joined Oct 2, 2009
30,824
Scanning the 4x4 keypad is the easy part. Emulating a PS2 keyboard is even more difficult.
Hence if you really want to do this I would drop the 74C922 and buckle down to some serious programming.

So the first step is to scan the 4x4 keypad. This is actually easier than you think.
You will need two 4-bit ports. Four bits will be inputs and four bits will be outputs. The four bits can be from the same single 8-bit port.

If you are willing to go all the way I can lead along the way.
 

Thread Starter

radioguy90

Joined Jun 27, 2012
7
Thank You for the offer of help -- my initial question can you recommend a good resource textbook or reference book to better understand the commands/codes etc.

I have been accummulating a lot of info on keypads and decoding but without a magic decoder ring it is still pretty much a mystery.
 

MrChips

Joined Oct 2, 2009
30,824
I have not personally come across a reference to scanning keypads but the concept is fairly straight forward.

Here is a schematic for a 2x2 keypad as an example:



The buffers shown are for illustrative purposes only. These will be replaced with the actual ports of the MCU. Only 2x2 keypads are shown. In your case there will be 4x4 keypads.
There are two ways to wire the 4-bit input port pins, either with pull-down resistors or with pull-up resistors. The schematic shows pull-up resistors and this would be the preferred choice if we were using TTL gates.

Four output bits would be programmed to output 0000.
When no key is pressed, the input pins will read back 1111.
If any key is pressed one of the 1's will read back as 0.
This is first recognition that a key is pressed.

Are you with me so far?
 

Thread Starter

radioguy90

Joined Jun 27, 2012
7
Lets see if U1A and U1B would be programmed to be 0 then pushing S1 would cause U2A to go to 0. Would not the same condition apply if S3 would be pushed and you would read 0 on U2A. I assume if you were scanning by rows and were in the U1B row and saw a 0 on U2A then you would know that S3 has been depressed. If you scanned the U1A row and saw a 0 on U2A, then S1 was depressed.

Is that the answer?
 

MrChips

Joined Oct 2, 2009
30,824
That's correct, but you're jumping the gun.
If you already know how to scan a keypad then I wouldn't bother to go through the steps.
We're only at step #1, recognizing that a key was pressed.
 

Thread Starter

radioguy90

Joined Jun 27, 2012
7
I don't know how to scan a keypad but was relating to some of the literature that I saw that talked about scanning rows and columns. That is the part I don't know how to do and that is why initially was looking at the 74C922
 

MrChips

Joined Oct 2, 2009
30,824
Ok, when the input bits return a zero in one of the bits, you have narrowed it down from 16 switches to 4 switches, that is, we know which column the switch is in.

So to find which of the four has been pressed we scan the rows as follows:

Send 0111 and read back the inputs
if 1111, send 1011 and read back the inputs
if 1111, send 1101 and read back the inputs
if 1111, send 1110 and read back the inputs

From those four attempts, one of them has to read back something other than 1111, i.e. you should get the pattern as in the very first step. This will tell you which row the switch is in and you have found the switch.

If you can follow this, I will write this out as an algorithm (or as a flow chart), maybe tomorrow, unless you are fairly certain that you get it. (It's getting late, maybe not yet for you on Pacific time).

We also have to debounce the key-switches.
 

Thread Starter

radioguy90

Joined Jun 27, 2012
7
this is where I don't understand what is happening. I see where you would detect a 0 at the input signifying which column. The scanning of the rows and how that data is sent and read is unclear to me.

thinking out loud. Suppose the rows are 1,2,3,4 and on the first pass 1=0, 2=1, 3=1 and 4 =1. Then on the second pass 1=1, 2=0, 3=1, 4=1. On the third pass 1=1, 2=1, 3=0 and 4=1. On the fourth pass 1=1, 2=1, 3=1, 4=0. Is that how the scanning of the rows is done and is this being done while the switch is still closed? [Actually you may not need 4 passes as the detection might occur on the first pass.]

in the column where the switch is closed it is reacting to the "0" from the switch in a row. How does data being sent detect that the switch is closed and thus the row to give you something other than 1111 on the input? I can see where a 1 in the row and a 1 in the column will give a 1 at the input. I am assuming it is the 0 that is detecting the correct row. But how does the closed switch play in with the data being sent?

i don't understand the statements "if 1111, send 1011 and read back inputs" *** that you should get back a pattern as in the first step (like 0111)

My fundamental problem is understanding how the data is sent in the rows, how that interacts with the switch closure and how that is read. If there is a 0 in row 2 and you have a 0 show up on the third column then you know it is the switch located in the second row third column assuming it remains closed. A 0 in column 3 could be 4 switch positions how does sending the 4 codes know which one unless this occurs while the switch is closed.

as you can see I am really new at this and very confused.
 

Thread Starter

radioguy90

Joined Jun 27, 2012
7
After a bit of sleep perhaps things are making more sense to me. Then again I may only be deeper in the water and sinking faster.

The scans have to take place while the key is pressed which puts a 0 on one of the input columns as the row state (output) is at 0 as the key closure brings down the pull up to 0 and the input goes to 0. By scanning the rows where the key is depressed, in three cases the row is being forced to a "1". So the switch closure does not bring down the pull up thus you would get a 1111 on the inputs for three cases.

In the case of the 0 being put on the row where the key is, the pull up is taken to 0 and the input sees a 0. Then you would know which row. The key debounce comment makes the case that when the key is depressed it must stay depressed (and not bounce around) for a sufficient time for the scan to take place.

Is that closer to what is happening?
 

BMorse

Joined Sep 26, 2009
2,675
4x4 switch matrix.png



Your assumptions are correct, but I would pull the columns down with a 10k resistor, you can then group the 4 columns into a nibble variable (4 bits), that way you can read all 4 columns at once for every row scan.....


you will use up 8 bits (I/O's) on a microcontroller just for the 4x4 matrix, 4 for the columns which should be set up as inputs, and 4 for the rows which should be set as outputs, keep all row outputs low until you want to scan for a keypress, then turn on Row1 get your 4 bit reading, compare the reading to a table to determine which keys are pressed on that row (example = 1st and 4th key is pressed on the first row = 1001 on inputs, which is 0x09 in hex).... then turn that row off, and turn on the next row and get your reading, and so forth...... you want to keep reading the whole matrix even if you already detected a keypress that way in case multiple keys are pressed on different rows you will still detect those also..... this way you can scan the whole matrix with just 4 passes :)





and also, here is some light reading on the PS2 keycodes and possibly how you can interface to the PC and send the data from the Pic. http://www.piclist.com/techref/microchip/io/dev/keyboard/ps2-jc.htm
 

MrChips

Joined Oct 2, 2009
30,824
You can use pull-ups or pull-downs. Pull-downs are easier to follow because we would discuss the scan operation using POSITIVE LOGIC.

If you were using TTL series logic gates you would use pull-ups because the pull-down resistors would have to be less than 200Ω.

Many MCUs allow you to enable weak pull-ups thus eliminating the need for external pull-up resistors.
 

MrChips

Joined Oct 2, 2009
30,824
We have to make sure the op fully understands the scan procedure before going any further.

We will use the following example and make some assumptions.
We will use the lower 4-bits of PORTA configured as OUTPUTS connected to 4 rows.
We will use the lower 4-bits of PORTB configured as INPUTS connected to 4 columns with external 10kΩ pullup resistors.

(This is only for demonstration purposes. In actual implementation, the total of 8 bits can be assigned to a single 8-bit port.)

Assume that switch at row-2 column-3 is pressed (rows and columns are numbered 1 to 4, where column-4 and row-4 are connected to the LSB of PORTA and PORTB respectively.

We output PORTA = 0000
We read PORTB = 1101
This tells us that the switch is on column-3.

We output PORTA = 0111
We read PORTB = 1111

We output PORTA = 1011
We read PORTB = 1101

This tells us that the switch is on row-2.
It also confirms that the switch is still on column-3.

We have found our switch at row-2, column-3.

(If we were to use pull-down resistors, we simply invert all the 0s and 1s above).

We will discuss switch de-bounce later once you follow the above.
 

Thread Starter

radioguy90

Joined Jun 27, 2012
7
I think I might be seeing some light at the end of the tunnel and hopefully it is not a train.

Today I made a drawing just so I could get a better feel of what was happening. I think it follows what you said -- I used all PORT B. Just in case the insert picture didn't work http://www.jessystems.com/keypad.html

Since I made this drawing before I read your post I may not have the numbering convention correct. So that is another thing to get straight.



Thanks for the link re:interfacing the PIC to a computer I will need to look at that link. Thank you also for your patience and help.
 

MMcLaren

Joined Feb 14, 2010
861
If you fellows are going to promote/endorse a keypad method that drives four row outputs at the same time, you should probably mention to the OP why he should place diodes on the four output lines (or caution him against ever pressing more than one key in the same column at one time)... Myself, I use internal weak pull-ups on the entire port with only one active low output at a time if I don't want to include diodes in the circuit.

Cheerful regards, Mike
 
Last edited:
Top