[ARM] Reading Negative Values of accelerometer

Status
Not open for further replies.

nsparag

Newbie level 6
Joined
Aug 14, 2015
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
136
Hello
Im interfacing accelerometer with TivaC and displaying the RAW data on UART.


Code:
void main(){

signed int accelerationX;

accelerationX = getAcceleration_X();
if (accelerationX>=0){
UART_OutString("\r\nX Axl: ");
UART_OutUDec((unsigned short) accelerationX);
} else {
UART_OutString("\r\nX Axl: - ");
UART_OutUDec((unsigned short) (accelerationX*-1));
}

}

Such type of code I got on some forum.
I'm not understanding why " accelerationX*-1 " is done when acceleration is negative.
 
Last edited by a moderator:

It appears that the value is stored in a unsigned variable which only holds positive numbers. Therefore a negative value needs to be multiplied by -1.

This has the result of storing the absolute value of acceleration.
 
I think
Code:
UART_OutUDec((unsigned short) (accelerationX*-1));
has no meaning. It is multiplied by -1 and again cast into "unsigned".

And other thing is
accelerationX;
is "int". But it is casted into "short". I guess output of
getAcceleration_X()
is short.
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…