Home > OS >  converting int into float dsPIC33
converting int into float dsPIC33

Time:08-03

I am trying to convert int16_t to float

code is

int16_t ADC_value;

float voltage = (float)ADC_value/1000.0f;

printf("%f\r\n",voltage);

result

when the voltage exceeds 2.0V floating point values become (-)values. but before it converts to floating point values it is normal.

code:-

int16_t ADC_value;

printf("%u\r\n",ADC_Value);  //this is correct

is that int16_t to float conversion wrong?

CodePudding user response:

For ADCs, I find this to be the prevailing pattern. Since you are using LTC1867, you need to determine whether the IC is operated in bipolar or uni-polar mode. The FSR is 4.096. If unipolar, you should use a uint16_t as the ADC value. If bipolar mode, you should use int16_t. In bipolar mode, the ADC is a signed twos complement, which can vary from /- 32767, so you would need to alter the FSR variable I use below.

const float fsr = 4.096;        // Full-scale range, Volts, depends on the ADC
const float max_val = 65535.0f; // For 16-bit unsigned ADC, 2^16 -1 
float voltage = fsr*((float)ADC_value / max_val); 
  • Related