cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Integer to float

Sax
Associate III

Hello everyone, can someone tell me why casting doesn't work. I want to convert int values of an array to float.

for (int i = 0; i<= adcBufferSize; i++)

{

adc_buffer_float[i] = (float)adc_buffer [i];

}

12 REPLIES 12
Miha
Associate II

I have the same problem. I read analog values with my ADC and values are type of uint16_t but when i convert my array of values that i get from ADC (Because of doublebuffering) to Float numbers get weird. Is it because of endians, maby overflows i dont know. I have it all on my work PC but i can get a screenshot tomorow.
I have tried
for(int n = 0;n < BuffSize;n++){
FLOATARRAY[n] = (float) INTARRAY[n] * 1.0f; 
}

 

You'll need to show the definitions, and datasets which are failing, to have any idea as to why the presented code fragment is failing.

I doubt it's got anything to do with endian ordering or overflow, as a float should have no trouble representing a number in the 0-65535 range. Where you'll get issues is 8 or more digits.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I discoverd my problems nest. Its not in typecasting it is in declaration. I have few arrays named 

uint16_t ADC_BUFFER_RAW [ADC_BUFFER_LENGHT];

float ADC_BUFFER [ADC_BUFFER_LENGHT];

float FFT_BUFFER [FFT_BUFFER_LENGHT];

float MAGNITUDE_BUFFER[FFT_BUFFER_LENGHT/2];

If i define FFT_BUFFER_LENGHT to be more than size of 31,

it corupts my values found in ADC_BUFFER. I have no idea why.

 

My .map file shows that registers are not overlaping:


.bss.fft_instance
0x00000000200002e4 0x18 ./Core/Src/main.o
0x00000000200002e4 fft_instance
.bss.ADC_BUFFER_RAW
0x00000000200002fc 0x80 ./Core/Src/main.o
0x00000000200002fc ADC_BUFFER_RAW
.bss.ADC_BUFFER
0x000000002000037c 0x100 ./Core/Src/main.o
0x000000002000037c ADC_BUFFER
.bss.FFT_BUFFER
0x000000002000047c 0x40 ./Core/Src/main.o
0x000000002000047c FFT_BUFFER
.bss.MAGNITUDE_BUFFER
0x00000000200004bc 0x20 ./Core/Src/main.o
0x00000000200004bc MAGNITUDE_BUFFER
.bss.ADC_CONVERSION_COMPLETE
0x00000000200004dc 0x1 ./Core/Src/main.o

(TEXT is slightly diferent as HTML does not support original text)