Shop OBEX P1 Docs P2 Docs Learn Events
General question about code — Parallax Forums

General question about code

MichelBMichelB Posts: 154
edited 2009-09-30 16:36 in BASIC Stamp
I don't understand why values of constants are sometimes Hex sometimes Dec. Ex.: SW21-EX26-Servo_Control.BS2
Scale··· CON·· $00C6 (198)
Center· CON·· 1500
PwAdj·· CON·· $0080· (128)
Best regards.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-30 16:21
    It usually depends on which form is more meaningful for the intended usage. As far as the Stamp is concerned, it doesn't matter. If the intended use of the constant makes use of some binary property like the fact that it uses a particular bit or might be used with the */ operator where the position of the value in the intermediate 32 bit value is important, then using a hexadecimal or binary representation might provide more information than using a decimal representation.
  • ZootZoot Posts: 2,227
    edited 2009-09-30 16:21
    The scale and pwmadj are used with */ which presumes a hex number where the high byte is the left of decimal multiplier and the low byte is the right of decimal multiplier. I.E., when you use something like:'

    100 */ $0180

    That is equivalent to multiplying 100 by 1.5 ($0100 = 1; $0080 = .5 or 1/2 of 256 )

    You could use dec or hex, but using hex for values (constants) when using ** or */ makes it much easier to "read" by imaginging the decimal between the bytes of the word ( */ in "1/256ths") or to the left of the word ( ** in 1/65536ths ).

    You "create" a multiplier for */ by doing something like:

    x = x * 1.5
    x = x */ ( ( 1 * 256 ) + ( .5 * 256 )
    x = x */ ( $0100 + $0080 )
    x = x */ $0180

    In actuality what's happening is that when you use */ you are multiplying the first value by the multplier THEN dividing the result by 256, e.g.

    x = ( x * $0180 ) / 256

    It's a "trick" for doing fractional math in an integer-only environment. Essentially, you convert everything to "256ths", do your operations then convert back.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 9/30/2009 4:27:16 PM GMT
  • MichelBMichelB Posts: 154
    edited 2009-09-30 16:36
    Thank you very much Mike and Zoot.
Sign In or Register to comment.