This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

hmc 5883 interface with AT89S52

hi,
I'm working on the compass sensor hmc5883l with at89s52 controller. In the process of reading the data from the compass sensor, i am supposed to collect data from 2 registers and join these 2 datas to make a single data (in short concatenation 8 bit +8 bit =16 bit). the data present in the registers is in the form of 2's complement hexadecimal number.
can any1 suggest me how to convert this 2's complement data to a decimal number.
also i need to display this number onto the 16*2 char lcd. for this i again need to convert the decimal data to character data.

thanks in advance.

Parents
  • Get some books on programming in C, micro-processor architectures and read them. Some math skills would probably also help.

    www.cplusplus.com/.../

    int16_t value;
    uint8_t lowbyte, highbyte;
    
    value = (int16_t)((uint16_t)lowbyte | ((uint16_t)highbyte << 8));
    
    static char *itoa_i16_10(int i)
    {
      static char str[8];
      char *s = str + sizeof(str);
      int sign = 0;
    
      *--s = 0;
    
      if (i < 0)
      {
        sign = 1;
        i = -i;
      }
    
      do
      {
        *--s = '0' + (char)(i % 10);
        i /= 10;
      }
      while(i);
    
      if (sign)
        *--s = '-';
    
      return(s);
    }
    

Reply
  • Get some books on programming in C, micro-processor architectures and read them. Some math skills would probably also help.

    www.cplusplus.com/.../

    int16_t value;
    uint8_t lowbyte, highbyte;
    
    value = (int16_t)((uint16_t)lowbyte | ((uint16_t)highbyte << 8));
    
    static char *itoa_i16_10(int i)
    {
      static char str[8];
      char *s = str + sizeof(str);
      int sign = 0;
    
      *--s = 0;
    
      if (i < 0)
      {
        sign = 1;
        i = -i;
      }
    
      do
      {
        *--s = '0' + (char)(i % 10);
        i /= 10;
      }
      while(i);
    
      if (sign)
        *--s = '-';
    
      return(s);
    }
    

Children
  • The code is poorly documented, coder made undocumented assumptions, and code is not portable.

    Some Qestions/Answers

    Q1) What if sizeof(int) is 4?
    A1) function can write beyond array str[] limits

    Q2) What if sizeof(int) is 2 and i is -32768?
    A2) It will fail inverting the sign as there is no +32768

    Q3) Is the code safe? Do I want to see this in an embedded application.
    A3) Definitly not.

    :-)