How Do I Convert 10-bit A2D  Counts to Displayed Voltage in C? - Page 1
Forum section: electronics / general

author: martys
author_rank: Regular Contributor
timestamp: January 15, 2017, 05:45:14 pm
content: (I can't figure out if this question is too electronic to be also a programming post. A person of thirst seeks among many dry wells.Forgive me, ye gods of double-posting, but after a while and no replies on this board, my great impatience yielded to the temptation to seek elsewhere.)I am trying to figure out how to program a 10-bit MCU A2D converter in C-language to read battery voltage(approx 14V), but I need just a little guidance on how to develop an equation to best accurately display the battery voltage from my data.I have no problem getting the A2D result in counts nor displaying the result in decimal on my display, just need help trying to get get an idea on how to develop an equation.Attached is a simple voltage divider schematic that I am using to measure the battery voltage and uses an npn-output optoisolator to switch on the measurement when the battery voltage is to be sampled.I have measured the resistors, the voltage drops of the optoisolator, the measured voltages etc, and the attached shows the readings I get on my Fluke DVM.I notice the voltage drop across the opto-isolator varies non-linearly a bit and I want to correct for that but that is not my question.But how do I solve this seemingly simple voltage divider coding problem to get a conversion count multiplier so the voltage in counts is to be most accurately converted to decimal volts?(I know I can just do many reprogramming attempts with guesses to zero-in on the required multiplier, but what is the science in that!)

author: ajb
author_rank: Super Contributor
timestamp: January 15, 2017, 07:10:50 pm
content: I don't understand your schematic, but generally, for a single-ended ADC:VADC= (NADC*VRef)/(2b-1)Where :NADCis the ADC readingVADCis the voltage at the ADC inputVRefis the ADC reference voltageb is the resolution of the ADC in bits.If your ADC has a gain stage or you have a resistor divider in front of it, then you will need to add a suitable multiplication or division factor to the calculation.

author: Buriedcode
author_rank: Super Contributor
timestamp: January 15, 2017, 08:53:01 pm
content: Not trying to nit-pick but...VADC= ((NADC/2b)*Vref)So 1024 for the ADC. Even though it has a maximum value of 1023 (being 10-bit) it provides an even rounding error.

author: martys
author_rank: Regular Contributor
timestamp: January 15, 2017, 09:52:24 pm
content: I don't quite understand how this formula is to resolve my posted question..what am I missing?I want to calculate the conversion factor from the data I have presented and I don't know the A2D count before programming the MCU and debugging values.

author: FreddyVictor
author_rank: Regular Contributor
timestamp: January 15, 2017, 11:07:46 pm
content: So the Vout on your diagram is going to the uC A2D (ADC) pin ?if so, then the ADC reading should be approx (1.529/4.98) * 1024 = 314so, working backwards, the formula to get Voltage from the ADC reading:Vout = (ADC / 1024) * VrefVout = (314 / 1024) * 4.98 = ~1.527v

author: martys
author_rank: Regular Contributor
timestamp: January 15, 2017, 11:39:41 pm
content: Thanks FreddyVictor!So simple once I clearly understood it the way you've presented it!

author: macboy
author_rank: Super Contributor
timestamp: January 16, 2017, 03:17:15 pm
content: You could also do a calibration routine on the MCU. Input a known/measured voltage, let the MCU sample it, then tell the MCU what the known/measured value is. Let it calculate the multiplier for future use. This will give you the most accurate result since the tolerance of the resistors, etc. is all calibrated out.

author: martys
author_rank: Regular Contributor
timestamp: January 20, 2017, 11:02:06 pm
content: Macboy, you missed the point, I wanted to find out how to write the code to display the value correctly before programming the MCU.

author: danadak
author_rank: Super Contributor
timestamp: January 21, 2017, 12:27:12 pm
content: I think macboy was commenting on the earlier comment you madeabout taking care of the non linearity in the signal path, the opto-coupler.Attached zip file has a basic approach.Regards, Dana.