Maker Pro
Maker Pro

Reading Negative Values of accelerometer

parag

Aug 17, 2015
4
Joined
Aug 17, 2015
Messages
4
Hello
Im interfacing accelerometer with TivaC and displaying the RAW data on UART.


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.
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
Apparantly, the function getAcceleration_X() can return a negative value. If this is so, it is converted to a positive value by multiplying by -1 before sending the resulting decimal value to the UART. Note that accelerationX is cast to unsigned short before sending as a decimal number to the UART. Previously it was a signed integer.
 
Top