Keypad subroutine
Archiver
Posts: 46,084
Any Ideas on how I can take the program below and put it into some type of
subroutine so that when a key is pressed my stamp will go and get the value.
Input 12 is a keystrobe so when this input goes low that means a key has been
pressed. I have a menu driven system that pretty well uses four keys unless
your entering data and I can't seem to get the value and return back to where I
was in my menu.
'{$STAMP BS2e}
'=========Define names for LCD instructions, bps=======
CLRTRM con 12 ' Clear
entire LCD screen.
star con "*" '
Backspace key
pound con "#" ' Enter
key
ESC con 27 '
Escape character
BAUD con $054 ' 9600
baud, BS2.
'=========Define variable(s)===========================
keyIn var byte ' Digit
entered at keypad.
numVal var word ' Numeric
value input from keys.
' =================Begin demonstration=================
begin:
pause 5000 ' Wait a
sec. ' Clear the screen and print text.
serout 10,BAUD,[noparse][[/noparse]CLRTRM,"Enter number:",cr]
numVal = 0 ' Clear
numVal to accept new value.
'
Now enter a loop that checks TRM keystrobe (on BS2
'
pin P2) for key-press indication (0).
again:
input 12 ' Set
keystrobe input.
pause 50 '
Simulate other processing.
if in12 = 1 then again ' No key
press--loop.
serin 11\12,BAUD,[noparse][[/noparse]keyIn] ' Get number
if keyIn = star then backspace
if keyIn = pound then enter
if keyIn < "0" then again ' Reject
non-numbers
if keyIn > "9" then again
'
The next line multiplies numVal by 10 (shifting its
'
value one decimal position to the left), and adds
'
the value of the current key. Subtracting "0"
'
converts the key's ASCII code to its numeric value.
numVal = numVal * 10 + (keyIn - "0")
serout 10,BAUD,[noparse][[/noparse]keyIn]
goto again
' If
the backspace key (*) is pressed, divide accumulated
' numVal by 10 (to remove ones-place digit value) and
'
echo a backspace character to the display.
backspace:
numVal = numVal/10
serout 10,BAUD,[noparse][[/noparse]BKSP]
goto again
' If
the enter key (#) is pressed, clear the screen and
'
announce the value of numVal. Then wait for a keypress
' to
resume the program from the beginning.
enter:
serout 10,BAUD,[noparse][[/noparse]CLRTRM,"You entered: ", DEC numVal,cr,cr]
serout 10,BAUD,[noparse][[/noparse]"Press any key",cr, "to continue"]
input 12 ' Set
keystrobe input.
hold:
if in12 = 1 then hold ' No key
press--loop.
pause 100 ' pause
to discard key input.
goto begin
[noparse][[/noparse]Non-text portions of this message have been removed]
subroutine so that when a key is pressed my stamp will go and get the value.
Input 12 is a keystrobe so when this input goes low that means a key has been
pressed. I have a menu driven system that pretty well uses four keys unless
your entering data and I can't seem to get the value and return back to where I
was in my menu.
'{$STAMP BS2e}
'=========Define names for LCD instructions, bps=======
CLRTRM con 12 ' Clear
entire LCD screen.
star con "*" '
Backspace key
pound con "#" ' Enter
key
ESC con 27 '
Escape character
BAUD con $054 ' 9600
baud, BS2.
'=========Define variable(s)===========================
keyIn var byte ' Digit
entered at keypad.
numVal var word ' Numeric
value input from keys.
' =================Begin demonstration=================
begin:
pause 5000 ' Wait a
sec. ' Clear the screen and print text.
serout 10,BAUD,[noparse][[/noparse]CLRTRM,"Enter number:",cr]
numVal = 0 ' Clear
numVal to accept new value.
'
Now enter a loop that checks TRM keystrobe (on BS2
'
pin P2) for key-press indication (0).
again:
input 12 ' Set
keystrobe input.
pause 50 '
Simulate other processing.
if in12 = 1 then again ' No key
press--loop.
serin 11\12,BAUD,[noparse][[/noparse]keyIn] ' Get number
if keyIn = star then backspace
if keyIn = pound then enter
if keyIn < "0" then again ' Reject
non-numbers
if keyIn > "9" then again
'
The next line multiplies numVal by 10 (shifting its
'
value one decimal position to the left), and adds
'
the value of the current key. Subtracting "0"
'
converts the key's ASCII code to its numeric value.
numVal = numVal * 10 + (keyIn - "0")
serout 10,BAUD,[noparse][[/noparse]keyIn]
goto again
' If
the backspace key (*) is pressed, divide accumulated
' numVal by 10 (to remove ones-place digit value) and
'
echo a backspace character to the display.
backspace:
numVal = numVal/10
serout 10,BAUD,[noparse][[/noparse]BKSP]
goto again
' If
the enter key (#) is pressed, clear the screen and
'
announce the value of numVal. Then wait for a keypress
' to
resume the program from the beginning.
enter:
serout 10,BAUD,[noparse][[/noparse]CLRTRM,"You entered: ", DEC numVal,cr,cr]
serout 10,BAUD,[noparse][[/noparse]"Press any key",cr, "to continue"]
input 12 ' Set
keystrobe input.
hold:
if in12 = 1 then hold ' No key
press--loop.
pause 100 ' pause
to discard key input.
goto begin
[noparse][[/noparse]Non-text portions of this message have been removed]