Author Topic: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?  (Read 2828 times)

0 Members and 1 Guest are viewing this topic.

Offline martysTopic starter

  • Regular Contributor
  • *
  • Posts: 129
  • Country: us
How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?
« on: January 15, 2017, 05:45:14 pm »
(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!)
« Last Edit: January 15, 2017, 11:42:09 pm by martys »
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: us
Re: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?
« Reply #1 on: January 15, 2017, 07:10:50 pm »
I don't understand your schematic, but generally, for a single-ended ADC:

VADC = (NADC*VRef)/(2b-1)

Where :
NADC is the ADC reading
VADC is the voltage at the ADC input
VRef is the ADC reference voltage
b 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.
 
The following users thanked this post: TheInfernoMan

Online Buriedcode

  • Super Contributor
  • ***
  • Posts: 1612
  • Country: gb
Re: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?
« Reply #2 on: January 15, 2017, 08:53:01 pm »
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.
 
The following users thanked this post: martys

Offline martysTopic starter

  • Regular Contributor
  • *
  • Posts: 129
  • Country: us
Re: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?
« Reply #3 on: January 15, 2017, 09:52:24 pm »
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.


« Last Edit: January 15, 2017, 10:02:20 pm by martys »
 

Offline FreddyVictor

  • Regular Contributor
  • *
  • Posts: 164
  • Country: gb
Re: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?
« Reply #4 on: January 15, 2017, 11:07:46 pm »
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 = 314

so, working backwards, the formula to get Voltage from the ADC reading:
Vout = (ADC / 1024) * Vref
Vout = (314 / 1024) * 4.98 = ~1.527v
 
The following users thanked this post: martys

Offline martysTopic starter

  • Regular Contributor
  • *
  • Posts: 129
  • Country: us
Re: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C? (Solved!)
« Reply #5 on: January 15, 2017, 11:39:41 pm »
Thanks FreddyVictor!

So simple once I clearly understood it the way you've presented it!
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2256
  • Country: ca
Re: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?
« Reply #6 on: January 16, 2017, 03:17:15 pm »
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.
 

Offline martysTopic starter

  • Regular Contributor
  • *
  • Posts: 129
  • Country: us
Re: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?
« Reply #7 on: January 20, 2017, 11:02:06 pm »
Macboy, you missed the point, I wanted to find out how to write the code to display the value correctly before programming the MCU.

 

Offline danadak

  • Super Contributor
  • ***
  • Posts: 1875
  • Country: us
  • Reactor Operator SSN-583, Retired EE
Re: How Do I Convert 10-bit A2D Counts to Displayed Voltage in C?
« Reply #8 on: January 21, 2017, 12:27:12 pm »
I think macboy was commenting on the earlier comment you made
about taking care of the non linearity in the signal path, the opto-
coupler.

Attached zip file has a basic approach.


Regards, Dana.
Love Cypress PSOC, ATTiny, Bit Slice, OpAmps, Oscilloscopes, and Analog Gurus like Pease, Miller, Widlar, Dobkin, obsessed with being an engineer
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf