Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ADC conversion and voltage average with PIC16F873

Status
Not open for further replies.

gdaporta

Member level 5
Joined
Apr 4, 2006
Messages
86
Helped
12
Reputation
24
Reaction score
1
Trophy points
1,288
Location
Argentina
Activity points
1,943
pic adc conversion

Hello everybody,

I´m looking for someone ho has some expierence in working with ADC.
Actually I'm working with a PIC, the 16F873 and I'm using the AD0 to test a voltage. This voltage is always oscilating, there are two possibilities, going up to 14.8v and then goes down to 12.3v; or going up to 28.8v and then goes down to 26.2v more or less.

I have to show the average voltage value in an LCD.

Right now, I put any fix voltage in AD0 and I convert and show it OK, but when it's oscillating I'm having a lot of problems to show the correct average. In major situations, the average calculation is under the real average voltage, when the voltage is under the 20v, my firmware shows 200mV less, and when the voltage is over 20v, my firmware shows 300 mV over.

For the average calculation I'm saving the maximum and minimum voltage, then I calculate the average and show it.

The ADC conversions are done by pooling an input, like a trigger.

The ADC input have a serial resistor of 150k, a grounded resistor of 15k and then a 47nF capacitor to ground.

Any idea or suggestion ???

By,

GuillerMo [AR]
 

16f873 adc

gdaporta said:
The ADC input have a serial resistor of 150k, a grounded resistor of 15k and then a 47nF capacitor to ground.

Bad move with the series 150K. Read the datasheet under ADC's and you'll see that not more than 2.5K input impedance is recommended. Essentially you are charging an internal capacitor inside the PIC and when charged it begins the conversion to a 10-bit digital value. You need low impedance in order to charge the capacitor quickly, otherwise your acquisition times will have to be increased substantially and usually beyond your circuit requirements. The datasheet has some intense calculations to cover impedance and acquisition times.

Avoid all the calculations and drive the ADC input with an op-amp. This will provide you a solid low impedance interface to the ADC. Do your voltage level changing before or with the op-amp and all should be fine to start reading voltages accurately.

Also, consider taking several ADC samples, adding them, and then dividing by the number of samples to get a better average reading. A running average is not a real average. Taking a number of samples in powers of 2 make for very simple and easy binary math by simply shifting the number to the right by the number of samples in terms of 2^x.

Read 8 samples and add them, then shift the value 3 times to the right for the average result:

Code:
dim adc as word
dim i as byte

adc = 0

For i = 0 to 7
   adc = adc + ADC_READ(0) 
Next i

adc = adc >> 3       ' shift number right 3 times, 8 is 2^3

If you take 16 samples shift the result right 4 times, 32 samples, shift right 5 times, and so on... you get the picture, right.
 

    gdaporta

    Points: 2
    Helpful Answer Positive Rating
pic16f873 adc

Perhaps an averaging filter might be more appropriate? Here's one for a 16 sample width;

AVG = AVG + (NEW - AVG)/16
 

adc average

TAkin average value MIGHT NOT BE great idea.tRY RMS VALUE WILL WORK OUT FOR U..
gO THROUGH THE DATSHEET OF ADC U ARE USING.U DID'NT MENTION THE naME OF ADC U R WORKING WITH.
iT WUD BE BETTER IF U USE A LOW VALUE POT....
 

adc pic16f873

xorcise said:
gdaporta said:
The ADC input have a serial resistor of 150k, a grounded resistor of 15k and then a 47nF capacitor to ground.

Bad move with the series 150K. Read the datasheet under ADC's and you'll see that not more than 2.5K input impedance is recommended. Essentially you are charging an internal capacitor inside the PIC and when charged it begins the conversion to a 10-bit digital value. You need low impedance in order to charge the capacitor quickly, otherwise your acquisition times will have to be increased substantially and usually beyond your circuit requirements. The datasheet has some intense calculations to cover impedance and acquisition times.

Avoid all the calculations and drive the ADC input with an op-amp. This will provide you a solid low impedance interface to the ADC. Do your voltage level changing before or with the op-amp and all should be fine to start reading voltages accurately.

Also, consider taking several ADC samples, adding them, and then dividing by the number of samples to get a better average reading. A running average is not a real average. Taking a number of samples in powers of 2 make for very simple and easy binary math by simply shifting the number to the right by the number of samples in terms of 2^x.

Read 8 samples and add them, then shift the value 3 times to the right for the average result:

Code:
dim adc as word
dim i as byte

adc = 0

For i = 0 to 7
   adc = adc + ADC_READ(0) 
Next i

adc = adc >> 3       ' shift number right 3 times, 8 is 2^3

If you take 16 samples shift the result right 4 times, 32 samples, shift right 5 times, and so on... you get the picture, right.

Thank's for your answer xorcise. I read a few minutes ago the uC datasheet and I found the mention to te impedance you are talking to me.

Tonight i will try changing the inoput resistors.

By,

GuillerMo [AR]

Added after 1 minutes:

nan_ishan said:
TAkin average value MIGHT NOT BE great idea.tRY RMS VALUE WILL WORK OUT FOR U..
gO THROUGH THE DATSHEET OF ADC U ARE USING.U DID'NT MENTION THE naME OF ADC U R WORKING WITH.
iT WUD BE BETTER IF U USE A LOW VALUE POT....

nan_ishan,

The ADC is inside de uC, it isn't an independent ADC.

By,

GuillerMo [AR]
 

pic16f873 a/d converter example

Mike said:
Perhaps an averaging filter might be more appropriate? Here's one for a 16 sample width;

AVG = AVG + (NEW - AVG)/16

Mike,

Perhaps I'm too slow, but I don't understand your calculation, could you explain what are the variables?

Thanks,

GuillerMo [AR]
 

adc con pic16f873

nan_ishan said:
TAkin average value MIGHT NOT BE great idea.tRY RMS VALUE WILL WORK OUT FOR U..
gO THROUGH THE DATSHEET OF ADC U ARE USING.U DID'NT MENTION THE naME OF ADC U R WORKING WITH.
iT WUD BE BETTER IF U USE A LOW VALUE POT....

nan_ishan,

Could you explain to me how to calculate an RMS value ???

Thanks !


By,


GuillerMo [AR]
 

voltage avg

nan_ishan said:
TAkin average value MIGHT NOT BE great idea.tRY RMS VALUE WILL WORK OUT FOR U..
gO THROUGH THE DATSHEET OF ADC U ARE USING.U DID'NT MENTION THE naME OF ADC U R WORKING WITH.
iT WUD BE BETTER IF U USE A LOW VALUE POT....

What is wrong with your keyboard?
 

adc pic24 impedance

There are many unknowns in your question. I am assuming that you are comparing reading of your PIC meter with some comercial meter. Few reasons come to mind for discrepancy, but there might be something else as well.

Is that meter that is "accurate" measuring average or RMS value? RMS would be short Root of Mean of Squared samples. If you don't know what this means, look it up on wikipedia. If your PIC meter measures average and comercial meter measures RMS it is obvious there is going to be different reading on them.

Do you allow sufficient sample time for hold capacitor to be fully charged? Tacq should be in several microseconds rough estimate.

What is maximum frequency of your signal being measured? Sample frequency should be several times higher than highest frequency being measured. If you cannot achieve required sampling rate with PIC, you will have to limit bandwidth of signal or change to different ADC.

It is always good idea to condition signal before ADC with opamp, and you could also cut off higher frequencies that you cannot measure.

These are just few pointers based on obscure info you placed here. If you post complete circuit and detailed info on signal you are measuring, what are project requirements for accuracy, running average time,... you could get more precise help.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top