Hello Guys
i am using the example from melab keypad entry to enter numbers like 2 digits,3 digits, 4 digits etc
i modifiad the program below for 3 digits but know there has to be a better way of doing this.
this program relies on the user entering a 3 digit number but want to make it flexible so that any number of digits between 1 to 5 can be entered .
Any help would be appreciated

Toni





'************************************************* ***************
'* Name : keypad.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2004 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 9/27/04 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************

DEFINE LOADER_USED 1 ' uses a bootloader
Include "Modedefs.Bas"


Define OSC 20
clear

' Setup Hardware for uart
DEFINE HSER_BAUD 115200
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1


DEFINE LCD_DREG PORTD ' Use Portd for LCD Data
DEFINE LCD_DBIT 4 ' Use Upper(4) 4 bits of Port
' PORTD-4 thru PORTD-7 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTE ' PORTE for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 0 ' PORTE-0 pin for LCD's RS line
DEFINE LCD_EREG PORTE ' PORTE for Enable (E) bit
DEFINE LCD_EBIT 1 ' PORTE-1 pin for LCD's E line

DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)

' ** Define LCD Control Constants **

I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2

' Define program variables
col var byte ' Keypad column
row var byte ' Keypad row
key var byte ' Key value

bigword var word
NUM VAR BYTE[3]
x VAR BYTE

' Initialise Hardware
' -------------------
ADCON1 = 7 ' Make PORTA and PORTE digital
TRISA=%00000000
TRISB=%00000000
TRISC=%10000000
OPTION_REG.7 = 0 ' Enable PORTB pullupsTRISD=%00000000



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

lcdout I,Clr:Pause 30
lcdout I,Line1," KEY PAD TEST "
lcdout I,Line1,"..Power UP.. !!"
pause 500


loop:
lcdout I,Clr:Pause 30
lcdout I,Line1, "Press any key" ' Display sign on message
gosub getword
lcdout I,Clr:Pause 30
lcdout I,Line2,#bigword
pause 1000
Goto loop




' Subroutine to get a key from keypad
getkey:
Pause 50 ' Debounce

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 any keys down, loop

Pause 50 ' Debounce

getkeyp:
' Wait for keypress
For col = 0 to 3 ' 4 columns in keypad
PORTB = 0 ' All output pins low
TRISB = (dcd col) ^ $ff ' Set one column pin to output
row = PORTB >> 4 ' Read row
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))
Return ' Subroutine over
getword:
for x = 1 to 3
Gosub getkey ' Get a key from the keypad
lcdout I,Line2+x,#key ' Display ASCII key number
NUM[x]=key
next
bigword= NUM[1]*100 + (NUM[2]*10)+(NUM[3])

return

End