Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[ARM] Reading Negative Values of accelerometer

Status
Not open for further replies.

nsparag

Newbie level 6
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:

BradtheRad

Super Moderator
Staff member
Advanced Member level 7
Joined
Apr 1, 2011
Messages
14,806
Helped
2,878
Reputation
5,768
Reaction score
2,901
Trophy points
1,393
Location
Minneapolis, Minnesota, USA
Activity points
110,637
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.
 

Duresh

Junior Member level 3
Junior Member level 3
Joined
Mar 12, 2011
Messages
26
Helped
11
Reputation
22
Reaction score
11
Trophy points
1,283
Activity points
1,430
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top