Reading Word data
T&E Engineer
Posts: 1,396
Can someone look at my test code below and tell me what I am doing wrong. I want to be able to read in larger numbers than 255 so it would have to be WORDs. I just get a bunch·of larger numbers than expected.
ReadAddress···· VAR·· Word
Wchar············· VAR·· Word
Numbers015··· DATA· Word 2,Word 6,Word 12,Word 18,Word 24,Word 30,Word 25,Word 40,Word 45,Word 50,Word 55,Word 60,Word 67,Word 72,Word 77,Word 81,Word 0
Numbers1631·· DATA· Word 87,Word 92,Word 99,Word 105,Word 112,Word 118,Word 124,Word 132,Word 139,Word 145,Word 152,Word 159,Word 167,Word 175,Word 182,Word 190,Word 0
Numbers3247·· DATA· Word 198,Word 205,Word 212,Word 220,Word 228,Word 235,Word 243,Word 252,Word 260,Word 268,Word 276,Word 283,Word 292,Word 302,Word 310,Word 320,Word 0
Numbers4859·· DATA· Word 328,Word 336,Word 346,Word 354,Word 362,Word 370,Word 378,Word 386,Word 394,Word 402,Word 410,Word 418,Word 0
Times········ DATA· Word 427,Word 434,Word 440,Word 445,Word 0
AMPM········ DATA· Word 454,Word 460,Word 0
Days········· DATA· Word 466,Word 472,Word 481,Word 488,Word 497,Word 504,Word 510,Word 0
Phrases······ DATA· Word 518,Word 524,Word 532,Word 541,Word 549,Word 0
Main:
DEBUG CLS
ReadAddress = Numbers4859
GOSUB NumberRead
END
NumberRead:
· DO
··· READ ReadAddress, Wchar
··· ReadAddress = ReadAddress + 1
··· IF (Wchar = 0) THEN EXIT
··· DEBUG DEC Wchar
· LOOP
RETURN·
ReadAddress···· VAR·· Word
Wchar············· VAR·· Word
Numbers015··· DATA· Word 2,Word 6,Word 12,Word 18,Word 24,Word 30,Word 25,Word 40,Word 45,Word 50,Word 55,Word 60,Word 67,Word 72,Word 77,Word 81,Word 0
Numbers1631·· DATA· Word 87,Word 92,Word 99,Word 105,Word 112,Word 118,Word 124,Word 132,Word 139,Word 145,Word 152,Word 159,Word 167,Word 175,Word 182,Word 190,Word 0
Numbers3247·· DATA· Word 198,Word 205,Word 212,Word 220,Word 228,Word 235,Word 243,Word 252,Word 260,Word 268,Word 276,Word 283,Word 292,Word 302,Word 310,Word 320,Word 0
Numbers4859·· DATA· Word 328,Word 336,Word 346,Word 354,Word 362,Word 370,Word 378,Word 386,Word 394,Word 402,Word 410,Word 418,Word 0
Times········ DATA· Word 427,Word 434,Word 440,Word 445,Word 0
AMPM········ DATA· Word 454,Word 460,Word 0
Days········· DATA· Word 466,Word 472,Word 481,Word 488,Word 497,Word 504,Word 510,Word 0
Phrases······ DATA· Word 518,Word 524,Word 532,Word 541,Word 549,Word 0
Main:
DEBUG CLS
ReadAddress = Numbers4859
GOSUB NumberRead
END
NumberRead:
· DO
··· READ ReadAddress, Wchar
··· ReadAddress = ReadAddress + 1
··· IF (Wchar = 0) THEN EXIT
··· DEBUG DEC Wchar
· LOOP
RETURN·
Comments
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95
http://www.parallax.com/detail.asp?product_id=30012
Product web site: www.sxvm.com
Available now... SX-Video OSD module $59.95 www.sxvm.com
Those that would give up freedom for security will have neither.
·
Thanks for the help. ReadAddress = ReadAddress + 2 made it a little better for DATA up to 255. After that it is unknown to me.
Using this DATA statement:
Numbers4859·· DATA· Word 328,Word 336,Word 346,Word 354,Word 362,Word 370,Word 378,Word 386,Word 394,Word 402,Word 410,Word 418,Word 0
I get the following output with ReadAddress = ReadAddress + 2:
and using: DEBUG DEC Wchar, " "
statement.
72 80 90 98 106 114 122 130 138 146 154 162
Are these High and Low bytes of the WORD ?
I just want to be able to read out these WORDs and perform manipulations in an effort to save some EEPROM space on the BS2.
Please Help.
Thanks again,
Timothy Gilmore
READ ReadAddress, Word Wchar
This is only allowed with PBASIC 2.5 directive.
Paul
I think I have it working but my method requires doing some math on the High and Low bytes of the WORD. It does work though. I will try your method tomorow as it looks simpler.
' {$STAMP BS2}
' {$PBASIC 2.5}
ReadAddress···· VAR·· Byte
Wchar·········· VAR·· Word
Result········· VAR·· Word
Numbers015··· DATA· Word 2,Word 6,Word 12,Word 18,Word 24,Word 30,Word 25,Word 40,Word 45,Word 50,Word 55,Word 60,Word 67,Word 72,Word 77,Word 81,Word 0
'Numbers1631·· DATA· Word 87,Word 92,Word 99,Word 105,Word 112,Word 118,Word 124,Word 132,Word 139,Word 145,Word 152,Word 159,Word 167,Word 175,Word 182,Word 190,Word 0
Numbers3247·· DATA· Word 198,Word 205,Word 212,Word 220,Word 228,Word 235,Word 243,Word 252,Word 260,Word 268,Word 276,Word 283,Word 292,Word 302,Word 310,Word 320,Word 0
Numbers4859·· DATA· Word 328,Word 336,Word 346,Word 354,Word 362,Word 370,Word 378,Word 386,Word 394,Word 402,Word 410,Word 418,Word 0
'Times········ DATA· Word 427,Word 434,Word 440,Word 445,Word 0
'AMPM········· DATA· Word 454,Word 460,Word 0
'Days········· DATA· Word 466,Word 472,Word 481,Word 488,Word 497,Word 504,Word 510,Word 0
'Phrases······ DATA· Word 518,Word 524,Word 532,Word 541,Word 549,Word 0
Main:
DEBUG CLS
ReadAddress = Numbers4859
GOSUB NumberRead
END
NumberRead:
· DO
··· READ ReadAddress, Wchar.LOWBYTE
··· READ ReadAddress+1, Wchar.HIGHBYTE
··· ReadAddress = ReadAddress + 2
··· IF (Wchar = 0) THEN EXIT
··· IF Wchar.HIGHBYTE = 0· THEN
··· Result = 256+Wchar.LOWBYTE
··· ELSE
··· Result = 256*Wchar.HIGHBYTE + Wchar.LOWBYTE
··· ENDIF
··· DEBUG DEC Result, " "
· LOOP
RETURN
I think you can simplify your program a bit.
Result = 256*Wchar.HIGHBYTE + Wchar.LOWBYTE
is the same as:
Result = Wchar '(no math required, both are word values)
and... this
IF Wchar.HIGHBYTE = 0 THEN
Result = 256+Wchar.LOWBYTE
.. $0102 = $100+$02· [noparse][[/noparse]edit] sorry, I think in hex sometimes.
is puzzling as the "Result =" statement just adds 1 to the highbyte. So why don't you just put the correct values in the DATA statements to begin with? "word 2" would equal "word 258" or in hex, $102. Granted I don't really know what output you are trying to get exactly but it would seem to simplify things. Have you looked into using LOOKUP/LOOKDOWN commands. These are great for lookup tables and reducing multi-line "if" statements into a couple lines.
LOOKUP idx,[noparse][[/noparse] 2, 4, 6,12,18,24,30,25,40,45,50,55,60,67,72,77,81], Result
Paul
Post Edited (Paul) : 11/4/2005 3:41:38 PM GMT
Your first post was correct:
NumberRead:
· DO
···· READ ReadAddress, Word Wchar
···· ReadAddress = ReadAddress + 2
···· IF (Wchar = 0) THEN EXIT
···· DEBUG DEC Wchar, " "
· LOOP
RETURN
is the same as my method (below):
NumberRead:
· DO
··· READ ReadAddress, Wchar.LOWBYTE
··· READ ReadAddress+1, Wchar.HIGHBYTE
··· ReadAddress = ReadAddress + 2
··· IF (Wchar = 0) THEN EXIT
··· IF Wchar.HIGHBYTE = 0· THEN
··· Result = 256+Wchar.LOWBYTE
··· ELSE
··· Result = 256*Wchar.HIGHBYTE + Wchar.LOWBYTE
··· ENDIF
··· DEBUG DEC Result, " "
· LOOP
RETURN
However, your method using (top method):
:
READ ReadAddress, Word Wchar
:
is much neater and less memory too.
Thanks again!