Shop OBEX P1 Docs P2 Docs Learn Events
Stupid Math question — Parallax Forums

Stupid Math question

PeakXLPeakXL Posts: 3
edited 2005-12-21 00:13 in BASIC Stamp
I haven't played with this stuff in years, and I just got my B2 module and an accelerometer.·· Now I question some (very basic) math and why it is there.

In the code for the accelerometer there are a number of debug statements, one of which is:

DEBUG ... xGForce.Bit15 * 13 ...

Can someone tell me what that is all about?

See Program MEMSIC2125.BS2 for a complete listing of the code . . . but I think I have most of it down.

Actually, while I am at it,·can anyone direct me to some formulae for calculating the directional·movement in the x-plane·and the y-plane?· Can this be done without a real-time clock?

Thanks.

Comments

  • Philip GamblinPhilip Gamblin Posts: 202
    edited 2005-12-20 17:32
    I haven't looked at the program listing, but the line you listed does the following:

    xGforce.bit15··· ' xGforce is a variable and .bit15 is a Pbasic modifier specifying the bit 15 or the MSB of that variable

    * 13···············'means multiply by 13.

    You should probably download the Basic Stamp Pbasic editor herehttp://www.parallax.com/html_pages/downloads/software/software_basic_stamp.asp

    Pick your operating system and download the appropriate Version then install and open it. The·Pbasic help file is excellent with· program examples yhou can paste and run.

    If you do a search on "variable·" in the help file it will explain the Pbasic variable structure
  • PeakXLPeakXL Posts: 3
    edited 2005-12-20 17:47
    Sorry, I made myself out to be too much of an idiot.·· I understand the logic (15 years removed assembler programmer), I don't understand why in the world they would mutiply the MSB by that factor . . . if I remember these things, that is just a sign bit?!

    Thanks, and any other advice/insight would be helpful.

    ·
  • BeanBean Posts: 8,129
    edited 2005-12-20 18:23
    PeakXL,
    · Jon got really tricky there. He is doing (xGForce.BIT15 * 13 + " ") this will print a space " "(32) if the number is positive or a "-"(45) if the number is negative. And of course the difference in ASCII is 13.

    · You could change this to (xGForce.BIT15 * 2 + "+") and this would print either a "+" or a "-".
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    Forget about the past, plan for the future, and live for today.
    ·
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-12-20 18:51
    Hi PeakXL

    The Stamp DEBUG command can deal with twos complement integers using the SDEC modifier. For example,
    x = -12345
    DEBUG SDEC x
    will print the signed integer (-32768<=x<=32767) correctly with sign.

    The code you referenced did not use that form so there is probably something else going on, for example, the number -12345 really represents the decimal value, -123.45 and you want the DEBUG to display it in that way. The SDEC command only works with integers, so to display the decimal you have to break it up. Something like this:
    DEBUG x.bit15*13+32, DEC ABS x/100, ".", DEC2 ABS x
    That would cover the bases for x both positive negative or zero. First it prints either a space or a "-" depending on the sign bit. 32 ascii is space, and "-" is 45 ascii, so 13 is added to space conditional on the sign bit. Then the program prints the digits to the left of the decimal point, then the decimal point, then the two digits to the right of the decimal point. The division by 100 is integer division, that drops the remainder. The DEC2 modifier prints two decimal digits, which allows for the case where the leading digit after the decimal point happens to be a zero. You dont want to print 123.3 when the answer should really be 123.03.

    I haven't actually looked at the MEMSIC2125.BS2 program code to see what it actually going on in the rest of the DEBUG you quoted.

    Another way to print out a "-" conditional on the sign bit is the following:
    DEBUG REP "-"\x.bit15, ...
    Instead of space, that method prints nothing if the number happens to be positive. Look up the REP modifier in the book. How you do it depends on the format you want to display.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-12-20 18:55
    Truth be told, I *liberated* the BIT15 trick from my pal, Dr. Allen!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • PeakXLPeakXL Posts: 3
    edited 2005-12-21 00:13
    Thank you. I started looking up ASCII codes, but decided to ask some experienced hands first.

    Thanks, I get it now.

    Don.
Sign In or Register to comment.