cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f4xx ADC values to voltage float values

xtreme_ruler1
Associate II
Posted on October 19, 2013 at 04:49

The original post was too long to process during our migration. Please click on the attachment to read the original post.
7 REPLIES 7
Posted on October 19, 2013 at 05:17

%f not %d

Should have 3 decimal places, so %5.3f ?

float yourVoltage = (float)ADCResult * voltsPerBit;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xtreme_ruler1
Associate II
Posted on October 19, 2013 at 05:26

Thanks for the reply.

Variables window shows:

ADCResult    4026    

maxAdcBits    4095.0    

maxVolts    3.0    

voltsPerBit    7.3260075E-4    

Made the changes but the semi-hosting still shows output as:

PC1 Value is: 4027    PC1 Volts is: : 4027   

Posted on October 19, 2013 at 05:30

Most likely a half-assed printf() implementation on the CooCox side, try converting to millivolts, as an integer

 printf(''PC1 MilliVolts is: %d \n\n'', (int)(yourVoltage * 1000.0f) );

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xtreme_ruler1
Associate II
Posted on October 19, 2013 at 05:37

Thank-you, that works:

C1 Value is: 3413    PC1 Volts is: : 3413    PC1 MilliVolts is: 2500

3413/4095 * 3 = 2.5003663

Is there anyway to take that milli value and turn it to decimal?

Posted on October 19, 2013 at 05:47

For positive values

int mv = (int)(yourVoltage * 1000.0f);

int x = mv / 1000;

int y = mv % 1000;

printf(''%d.%03d V\n'',x,y); // assuming printf() implementation supports leading zero properly

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xtreme_ruler1
Associate II
Posted on October 19, 2013 at 05:50

Works:

PC1 Value is: 3414    PC1 MilliVolts is: 2501     PC1 Voltage is: 2.501 V

thanks a bunch.

Posted on October 19, 2013 at 06:06

Assuming that the printf() library is a light-weight integer only version, you might want to see if there is some option to include one that supports floats. Mind you it might have a significantly larger memory footprint, and have more features that you actually want, need, or might use.

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