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

John Kauffman
09-21-2008, 08:05 PM
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.


Mike Cook
09-21-2008, 08:15 PM
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 (http://www.allsurplus.net/)

John Kauffman
09-21-2008, 08:25 PM
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).


Mike Cook
09-21-2008, 08:33 PM
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 (http://www.allsurplus.net/)