PDA

View Full Version : storing numbers greater than 9 one digit at a time



grkblood13
04-06-2007, 01:15 AM
hey, im running into a problem with my keypad. the problem is storing numbers greater than 9. say the number is 39. they way the key pad works is that it doesnt recognize 39, it recognizesˇ 3 and 9 because of the coding.

is there a way to store 3 in a variable, then store a 9 next to the 3 to make it 39?

allanlane5
04-06-2007, 01:17 AM
Variable * 10 + 9 ' Maybe?

Zoot
04-06-2007, 01:23 AM
How about something like this? This isn't necessarily how you can (or should) do it, but hopefully it will point you in the right direction.





Chars VAR Byte(4) '4 byte array for up to 4 digits
ioByte VAR Byte 'work byte
idx VAR Nib 'index counter

Reset:
idx = 0 'initialize count

Main: 'read up to 4 digits from keypad
'YOUR code here for reading keypad into ioByte
Chars(idx) = ioByte
IF idx < 4 THEN Main

'now you've got 4 characters in the array, show them in the debug screen
DEBUG CLS
FOR idx = 3 TO 0
DEBUG DEC Char(idx)
NEXT

END




Now, if you need two "characters" from the keypad to be available as decimal values elsewhere in the program, you'll need to do a little work to convert the BCD array into something useful.

Is this helpful?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST

stamptrol
04-06-2007, 02:04 AM
I had exactly the same problem. I was using a keypad with a serial interface.

SERIN 6,baud,[SDEC3 keyval] 'stay here until a valid +/- 3 digit number is read

The system will sit and wait for as many characters as determined by "SDEC3". ( here it was for +/- 3 digits.). keyval is a WORD variable.

It just sees each keypress as a different digit and puts them together for you. See SERIN in the Help file.

If you don't have a serial keypad, the previous post will work fine.


Cheers,

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Sisk

http://www.siskconsult.com
ˇ

grkblood13
04-06-2007, 05:49 AM
thanks, im going to try this. i have to go to lab first so i dont have the code in front of me but i think there might be one problem. i am using an enter key which is a carraige return. if this code automatically stores the number after 3 digits is there a way to make that it store when the enter key is pressed. by the way, the keypad works on a logic chart of four pins. the enter key is 1011 or high,low,high,high.

Zoot
04-06-2007, 05:53 AM
Well, I think I know what you mean....





Chars VAR Byte(4) '4 byte array for up to 4 digits
ioByte VAR Byte 'work byte
idx VAR Nib 'index counter

Reset:
idx = 0 'initialize count

Main: 'read up to 4 digits from keypad
'PUT YOUR code here for reading keypad into ioByte
Chars(idx) = ioByte
IF idx < 4 AND ioByte <> %1011 THEN Main

'now you've got 4 characters in the array, show them in the debug screen
DEBUG CLS
FOR idx = 3 TO 0
DEBUG DEC Char(idx)
NEXT

END





Or something like that. The above would stop reading in key presses after 4 strokes or "enter" -- %1011 -- whichever came first. Without knowing precisely how you are reading the keypad it's hard to guage.

Of course there are lots of ways to skin a cat -- the above is just a super-simple example.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST

grkblood13
04-06-2007, 08:36 AM
Thanks for all the replies...I will have to try them when I have a chance to get back to the lab...but just one more question. Does anyone know if you can use the AppMod Header pins as I/O pins? or are they just for add-ons???

grkblood13
04-10-2007, 12:30 AM
ok, heres what im thinking....



keyval VAR Word
keycount VAR Nib
keynum VAR Bit
main:
keycount = 0
keypad:
IF keycount = 0 THEN keyval = keynum
IF keycount > 0 THEN keyval = (keyval * 10 + keynum)

LOW 12
LOW 13
LOW 14
LOW 15
DO
IF (IN15 = 0) AND (IN14 = 0) AND (IN13 = 0) AND (IN12 = 1) THEN press0
IF (IN15 = 0) AND (IN14 = 0) AND (IN13 = 1) AND (IN12 = 0) THEN press1
IF (IN15 = 0) AND (IN14 = 0) AND (IN13 = 1) AND (IN12 = 1) THEN press2
IF (IN15 = 0) AND (IN14 = 1) AND (IN13 = 0) AND (IN12 = 0) THEN press3
IF (IN15 = 0) AND (IN14 = 1) AND (IN13 = 0) AND (IN12 = 1) THEN press4
IF (IN15 = 0) AND (IN14 = 1) AND (IN13 = 1) AND (IN12 = 0) THEN press5
IF (IN15 = 0) AND (IN14 = 1) AND (IN13 = 1) AND (IN12 = 1) THEN press6
IF (IN15 = 1) AND (IN14 = 0) AND (IN13 = 0) AND (IN12 = 0) THEN press7
IF (IN15 = 1) AND (IN14 = 0) AND (IN13 = 0) AND (IN12 = 1) THEN press8
IF (IN15 = 1) AND (IN14 = 0) AND (IN13 = 1) AND (IN12 = 0) THEN press9
IF (IN15 = 1) AND (IN14 = 0) AND (IN13 = 1) AND (IN12 = 1) THEN pressenter
LOOP

press0:
SEROUT 11, 240, ["0"]
PAUSE 1500
keynum = 0
keycount = keycount + 1
GOTO keypad

press1:
SEROUT 11, 240, ["1"]
PAUSE 1500
keynum = 1
keycount = keycount + 1
GOTO keypad

.
.
.

pressenter:
SEROUT 11,240,[DEC keyval]
DEBUG DEC keyval
keycount = 0
PAUSE 1500
GOTO main


i dont see why this wont work but...it doesnt

for example, 12 is displayed as "10", 123 is displayed as "101"

any suggestions?

allanlane5
04-10-2007, 01:22 AM
Ouch!

When you say LOW 12: LOW 13: LOW 14: LOW 15 -- you are setting these to Outputs, and Outputting a 'LOW'.

The IF's then try to read those pins as inputs. This implies you have something on the 'far side' of that pin, trying to drag it high. This won't work, and if you don't have current limiting resistors may damage stuff.

If you're trying to drive a 'matrixed switch array', I think you have to read 4 OTHER wires to get the states of the key-presses.

grkblood13
04-10-2007, 01:25 AM
i set them low because they are reading high. based on the logic chart it DOES read a 6 when 6 is pressed or a 4 when 4 is pressed. when it messes up is when i try doing the multiple number thing. if i take the low commands out it wont decipher each number. itll constantly read everything as high. so i dont think my problem is with the low commands b/c it does decipher each indvidual number when i press them

grkblood13
04-10-2007, 01:27 AM
I GOT IT, for some reason the keynum variable needed to be a word. or bigger than a bit

Post Edited (grkblood13) : 4/9/2007 10:32:20 PM GMT