I would like to translate this "C" to Proton Compiler is for accelerometre.
There are parts of the program that I understand and some do not.
C Code:
Code:
volatile char sval[8]; Array of 8 element
void main()
{
unsigned char i;
unsigned char tmp; "tmp" with String
while(1) Bucle While
{
lcd_cmd(L_L1+1); I understand
lcd_str("x="); I understand
tmp=acc_read(0x6); I understand
if(tmp & 0x80) Returns the logical AND of two values ¿ I believe.... ?
{
tmp=~tmp+1; It makes the complement of the variable "~" tmp +1
sval[0]='-'; out sign - to LCD
}
else
sval[0]='+'; out sign + to LCD
sval[1]=((tmp)/100)+0x30; NO understand
sval[2]=((tmp%100)/10)+0x30; NO understand
sval[3]=(tmp%10)+0x30; NO understand
sval[4]=0; NO understand
lcd_str(sval); Out all to LCD ¿ I believe.... ?
The 'if(tmp & 0x80)' is a 'bit-wise and' that will be true if the MSB of 'tmp' is set - in other words if the number is negative.
The 'tmp=-tmp+1' is generating the 2's compliment of the original value. Given the 'if' above, this means that 'tmp' holds a negative value and this turns it back to a positive one.
The 'sval[1]=' and similar statements are a way of converting the binary value to decimal. For example, '(tmp&100)' calculates the original value modulus 100 - lets assume tmp = 234 so 234% 100 would be 34. That is then divided by 10 which gives you the 3. However that '3' is still in binary (i.e.0x03) and the 0x30 is the binaryversion of the ASCII character '0'. A characteristic of the ASCII characters for the digits is that they are all in sequence so adding binary 0x03 to 0x30 gives 0x33 which is the ASCII character "3".
The 'sval[4]=0' null terminates the string that is made up of the characters stored in the sval array which is then sent to the LCD.
A lot of this would be easier if 'tmp' was signed in which case you could get the positive equivalent of a negative number using 'tmp=-tmp'. In fact some compilers will let you do this even on unsigned variables.
In effect a lot of this is equivalent to the 'itoa' function'
Susan