PDA

View Full Version : ADCIN problems with PIC16F73



passion1
- 25th July 2007, 07:16
I am using a PIC16F73 with a 20MHz crystal. The 16F73 has a 8 bit A/D converter.
I have a current transducer connected to the RA3 pin

I am using the folling code:

DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3-internal clock)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time

result var word


TRISA = %00001011 ' Set RA0, RA1, RA3 to input, rest is output
ADCON1 = %00000100 ' Set RA0, RA1, RA3 to analog input, RA2, RA5 to digital I/O
' reference voltage = Vdd, left adjusted
ADCON0 = %11011001 ' A/D clock is internal RC, select RA3/AN3, turn on A/D



Within a loop I am calling ADCIN as follow:

ADCIN 3,result ' Read RA3 ADC channel


My problem is:
The transducer outputs a constant value of 2.54V which is right given the sensored current value, thus I know the
transducer is working properly.
However, ADCIN converts the constant 2.54V to a value that varies between 95 and 110?
If would be expecting the converted result to equal a value of +-130 (2.54/5*255=129.54)?
What bothers me most is why the ADCIN result is fluctuating between 95-110 while my voltmeter confirms a constant
2.54V as output from the transducer?
Any help or suggestion is appreciated.

mackrackit
- 25th July 2007, 10:00
(2.54/5*255=129.54)?
Should be 256 instead of 255. 0 to 255 = 256

What are you using for a reference voltage?
The results you are getting looks like the reference is swinging from 5.91 to 6.84 volts.
256 x SIGNAL / ADCresult = REF VOLTAGE

Do you have another volt meter?

passion1
- 25th July 2007, 12:04
Mackrackit

ADCON1 = %00000100 sets reference voltage to Vdd
I have measured the ref voltage going to the pic as +5.15V, thus I believe my voltmeter is accurate.
2.54/5.15*256=126.3 which is still far away from the reading of 95?

paul borgmeier
- 25th July 2007, 13:32
What is the output impedance of your transducer (is it < 10kOhms)?

Try converting the low rail and see if you get 0
Try converting the high rail and see if you get 255
If these work, try a simple voltage divider and see what you get.

Long Shot - You also are using a 20MHz XTAL and not putting the PIC to sleep while sampling ... you may want to try using the TAD of the XTAL rather than the TAD of the internal RC - you are "supposed" to use minimum TAD possible.

If no luck above, what is your power source? Do you have decoupling caps between VDD and VSS? How about a cap on RA3?

Let us know

paul borgmeier
- 25th July 2007, 13:43
Should be 256 instead of 255. 0 to 255 = 256

I believe it to be correct as originally written - the largest output from the ADC is 255, which should match AVDD....

IF the input is the upper rail ...

255/255 * AVDD = AVDD
255/256 * AVDD <> AVDD

my 2 cents

mackrackit
- 25th July 2007, 14:04
Hi Paul,

I missed the RA3 as the ADC input on this one, but I will stick to the idea that to get to 255 there are 256 steps.

What if there is a 5v ref and a 2.5v sig.

2.5/5*256=128

I thought this digital stuff started at 0?

passion1
- 25th July 2007, 14:27
Paul

Thank you for the suggestions.
Could you please suggest values for the decoupling caps between VDD and VSS and the cap on RA3?


I have also searched all the other threads and have come across some comment for related problems.
The suggestions include:
1. Try setting the settling time on the ADC to a few microseconds, then initiating a conversion.
At the same time, make the PIC SLEEP for around the same length of time as the conversion.
This should SIGNIFICANTLY reduce the noise across the PIC silicon substrate and give you a lot better accuracy.

2. As a start, increase your Sample Time and/or change your Clock Source.
It's a good possibility that you are not letting the sampling Capacitor enough time to charge properly before you take your reading.


3. Have a cap on ADC pin. Try from 10uF to 220uF. (Cap+ to ADC pin, Cap- to GND.)
and, change Pause 20 to Pause 500.

4. Input pin require a pullup resistor too.

5. For 20 MHz OSC; Fosc/32 could be the solution as it helped me solve a similar problem

Any comments which of the above suggestions would be applicable to my problem?
Is the settling time and sample time the same thing?
How do I make the PIC sleep?
Should I try a cap or a pullup resistor or both, and if so where must the cap and resistor be placed and
what values would be recommended?
Thank you in advance!

dhouston
- 25th July 2007, 14:29
Should be 256 instead of 255. 0 to 255 = 256
So, are there 13 inches in a foot, 37 inches in a yard, etc.?

0-255 represents 256 values (if you consider 0 is a value) but there are only 255 intervals. It's easier to see this by using smaller scales - e.g. 0-4.

With GND--1K--1K--1K--1K--5V, each resistor drops 1/4 of 5V.

paul borgmeier
- 25th July 2007, 14:31
>> missed the RA3 as the ADC input on this one
I am not sure how this relates to my post??

>> but I will stick to the idea that to get to 255 there are 256 steps.
I agree, 0-255

>>What if there is a 5v ref and a 2.5v sig. 2.5/5*256=128
no
256 takes 9 bits to represent 100000000
255 takes 8 bits to represent 11111111
127 takes 7 bits to represent 1111111

255 divided by 2 = 127
2.5/5*255=127

clear as mud?

>> I thought this digital stuff started at 0?
it does, 0-255

passion1
- 25th July 2007, 14:34
Paul

The resistance of the transducer I am using is:
RL Load resistance >= 2KOhm
RM Internal measuring resistance 208 Ohm

Could you please suggest values for the decoupling caps between VDD and VSS and the cap on RA3?

paul borgmeier
- 25th July 2007, 14:38
Passion1,

Try converting the rails first - what do you get?

What is the output impedance of your transducer? Can't suggest a cap for RA3 until this is known.

For VDD and VSS, try a 10uF electrolytic and a .1uF ceramic (in parallel) - but adding these blindly is not your solution - what are you using for a power supply? I cannot believe this (lack of these caps) is your problem if you get 95 or 110 out now.

What else is going on in the circuit?

paul borgmeier
- 25th July 2007, 14:45
Could you please suggest values for the decoupling caps between VDD and VSS and the cap on RA3?

I'd use 100 pF or so (5 time periods * 100e-12 * 2000 = 1uS, way less than your 50uS sample time). This is not your problem either, my thought was you might have had something too big on there slowing the charge. Adding a cap here will help keep the answer solid, but at 8 bits, you should not have a problem without for now.

the rails?

passion1
- 25th July 2007, 15:28
Paul

Please excuse my ignorance , but what do you mean with:
"Try converting the rails first"?
The transducer has 3 different settings to allow different ranges of current to be measured:
Setting Primary_current Output_voltage Primary_resistance Primary_inductance
1 +-6A 2.5+-0.625V 0.18mOhm 0.013uH
2 +-3A 2.5+-0.625V 0.81mOhm 0.05uH
3 +-2A 2.5+-0.625V 1.62mOhm 0.12uH

Currently I am using setting 1 but in future I might want to use setting 3 because it allows greater accuracy.

passion1
- 25th July 2007, 16:23
I'd use 100 pF or so (5 time periods * 100e-12 * 2000 = 1uS, way less than your 50uS sample time).

Paul

I assume this is the value for the cap on RA3?
Should it be electrolytic or ceramic?
Thanks for all the help!

paul borgmeier
- 25th July 2007, 16:45
Paul

Please excuse my ignorance , but what do you mean with:
"Try converting the rails first"?


Passion,

The low rail means ground - connect a wire directly from your circuit ground to AN3 and convert (you should get 0)

The high rail means your PIC supply voltage - connect a wire directly from your 5V supply (the same one connected to your VDD pin on your PIC) to AN3 and convert (you should get 255)

If you do not get these values, then something else is going on.

The 100 pF is for AN3 (I use ceramic :)). I had replied about the VDD and VSS caps before I saw your next reply giving impedance info and asking again. I followed with the 100 pF recommendation, which is for AN3. However, although good practice to have these, I do not think the lack of these are your problem.

Let us know how the rails convert.

Good Luck

passion1
- 25th July 2007, 20:10
Paul

Thank you.
When I connect the circuit ground to AN3 and convert, I get values fluctuating between 12-18.
When I connect the +5V supply to AN3, the values initially fluctuate between 248-255 and eventually stabilizes at 255.

I will be trying some of your other suggestions now and report back....

passion1
- 26th July 2007, 06:55
Paul & other

I have increased the sample time to 200 using DEFINE ADC_SAMPLEUS 200 but it did not solve my problem.
I have also tried Fosc/32 by setting DEFINE ADC_CLOCK 2, but no luck!

In my program I am using mister_e's matrix keypad routines and Watchdog is disabled.
I am also running HPWM in the background which is pulsing the current I am trying to measure. The HPWM is switched on/off at defined intervals.
Can this influence the ADC readings?

Next I will try a cap on RA3......

paul borgmeier
- 26th July 2007, 08:00
Paul

Thank you.
When I connect the circuit ground to AN3 and convert, I get values fluctuating between 12-18.
When I connect the +5V supply to AN3, the values initially fluctuate between 248-255 and eventually stabilizes at 255.

I will be trying some of your other suggestions now and report back....

Passion,
That is good news - your ADC is working. However, ....
You should get a solid 0 and a solid 255 - Try turning off the HPWM and other stuff going on and see what you get for these conversions (i.e., leave everything off and convert these two at the beginning of your code and see what you get.)

Again...what is your power supply?-

Let us know

EDIT: Your goal is to make the ADC work alone and then slowly add your other stuff and see what is causing the problem. Then you can address it ... a systematic approach!

savnik
- 26th July 2007, 08:24
Mackrackit

ADCON1 = %00000100 sets reference voltage to Vdd
I have measured the ref voltage going to the pic as +5.15V, thus I believe my voltmeter is accurate.
2.54/5.15*256=126.3 which is still far away from the reading of 95?

2/5*256=102
no decimal in pbp
use multiply x100

passion1
- 26th July 2007, 11:38
Savnik

The 2.54 and 5.15 is actual Voltage readings and is not integer variables within PBP. So I believe the calculation using floating point values is right?