Simple Interfacing of 4x4 Keypad to PIC16F873
Okay, the initial plan was to use a PIC16F873A, but somehow, I accidently killed my 873A when the power and ground wires accidently got shorted. And... the 873A 'dieded' = ( . It can still be programmed and read from but it refuses to run the codes that are flashed on to it. Instead the DC voltages at the OSC1 and OSC2 pins reads 5.35V and 0V respectively. I probed both pins with an oscilloscope and all I see was noises :S.  So my guess is, that internal oscillation circuit must have somehow got fried by the 1 second short circuit. = (. Poor PIC16F873A,  she went before her time... = (.

Alright, now that the sad story is done with. Let's get down to business! I replaced the PIC16F873A with its older PIC16F873. They are practically the same except that for my PIC16F873 runs at the max speed of 4MHz, as written on the IC, PIC16F873-04.

The 'traditional' way of interfacing the keypad to the microcontroller is to hook up all the 7 pins of rows and columns via a pull-up resistor to Vcc to the microcontroller itself. Then, we write a code to scan rows and columns and scan... again...and again...zzzz...until finally a column gets HIGH and we check the which row went high to determine the key that was hit. Then we also have to consider for keypad debouncing effect whenever someone presses or releases a key. That can be solved using capacitors but it is doesn't work all the time. ><

A better way to interface a keypad to the PIC is to use a keypad encoder in between the keypad and the PIC. One encoder introduced to me by FJ (who is a friend of mine in the field of electrical engineering too.) is the MM74C922 Keypad Encoder which datasheet can be found
here. Simply hook up all the row and column pins directly to the encoder and it will output a HIGH pulse on the Data Available pin whenever a key is pressed. The keyboard mask capacitor will handle all the debouncing effect so there is no need to consider this factor when programming the PIC. After that, the encoder will output 4 bits of data to the PIC. Therefore, in our code, all we have to do is wait till the Data Available pin goes HIGH and then read the 4 bits and decode the key in our programme. For safety, we should also wait till the Data Available pin goes low before processing the next key to avoid 'multiple inputs' from the same key earlier.

That is the PROs of this method of interfacing. The CONs? Well, so far the only CON i found was that the MM74C922 isn't too cheap like any typical 18 pin IC. I bought it for about .... ummm...  US$4.50 or SEK30.00 in Stokholm, Sweden. =)
Jameco sells it for alittle more expensive, I think. = (

Also, in this Pet Project the codes for the LCD is also further optimized again. This time the check BUSY flag method is implemented instead of calling delay routines each time when outputting a character. It seems that we cannot call the Enable Pulse when reading the BUSY flag, instead we have to hold it HIGH when we want to read the flag then set it LOW after we copy the data to a register on the PIC. Will look more into this =). For now I shall use the some of the codes written by Peter Ouwehand from the Netherlands. Credits to Peter Ouwehand for his codes on the LCD. Really efficient. Check his site out over
here! His code is written for 16C84 but it can be easily ported over to any PICs. Not a problem! =).

As usual, the source code, the schematics and some pictures. =)

Source code :
lcdpad.asm
Last updated : 6th April 2005, 12.20am Eastern Time by Edwin Nam Chun Mui
Back to main page
1