Announcement

Collapse
No announcement yet.

Signed to unsigned conversion

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Signed to unsigned conversion

    Hi,

    I am receiving a 16 bit unsigned value for I2C bus.
    As the dispaly treats this as a signed value, I have written this conversion code to be able to display the value on the screen.

    But is there a smarter (faster) solution?


    // Convert channel 0 to unsigned and print
    // ADC_0 is a 2 byte unsigned value from I2C Bus.

    var TestNegativeSign:=0b1000000000000000;

    var Tmp; // temporary signed 16 bit var

    var DTmp[2]; // temporary Float var

    var FADC_0[2]; // Resulting Float var


    1 if (ADC_0&TestNegativeSign) // Test if MSB(7) is set, which will result in an unwanted negative number

    2 Tmp:=ADC_0&0b0111111111111111; // Change MSB(7) to 0

    3 flt_ITOF(DTmp,Tmp); // Load the Tmp value into DTmp Float var

    4 flt_VAL(FADC_0,"32768"); // Load Result with the MSB(7) that was removed in line 1

    5 flt_ADD(FADC_0,FADC_0,DTmp); // Add the remaining bits

    6 else // MSB(7) was not set

    7 flt_ITOF(FADC_0,ADC_0); // Use the original value.

    8 endif

    9 flt_PRINT(FADC_0,"%.0f");


    Thanks in advance

    Poul

  • #2
    Hi Poul,

    You can try this code below to display a 16-bit unsigned value.

    var value := 0xFFFF; // this the value from I2C, let's try it with 65535
    var ptr; //declare pointer to the value
    ptr := str_Ptr(&value); // let's create a pointer for printing the unsigned long value
    str_Printf(&ptr, "%u"); // print the value as unsigned int

    I hope this helps.

    If you have other inquiries, feel free to open a ticket here 4D Systems helpdesk or a new forum post.

    Best regards

    Comment


    • #3
      Hi ivan_4d

      Thanks, exactly want I wanted!

      regards Poul

      Comment


      • #4
        Hi Poul,

        We're happy to help.

        If you have other inquiries, feel free to open a ticket here 4D Systems helpdesk or a new forum post.

        Have a great day!

        Best regards

        Comment

        Working...
        X