PDA

View Full Version : Programming problem while interfacing keypad with pic 16f877a



uaf5000
- 20th July 2010, 17:52
Hey guys,
I am trying to interface 4x4 keypad with my pic and lcd. Everything is working fine except the last row of my keypad (i.e keys from 13-16) is not displayed on lcd. There is no problem in the hardware as i checked the keypad replacing rows with columns. I think there is some problem in the program. Can you guys help me out with this?

Regards.

Below is the program which i used:



' Define LCD connections
DEFINE OSC 20
DEFINE LCD_DREG PORTD
define LCD_DBIT 0
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
DEFINE LCD_RWREG PORTE
DEFINE LCD_RWBIT 2
DEFINE LCD_BITS 8
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
' Define program variables
col var byte ' Keypad column
row var byte ' Keypad row
key var byte ' Key value

OPTION_REG.7 = 0 ' Enable PORTB pullups
ADCON1 = 7 ' Make PORTA and PORTE digital

Low PORTE.2 ' LCD R/W low (write)

Pause 500 ' Wait for LCD to start

Lcdout $fe, 1
pause 250
lcdout "Press any key" ' Display sign on message

loop: Gosub getkey ' Get a key from the keypad
Lcdout $fe, 1, #key ' Display ASCII key number
Goto loop ' Do it forever

' Subroutine to get a key from keypad
getkey:
PAUSE 50 'Debounce key-input
getkeyu:' Wait for all keys up
PORTB = 0 ' All output-pins low
TRISB = $f0 ' Bottom 4-pins out, top 4-pins in
IF ((PORTB >> 4) != $f) THEN getkeyu'If keys down, loop
PAUSE 50 ' Debounce key-input

getkeyp:' Wait for keypress
FOR col = 0 TO 3 ' 4 rows in keypad
PORTB = 0 ' All output-pins low
TRISB = (DCD col) ^ $ff ' Set one row pin to output
row = PORTB >> 4 ' Read columns
IF row != $f THEN gotkey' If any keydown, exit
NEXT col
GOTO getkeyp ' No keys down, go look again

gotkey: ' Change row and column to key number 1 - 16
key = (col * 4) + (NCD (row ^ $f))
'NOTE: for 12-key keypad, change to key = (row * 3)
RETURN ' Subroutine over

END