Shop OBEX P1 Docs P2 Docs Learn Events
Convert hex to dec - Page 2 — Parallax Forums

Convert hex to dec

2»

Comments

  • RobbanRobban Posts: 124
    edited 2007-03-08 21:30
    Here is the new one....but the same value....
    i hope this is what i forgot to change/add.
    The reason i used 11 string is when i used 5 i didn´t get the hex to display at all.
    But now i figured it out (have not tried it in the car though,but from computer to computer it works

    ' {$STAMP BS2}
    k VAR Byte
    hexstr VAR Byte(5)
    hexa VAR Word
    i VAR Byte
    value VAR Word

    main:
    SEROUT 0,16468,[noparse][[/noparse]"010c",13]
    SERIN 1,16468,[noparse][[/noparse]SKIP 6,STR hexstr\5]
    FOR i=0 TO 5
    ·SEROUT 2,84,[noparse][[/noparse]hexstr(i)]
    NEXT

    FOR k = 0 TO 1
    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip1x
    ·· hexstr(i) = hexstr(i) - "0"

    skip1x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip2x
    ·· hexstr(i) = hexstr(i) - "A" + 10

    skip2x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip3x
    ·· hexstr(i)=hexstr(i)-"A" +10

    SKIP3x:
    NEXT

    FOR k = 3 TO 4
    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip4x
    ·· hexstr(i) = hexstr(i) - "0"

    skip4x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip5x
    ·· hexstr(i) = hexstr(i) - "A" + 10

    skip5x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip6x
    ·· hexstr(i)=hexstr(i)-"A" +10

    SKIP6x:
    NEXT

    value =(hexstr(0) << 12) + (hexstr(1) << 8) + (hexstr(3) << 4) + hexstr(4)

    DEBUG "hex1:",hexstr(0),CR
    DEBUG "hex2:",hexstr(1),CR
    DEBUG "hex3:",hexstr(3),CR
    DEBUG "hex4:",hexstr(4),CR
    DEBUG DEC value
    SEROUT 2,84,[noparse][[/noparse]"?n",DEC value]
    PAUSE 1000
    'GOTO main

    Post Edited (Robban) : 3/8/2007 9:54:56 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-08 21:54
    1) You forgot to add the "upper case" IF statement in the first loop (6 TO 7).

    2) You forgot to fix the assignment statements after the IF statements for "lower case". The "A" needs to be changed to "a".
  • RobbanRobban Posts: 124
    edited 2007-03-08 21:59
    sorry for this,but i replaced to old one posted as··new version just this very minute....where i hope i have fixed the if states...

    or is there something else?

    Post Edited (Robban) : 3/8/2007 10:04:43 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-08 22:39
    You still need to do item #2 ... Just before skip5x and just before skip2x, change the "A" to "a".
  • RobbanRobban Posts: 124
    edited 2007-03-09 15:07
    ok...changed it so this is the new version...but i still get 21912

    ' {$STAMP BS2}
    k VAR Byte
    hexstr VAR Byte(5)
    hexa VAR Word
    i VAR Byte
    value VAR Word

    main:
    SEROUT 0,16468,[noparse][[/noparse]"010c",13]
    SERIN 1,16468,[noparse][[/noparse]SKIP 6,STR hexstr\5]
    FOR i=0 TO 5
    SEROUT 2,84,[noparse][[/noparse]hexstr(i)]
    NEXT

    FOR k = 0 TO 1
    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip1x
    hexstr(i) = hexstr(i) - "0"

    skip1x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip2x
    hexstr(i) = hexstr(i) - "a" + 10

    skip2x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip3x
    hexstr(i)=hexstr(i)-"A" +10

    SKIP3x:
    NEXT

    FOR k = 3 TO 4
    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip4x
    hexstr(i) = hexstr(i) - "0"

    skip4x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip5x
    hexstr(i) = hexstr(i) - "a" + 10

    skip5x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip6x
    hexstr(i)=hexstr(i)-"A" +10

    SKIP6x:
    NEXT

    value =(hexstr(0) << 12) + (hexstr(1) << 8) + (hexstr(3) << 4) + hexstr(4)

    DEBUG "hex1:",hexstr(0),CR
    DEBUG "hex2:",hexstr(1),CR
    DEBUG "hex3:",hexstr(3),CR
    DEBUG "hex4:",hexstr(4),CR
    DEBUG DEC value
    SEROUT 2,84,[noparse][[/noparse]"?n",DEC value]
    PAUSE 1000
    'GOTO main
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-09 15:46
    I just noticed that you changed the variable in the FOR statements to "k" even though I had originally written it as "i". You didn't change the subscripts in the statements within the FOR/NEXT loop to match.

    Also, just after "main:", you have a "FOR i = 0 to 5". Shouldn't this be "FOR i = 0 to 4"?
  • RobbanRobban Posts: 124
    edited 2007-03-09 15:59
    OKI....:-D
    Now i got my 6904...changed the k to i and the 5 to 4
    gonna get down to the car and try it....*very exiting*
  • RobbanRobban Posts: 124
    edited 2007-03-09 17:54
    okej,it seem to "work" and react on the difrent rpm however....
    when i had 904 rpm the stamp showed 5464...and i had the "value/4" as i should according to the elm327 pdf as i posted to you earlier...
    It´s very strange that i works fine here with my computers and when i get down to the car it shows diffrent...
    the only thing i changed was the serout and serin pin....i swapped them in the program.
    can this be the diffrence?

    Post Edited (Robban) : 3/9/2007 6:41:45 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-09 19:01
    You'll have to make sure that the SERIN pin is the one being used for serial input to the Stamp and the SEROUT pin is the one being used for serial output. The Stamp doesn't care which pins are used. All of them (pins 0-15) are identical for most of the PBasic commands. The PBasic manual mentions restrictions where they exist (the LCD and I2C statements are some of those that use particular pins). Anytime you change something in a program that seems to work and it stops working, I'd suspect the changes until I've proven otherwise (and found the real problem).

    Since you have an LCD attached to your Stamp, you could use that for debugging. Maybe the Stamp isn't getting the data you expect.

    Like I said before, use DEBUG or the LCD to follow what's going on in your program. Simulate the program on paper with your expected data, then make sure (with some kind of debug output) that you're actually receiving what you expect.
  • RobbanRobban Posts: 124
    edited 2007-03-09 19:41
    Came to think of something that might be usefull....
    if you look at the attached elm pdf file at page 41 they say something about internal UART
    could it be this that´s missing?
    Or does the stamp have this?
    strange if if would though since i do get my hex values as i think i suppose to get.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-09 19:53
    The Stamp does not have an internal UART (Universal Asynchronous Receiver Transmitter). The SERIN and SEROUT statements provide most of the same functionality. The difference is that an internal UART is usually an independently functioning logic block that has some buffering. The SERIN/SEROUT statements do not have any buffering so, when the Stamp is not actually executing the SERIN statement, it is ignoring the receive pin. Similarly, when the Stamp is transmitting with a SEROUT statement, it's doing nothing else.
  • RobbanRobban Posts: 124
    edited 2007-03-10 11:17
    Mike!

    I got it working just as i should....
    1500 rpm on the car shows the same on the LCD.

    Thank you for all the help with the conversion etc

    here is the working program

    ' {$STAMP BS2}

    hexstr VAR Byte(15 )
    i VAR Byte
    value VAR Word

    main:
    SEROUT 2,84,[noparse][[/noparse]"?f"]
    SEROUT 3,16468,[noparse][[/noparse]"010c",13]
    SERIN 4,16468,[noparse][[/noparse]STR hexstr\15]
    FOR i=6 TO 13
    'SEROUT 2,84,[noparse][[/noparse]STR hexstr]
    'DEBUG hexstr(i)

    FOR i= 6 TO 7
    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip1x
    hexstr(i) = hexstr(i) - "0"
    skip1x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip2x
    hexstr(i) = hexstr(i) - "a" + 10
    skip2x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip3x
    hexstr(i)=hexstr(i)-"A" +10
    SKIP3x:
    NEXT


    FOR i = 9 TO 10

    IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip4x
    hexstr(i) = hexstr(i) - "0"
    skip4x:
    IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip5x
    hexstr(i) = hexstr(i) - "a" + 10
    skip5x:
    IF hexstr(i)<"A" OR hexstr(i)>"F" THEN skip6x
    hexstr(i)=hexstr(i)-"A" +10
    SKIP6x:
    NEXT
    value =(hexstr(6) << 12) + (hexstr(7) << 8) + (hexstr(9) << 4) + hexstr(10)

    DEBUG "hex1:",hexstr(6),CR
    DEBUG "hex2:",hexstr(7),CR
    DEBUG "hex3:",hexstr(9),CR
    DEBUG "hex4:",hexstr(10),CR

    DEBUG DEC value,CR
    SEROUT 2,84,[noparse][[/noparse]"?n",DEC value/4]
    ' SERIN 0,16468,[noparse][[/noparse]WAIT(">")]
    PAUSE 1000
    GOTO main
    NEXT
    PAUSE 1000


    have saved this on several discs smile.gif
    Now i´m gonna expand it to show speed,coolant temperature etc...

    Once again....THANK YOU!
Sign In or Register to comment.