Shop OBEX P1 Docs P2 Docs Learn Events
get trouble — Parallax Forums

get trouble

ionion Posts: 101
edited 2004-08-10 15:52 in BASIC Stamp
Hello ,
I have trouble reading 2 words from the scratchpad ram with get.confused.gif
I declared two variable words V1 and V2
I managed to put on the scratchpad from 0 to 8 eight digits as follow:
0 1 2 3 4 5 6 7··· location on the scratchpad ram
1 2 3 4 5 6 7 8··· numbers at that location. I know they are there as i can see them·with debug when i enter them one by one.
I need to read now the V1 as =1234 and v2=5678
I used GET 0,Word V1 and
········ GET 5,Word V2,
but what i get is V1=0513 and V2=1541
What·am I doing wrong ?
Please help.
Thanks
Ionrolleyes.gif

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2004-08-10 08:41
    Ion -

    Posting the entire segment of code would probably be real helpful.

    Regards,

    Bruce Bates
  • ionion Posts: 101
    edited 2004-08-10 09:47
    Hi Bruce,

    Here is the code:

    KEYIN········ ·· VAR···· Byte
    CURSOR········ VAR···· Byte

    RESULT········ VAR···· Nib

    v1················ VAR··· ·Word
    v2················ VAR···· Word

    CURSOR=0

    INPUT_DATA:
    SERIN 14\2, 84,[noparse][[/noparse]keyin]
    IF KEYIN < $3A THEN RESULT= keyin-$30

    PUT cursor,result
    DEBUG DEC cursor," ",DEC result,CR


    CURSOR=CURSOR+1
    IF (CURSOR) = 8 THEN VERIFY
    GOTO INPUT_DATA

    VERIFY:

    GET 0, Word v1
    GET 3, Word v2


    DEBUG DEC v1," ",DEC v2,CR



    And here is the Debug window display

    0 1
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
    7 8
    513 1541

    Thanks for looking into my problem

    Ion
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-08-10 12:39
    Words occupy two bytes each in the Scratpad (stored in binary format, low byte, then high byte). You're actually storing the stream of digits. You could either modify your input routine, or convert your stream with a routine like this:

    SP_to_Val:
    · mult = 1000
    · value = 0
    · FOR idx = 0 TO 3
    ··· READ (target + idx), temp
    ··· value = value + (temp * mult)
    ··· mult = mult / 10
    · NEXT
    · RETURN


    Note that this routine is a bit limited: it expects your values to be in four-digit chunks. I think it would be easier to modifiy the input routine. I don't know the keypad you're working with, but I think you could do something like this:

    Get_Number:
    · value = 0
    · DO
    ··· SERIN 14\2, 84, [noparse][[/noparse]keyIn]
    ··· IF (keyIn < $3A) THEN
    ····· value = value * 10 + (keyIn - $30)
    ··· ENDIF
    ··LOOP UNTIL (keyIn = XX)

    · RETURN

    This will take a key, convert it from ASCII to a digit if valid, then add it to the value. Note that the value is shifted left one digit (x 10) for each new valid digit. A robust routine would also allow a backspace key (simply divide the current value by 10 if backspace hit). The routine will accept digits until some terminater key (XX) is pressed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • ionion Posts: 101
    edited 2004-08-10 12:55
    Thank you very much Jon.

    It does works as expected. I will practice more with the rest of the code to see how it will interact and if i·have an other question I will postit.

    Thank again

    ion
  • ionion Posts: 101
    edited 2004-08-10 15:41
    Hello Jon,

    It works like a charm ( the second solution- is the one which i choose for testing)

    I can check my keyboard input against a stored password on an externall EEprom, and do what i want if password is wrong or good.

    Here is the test code:

    MASTLO········ VAR···· Word
    MASTHI········ VAR···· Word
    cursor········ VAR···· Byte
    value········· VAR···· Word
    keyin········· VAR···· Word

    I2CIN 0,$A1, 1616,[noparse][[/noparse]DEC4 MASTHI,DEC4 MASTLO ]·· ' stored value is 1234 and 5678

    Get_Number:
    cursor=1
    first:
    · value = 0
    · DO
    ··· SERIN 14\2, 84, [noparse][[/noparse]keyIn]
    ··· IF (keyIn < $3A) THEN
    ····· value = value * 10 + (keyIn - $30)
    ··· ENDIF
    ··· DEBUG DEC value,CR
    ··· cursor=cursor+1
    · IF cursor=5 THEN check1
    · IF cursor=9 THEN check2
    · LOOP
    GOTO get_number
    check1:· IF (value=MASTHI) THEN first
    ········ DEBUG "Try again. Wrong password",CR
    GOTO get_number
    check2:· IF (value=MASTlo) THEN
    ··········· GOTO fin
    ········ ENDIF
    ······ DEBUG "Try again. Wrong password",CR
    ······ GOTO get_number

    fin:
    DEBUG "Password OK",CR
    GOTO get_number

    Thanks again

    Ion
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-08-10 15:52
    I'm glad your code works now, but let me suggest that it's a tad disorderly and I think will become problematic as your program grows. Since you're always accepting a 4-digit number, you could create a subroutine that you can call cleanly, eliminating a lot of spaghetti GOTOs:

    Main:
    · FOR pw = 0 TO 1

    ··· LOOKUP pw, [noparse][[/noparse]MastHi, MastLo], testVal
    ····DO
    ····· GOSUB Get_Num
    ····· IF (value <> testVal) THEN
    ····· · DEBUG "Wrong password -- try again"
    ····· ELSE
    ······· EXIT·· ' terminate DO-LOOP
    ····· ENDIF
    ··· LOOP

    · NEXT

    Fin:
    · DEBUG "Password okay"
    · END


    Get_Num:
    · value = 0
    · idx = 0
    · DO
    ··· SERIN 14\2, 84, [noparse][[/noparse]keyIn]
    ··· IF (keyIn < $3A) THEN
    ····· value = value * 10 + (keyIn - $30)
    ····· idx = idx + 1
    ··· ENDIF
    ··LOOP UNTIL (idx = 4)
    · RETURN


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office


    Post Edited (Jon Williams) : 8/10/2004 3:58:53 PM GMT
Sign In or Register to comment.