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
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
Comment