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.

How to translate this "C" to Proton Compiler

Status
Not open for further replies.

Mondalot

Junior Member level 3
Junior Member level 3
Joined
Sep 28, 2013
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
235
Hey

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.... ?

THX.
 

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
 

Hey

At the end only it reduces to break down and store the TMP variable and send them to the display.
Code:
TMP = 234

              DEC     HEX      HEX        HEX   ASCII
[1]  234/100 = 2,34  +  30  =  2 + 30  =  32 =    2

                        HEX       HEX           HEX   ASCII  
[2]  234%100 =  34 / 10 = 3,4  =  3 +  30    =  33 =    3    

               HEX    HEX     HEX     ASCII
[3]  234%10  =   4  +  30   = 34  =     4

NUMBER TO DISPLAY = 234

THX.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top