Shop OBEX P1 Docs P2 Docs Learn Events
from decimal to hex — Parallax Forums

from decimal to hex

moomoomoomoo Posts: 27
edited 2009-01-02 03:37 in BASIC Stamp
I have a picopic· 16 bit servo controller. I can only type in hex values in the serout command and not ascii represented hex.

SEROUT 1, 240,[noparse][[/noparse] 120, 1, $05, $DC, 100]

1 =··· OUTPUT PIN ON BS2SX
240 = BAUD RATE
120 = SERVO CONTROLLER ADDRESS
1 =··· CHANNEL ON THE SERVO CONTROLLER
$05 = LOWBYTE OF 1500 OR 1.5ms
$DC = HIGHBYTE OF 1500
100 = SPEED OF THE SERVO

I'M USING THE MEMSIC 2125 AND THE INPUT OF THE·MEMSIC·IS THE OUTPUT OF THE SEROUT COMMAND.
The problem is that the servo controller only excepts the hex commands this way only. It will not except HEX X.LOWBYTE, HEX X.HIGHBYTE and it will not except a decimal number or anything ascii.

I have the memsic setup to represent 1500 as "level and center" of my humanoid and that it's values deviate from 1500.

Now the question, how do I have 1500 ( or any number ) turned into hex value·and place this into the serout command for this to work right. I feel like I've tried everything.

I need help, please.

·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-01 04:25
    Hex is just a convenient notation, and in the SEROUT line you show above it is completely optional (trust me on this). Lets say your have a [noparse][[/noparse]Word] variable called postion that holds 1500. Your SEROUT line would be:

    SEROUT 1, 240,[noparse][[/noparse] 120, 1, position.LOWBYTE, position.HIGHBYTE, 100]

    Again, don't get wrapped around the axle on the Hex thing; its a format for humans, the devices don't care. What you need to understand about the HEX modifier, however, is that it converts a value to its text representation, hence your version did not work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2005-01-01 14:15
    A heck of a lot of calculators support coversion to Hex now.

    That includes the Scientific Calculator in any version of Microsoft Windows.

    I am also sure you can download a shareware calculator to do the same thing.

    So...

    It really is not that hard to convert.

    Alternatively, you can set up a table in Excel and then just peek at it rather than keyin values as required.· Personally, I like the table as it seems far more efficent and easier to use.



    AND also,

    Code usually limits the range of Hex numbers.· You are not going to use this for scientiful or engineering calulations - mostly it counts up to 255 or up to 4095 as a more convient format than Binary [noparse][[/noparse]which is really hard to deal with in anything over 16 or so].

    HEX works very well with the modularity of pages and addresses·in memory when you begin to work with such issues.·

    Decimal does not so obviously relate to pages [noparse][[/noparse]for instance 1K of memory is not really 1000 Bytels, but 1024 Bytes in Decimal·or 400 Bytes in HEX].


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    G. Herzog in Taiwan

    Post Edited (Kramer) : 1/1/2005 2:23:25 PM GMT
  • NewzedNewzed Posts: 2,503
    edited 2005-01-01 14:51
    If, after all the above, you still feel you need to convert DEC to HEX, I have a simple little program that will do that.· You enter the number, the debug screen returns the 4 digit HEX number, then assigns the low byte and hight byte to new variables so they can be manipulated.· You are welcome to it.· The program, dec to hex_A, ·is attached.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester?

    http://hometown.aol.com/newzed/index.html
  • moomoomoomoo Posts: 27
    edited 2005-01-01 16:33
    Jon,

    For the sake of arguement, I tried the position.lowbyte thing again and that doesn't work. They have made the firmware to only except hex values ONLY and nothing else! That is what is so frustrating. There is nothing in pbasic to convert a decimal to hex?

    My input data is a stream of decimal numbers.
    The program·needs to convert·decimal number·to hex equivalent.
    The hex value is then placed into the serout command to the servo controller.

    Is there another way?

    Anthony
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-01-01 17:34
    After reading the manual the correct syntax is:

    SEROUT 1, 240, [noparse][[/noparse]120, 1, position.HIGHBYTE, position.LOWBYTE, 100]
    
    


    Note that the 100 at the end of the stream will affect how fast the servo moves.· You may want to change this to 0.

    Let me say this again -- and please believe me because I've been doing this stuff a very long time -- Hex values are for programmers' convenience and the machines don't care; internally everything is binary (1s and 0s).

    And before you vent toward me again, let me point out that the PicoPic manual contains nothing but BASIC Stamp examples.· Do you think that would be the case if there was a problem using the BASIC Stamp to control this device?....· I don't.

    Try this little test program:

    pos    VAR    Word
     
    Main:
      FOR pos = 1000 TO 2000 
        SEROUT 1, 240, [noparse][[/noparse]120, 1, pos.HIGHBYTE, pos.LOWBYTE, 0]
        PAUSE 50
      NEXT
     
      FOR pos = 2000 TO 1000
        SEROUT 1, 240, [noparse][[/noparse]120, 1, pos.HIGHBYTE, pos.LOWBYTE, 0]
        PAUSE 50
      NEXT
     
      GOTO Main
    


    If your connections are correct (do you have a common ground between your BASIC Stamp and the PicoPic?) this should cause a servo connected to port #1 to sweep back and forth.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • DaveGDaveG Posts: 84
    edited 2005-01-01 17:36
    Anthony,

    In your original post you stated that $05 = LOWBYTE of 1500 and $DC = HIGHBYTE. I believe the order is reversed.

    For DEC 1500:
    HEX HIGHBYTE = $05
    HEX LOWBYTE = $DC

    I'm not sure it matters now in the discussion, but you might check it.

    Dave G
  • moomoomoomoo Posts: 27
    edited 2005-01-01 22:08
    Ron,

    Can you forgive me for my venting?

    I just read your reply and I read Dave G's reply about it being reversed and then I looked back at your example.........and I feel like an IDIOT!!!!

    I've had the SEROUT 1, 240,[noparse][[/noparse] 120, 1, position.LOWBYTE, position.HIGHBYTE, 100] reversed as Dave pointed out and you've had it written right the whole time and I wasn't paying attention to that at all.

    I'M AN IDIOT!!!!!!!

    Thank you so much, Ron, for your patience and the fact that you went through the trouble to look up the manual for the picopic and read it.

    Again, please forgive me!

    And thank you too, Dave!

    Rage is another form of blindness.
  • KenMKenM Posts: 657
    edited 2005-01-02 02:33
    Kramer said...

    A heck of a lot of calculators support coversion to Hex now.

    Also, every windows calculator I have seen can also convert between decimal and HEX...you know, Programs----Accessories
    Calculator, then View
    Scientific
    See attached image
    573 x 388 - 45K
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2005-01-02 13:05
    In some ways I feel a bit foolish·for jumping into this topic.· Obviously, the problem is about automatically converting data from·Decimal to Hex within a program, not about the user's programing ability.

    Please forgive me for not really looking at the context of the question.

    And, Happy New Year to all.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    G. Herzog in Taiwan

    Post Edited (Kramer) : 1/2/2005 1:06:33 PM GMT
  • lvsparklvspark Posts: 1
    edited 2008-12-31 05:25
    Hello, anybody know where I can find a picopic manual? picobytes.com no longer exists!!
    Thanks!
    Mike
  • $WMc%$WMc% Posts: 1,884
    edited 2009-01-01 23:33
    moomoo
    Not to follow up on the "IDIOT" thing, We all get balled up at times: But it was Mr. Jon Williams with a " J " not " RON " That answered Your post.

    Please edit Your post.As this is disrespectful to Mr. Williams. Its A southern Thing I hope You understand.


    ___Most Respectively______________$WMc%________Happy New Year

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Truth is out there
  • kelvin jameskelvin james Posts: 531
    edited 2009-01-02 03:37
    Strange....Anyone else catch the posting dates here? Once it changed to 2005, now it is today.
Sign In or Register to comment.