Shop OBEX P1 Docs P2 Docs Learn Events
Hex conversion in SX/B ?? — Parallax Forums

Hex conversion in SX/B ??

T&E EngineerT&E Engineer Posts: 1,396
edited 2007-04-09 02:05 in General Discussion
I am looking for a way to convert a variable into it's HEX value.

FOR EXAMPLE.....


Value···· VAR·· Byte(8)

' Value(1) = 23

Value(1) = $23····· ' is valid

but

' Value(1) = 23

Value(1) = $Value(1)··' is not·valid


Am I missing something or is a function required to do this?

If so, is anyone willing to share one?


Thanks.

Comments

  • BeanBean Posts: 8,129
    edited 2007-04-04 11:42
    The "$" prefix only works when defining constants.

    You want to divide the value by 10, then multiply by 16, then add in the original value modulus 10.

    What you are doing is called BCD conversion. Do a search there is probably a post about it in the forums.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Educate your children to self-control, to the habit of holding passion and prejudice and evil tendencies subject to an upright and reasoning will, and you have done much to abolish misery from their future and crimes from society"

    Benjamin Franklin
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-04-04 14:12
    Bean,
    I looked through every title under the SX section over the last few years for "HEX" and only came up with 1 or 2.

    I did see one reference to a WORD HEX COUNTER that you wrote. Here is a couple of subroutines:
    [size=2][code]
    ' ----------------------------------------------------------
    ' Subroutine DECLARATIONS
    ' ----------------------------------------------------------
    

    GET_HEX· SUB 2····· ' print using HEXx format
    LCD_OUT· SUB 1, 2·· ' send to LCD
    '
    '
    '
    ' Subroutines / Jump Table
    '
    LCD_OUT:
    · temp1 = __PARAM1······························ ' digit to send
    · SEROUT Lcdout, LCDBaud, temp1················· ' send the digitacter·
    · RETURN

    GET_HEX:·····
    · wTemp1 = __WPARAM12····························· ' copy output value
    · temp1 = wTemp1_MSB >> 4
    · READ Hex_Digit + temp1, temp1
    · LCD_OUT temp1
    · temp1 = wTemp1_MSB & $0F
    · READ Hex_Digit + temp1, temp1
    · LCD_OUT temp1
    · temp1 = wTemp1_LSB >> 4
    · READ Hex_Digit + temp1, temp1
    · LCD_OUT temp1
    · temp1 = wTemp1_LSB & $0F
    · READ Hex_Digit + temp1, temp1
    · LCD_OUT temp1
    RETURN

    '
    Hex_Digit:
    · DATA "0123456789ABCDEF"
    '
    [/code][/size]
    However, it is written for a WORD not a byte (which gets me back to LSB and MSB of Bytes (aka..nibbles for the SX which doesn't exist).

    However, in the SX DS1302 program written by JDOhio and my modification there is a routine which may also be useful:
    WriteDataToDisplay:
     temp3=__PARAM1
      FOR Idx=0 to 1
      ClockDisplay=temp3
      IF Idx=0 THEN
       SWAP ClockDisplay
      ENDIF
      ClockDisplay=ClockDisplay & $0f
      ClockDisplay=ClockDisplay | $30
      SendByte ClockDisplay
     NEXT
     RETURN
    
    

    I modified it a little but it will read in a byte (e.g. $5A) and give out $35 and $3A so they can be in ASCII format (which I believe is what the RobOympic "StringWriter" routine (I am using) looks for).

    I also found this DEC2BCD and BCD2DEC routine that JonnyMac wrote (but I have not been able to figure out how to test it yet):

    http://forums.parallax.com/showthread.php?p=640449

    DEC2BCD         FUNC    1, 1
    BCD2DEC         FUNC    1, 1
     
     
     
    DEC2BCD:
      tmpB1 = __PARAM1                              ' get parameter
      tmpB2 = tmpB1 / 10                            ' isolate 10s
      SWAP tmpB2                                    ' move to upper nibble
      tmpB2 = tmpB2 | __REMAINDER                   ' add lower nibble
      RETURN tmpB2
    
    BCD2DEC:
      tmpB1 = __PARAM1                              ' get parameter
      tmpB2 = tmpB1 >> 4                            ' isolate high nibble
      tmpB2 = tmpB2 * 10                            ' convert to decimal
      tmpB1 = tmpB1 & $0F                           ' isolate ones
      tmpB2 = tmpB2 + tmpB1                         ' add them
      RETURN tmpB2
    
    
    


    What do you think about these - Is this right or do I go after the BCD approach?

    Thanks.





    Post Edited (T&E Engineer) : 4/4/2007 2:45:40 PM GMT
  • JonnyMacJonnyMac Posts: 8,941
    edited 2007-04-04 14:55
    Do you have an SX-Key? If so, then you can use the Debug mode of the IDE:

    Start:
      FOR inVal = 0 TO 99
        outVal = DEC2BCD inVal
        \ WATCH inval, 8, udec
        \ WATCH outval, 8, uhex
        BREAK
      NEXT
      GOTO Start
    
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-04-04 15:05
    Thank you JonnyMac. This is what I needed. I am just now starting to use the WATCH function over the last week or so. I will look at it tonight after work and let you know if I have any problems.

    Thanks again.

    PS: I saw your website last night and your movie clips. Pretty impressive!
  • BeanBean Posts: 8,129
    edited 2007-04-04 17:09
    The DEC2BCD is what you want.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Educate your children to self-control, to the habit of holding passion and prejudice and evil tendencies subject to an upright and reasoning will, and you have done much to abolish misery from their future and crimes from society"

    Benjamin Franklin
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • T&E EngineerT&E Engineer Posts: 1,396
    edited 2007-04-04 23:04
    JonnyMac's BCD2DEC routine is not quite what I need here. His routine does the following:
    0 in gives $00 out
    30 in gives $30 out
    99 in gives $99 out

    What I need is a straight hex conversion not BCD it appears.
    0 in gives $00 out
    30 in gives $1E out
    99 in gives $63 out.

    I think what Bean said it what I need to do.

    What does "add the original value modulus 10" mean?
    Is it this?··
    Value···· VAR···· BYTE
    Value2·· VAR···· BYTE

    Value2 = Value
    Value = Value / 10
    Value = Value * 16
    Value = Value + Value2 // 10
    Bean (Hitt Consulting) said...
    The "$" prefix only works when defining constants.

    You want to divide the value by 10, then multiply by 16, then add in the original value modulus 10.

    What you are doing is called BCD conversion. Do a search there is probably a post about it in the forums.

    Bean.

    I also tried this routine but it is not right either. What is wrong? It is only looking at 1 digit I think.

    Start:
    · FOR inVal = 0 TO 99
    ··· outVal = DEC2HEX inVal
    ··· \ WATCH inval, 8, udec
    ··· \ WATCH outval, 8, uhex
    ··· BREAK
    · NEXT
    · GOTO Start

    DEC2HEX:
    · tmpB1 = __PARAM1····························· ' get parameter
    · tmpB2 = tmpB1 / 10··························· ' isolate 10s
    · tmpB2 = tmpB2 * 16
    · tmpB2 = tmpB2 + tmpB1
    · tmpB2 = tmpB2 // 10
    · READ Hex_Digit + tmpB1, tmpB2
    · RETURN tmpB2

    Hex_Digit:
    · DATA "0123456789ABCDEF"

    Post Edited (T&E Engineer) : 4/4/2007 11:27:26 PM GMT
  • JonnyMacJonnyMac Posts: 8,941
    edited 2007-04-04 23:51
    Number formats are for humans, internally it is all binary; whether one uses %1111, 15, or $0F, the [noparse][[/noparse]internal] value is the same. If you want to convert to ASCII characters for an external display then use LOOKUP or READ to convert each nib in a byte to its hex representation.
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2007-04-05 15:44
    T&E Engineer,

    This might work for you. (I have not actually tested it!)

    DEC2HEX:
      tmpB1 = __PARAM1                              ' get parameter
      tmpB2 = tmpB1 AND $F0                         ' isolate 1st hex value
      SWAP tmpB2                                    ' swap the nibbles
      READ Hex_Digit + tmpB2, tmpB2                 ' read the ASCII hex character
    
    '<inset display tmpB2 command here>             ' display the hex character
    
      tmpB2 = tmpB1 AND $0F                         ' isolate 2nd hex value
      READ Hex_Digit + tmpB2, tmpB2                 ' read the ASCII  hex character
    
    '<inset display tmpB2 command here>             ' display the hex character
    
    RETURN 
    
    
    Hex_Digit:
      DATA "0123456789ABCDEF"
    



    - Sparks
  • JonnyMacJonnyMac Posts: 8,941
    edited 2007-04-05 16:41
    That looks good, Sparks, though I'd suggest a subroutine name more like HEX_STR so that it doesn't get confused with numeric conversion routines.
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-04-05 17:21
    Thank you. I was working on some code similar to this when this was posted.

    I see that the byte separations (nibbles) are to be "displayed". What I would like is to combine them back together so that tmpB2 actually comes back with "1E" if I give it a 30 in. Howeer, I dont know if that means I need to AND or OR the nibble halves together.

    I will try this out tonight and let everyone know.

    Post Edited (T&E Engineer) : 4/5/2007 5:27:22 PM GMT
  • JonnyMacJonnyMac Posts: 8,941
    edited 2007-04-05 18:01
    You seem to be chasing a ghost that doesn't exist. Decimal 30 is hex $1E which is binary %00011110 -- the value is all the same. When you say you want it to come back as "1E", what EXACTLY do you mean? Do you want to display "1E" on something like an LCD (Sparks showed you how, above), or use the value 1E in some equation or function? If the latter, you don't have to do anything -- again 30 is $1E (the $ is just for us humans to indicate hex representation of the value).
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-04-05 18:29
    I see what you are saying JonnyMac. Let me look it over again tonight. Perhaps the issue is that the RX'd characters (from my SX28 DS1302 circuit) are coming into my SX52 LED matrix with the RobOlympic·"StringWriter" subroutine. Perhaps it·is·not being·interpreted correctly·so that I am assuming that they are in HEX (as to be sent to an LCD - ascii hex)·and the subroutine checks normal HEX values to see which DATA sections to READ the message into. Since I can't bring RX'd bytes into a DATA statement (like how the StringWriter subroutine wants) I am trying to convert·a variable in the StringWriter subroutine called·"DataChr" to allow it to read in the bytes from an RX'd array instead of the message DATA statement.
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-04-09 02:05
    Thanks but this thread can be closed now. I got the DS1302 TimeDate () information to read correctly thanks to help by Sparks-R-fun and others. It is in the completed projects with video and pics and code.
Sign In or Register to comment.