Shop OBEX P1 Docs P2 Docs Learn Events
Memsic 2125 Dual Axis Accelerometer - how does +/- sign in debug output come fr — Parallax Forums

Memsic 2125 Dual Axis Accelerometer - how does +/- sign in debug output come fr

John KauffmanJohn Kauffman Posts: 653
edited 2008-09-21 13:33 in BASIC Stamp
The sample code (attached) from Parallax for the Memsic 2125 dual axis accelerometer picks up an xRaw value from the sensor and then uses the following code to display the value in units of g.

··· DEBUG "X Input...· ",
········· ...
········· "G Force... ", (xmG.BIT15 * 13 + " "),
········· DEC (ABS xmG / 1000), ".", DEC3 (ABS xmG), " g",
········· ...

The G force is displayed in three parts:
(when needed) a minus sign from·· ·(xmG.BIT15 * 13 + " ")
units from·································· DEC (ABS xmG / 1000),
and thousandths from·················· ".", DEC3 (ABS xmG), " g",

I see how the digits are produced, but I don't understand how the code (xmG.BIT15 * 13 + " ") produces·a minus sign for a negative value and no character·for a positive value.

Thanks.

Comments

  • Mike CookMike Cook Posts: 829
    edited 2008-09-21 13:15
    From what I can tell the line:

    (xmG.BIT15 * 13 + " ")

    Operates as follows:

    1. if xmG.BIT15 = 1, bit set indicating a negative value, the resulting ASCII code will be decimal 45 which is the minus sign character.

    2. if xmG.BIT15 = 0, bit NOT set indicating a positive value, the resulting ASCII code will be decimal 32 which is a space character.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
  • John KauffmanJohn Kauffman Posts: 653
    edited 2008-09-21 13:25
    I think I understand, thanks.

    The trick is that the ascii value of the character to display is derived by adding a *literal* number (xmG.BIT15 * 13) plus an *implied* number (the ASCII code for a space).

    THanks.
  • Mike CookMike Cook Posts: 829
    edited 2008-09-21 13:33

    That's my 'take' on it.

    Keeps the value displayed, by the debug statement, from bouncing forward and back when the value is toggling from negative, positive, negative, and so on. Makes for a 'clean' looking output display that's easy to read.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike
Sign In or Register to comment.