Memsic 2125 Dual Axis Accelerometer - how does +/- sign in debug output come fr
John Kauffman
Posts: 653
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.
··· 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
(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
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.
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