Aug 26, 2015 #1 N 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: Aug 27, 2015
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.
Aug 27, 2015 #2 B BradtheRad Super Moderator Staff member Joined Apr 1, 2011 Messages 15,203 Helped 2,901 Reputation 5,814 Reaction score 2,984 Trophy points 1,393 Location Minneapolis, Minnesota, USA Activity points 113,923 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.
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.
Aug 27, 2015 #3 D Duresh 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; Click to expand... is "int". But it is casted into "short". I guess output of getAcceleration_X() Click to expand... is short.
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; Click to expand... is "int". But it is casted into "short". I guess output of getAcceleration_X() Click to expand... is short.