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 problem in pic16f877

Status
Not open for further replies.

janlyn

Junior Member level 1
Joined
Dec 14, 2004
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
169
adc problem

hello all...
i am a newbie in microcontroller...
hope u all can teach me...
jz now, i tried the sample code of ADC (PIC16F877)...
when i feed from 0v to 5v with resolution 1v, everything in expected besides 2v and 3v...

now,here is my question...
1) in pic16f877, there are 10 bit ADC = 1023. is tht mean 1 is equal 4.883mV in this case...so 1023x4.883mV =4.995V (~5V)?

2)if above is right, then when i fed AN0 with 3V, i jz get about 1.797V and while fed with 2V,i get 3.281V!others is still in condition...so, can u all help me to find where the problem is??i really stuck in this...
 

Re: adc problem

Hi,

If you willl read in the user manual of the PIC16F8xa:
The conversion of an analog input signal results in a
corresponding 10-bit digital number. The A/D module
has high and low-voltage reference input that is software
selectable to some combination of VDD, VSS, RA2
or RA3.
So in other words you will have to check your setting of the PCFG register what referance you are using.
For both or your questions it all depandands on the referance that you are using.
Good luck.

Added after 5 minutes:

Hi,

Go to the Hobby Circuits and Small Projects Problems in this board. They have few projects and asnwers for building a digital clock.
Good luck.
 

    janlyn

    Points: 2
    Helpful Answer Positive Rating
Re: adc problem

hi gidimiz,

i set my PCFG register as 0000...the reference voltage is Vdd and Vss, jz like the one required in sample program...but don't know why the 2V and 3V input voltage can't convert properly..

after thinking whole night..is below connection caused my problem??
i should not used external dc voltage but the dc supply of PIC with variable resistor??

thanks~
 

Re: adc problem

Hi,

Go to www.microchp.com and find the pdf for the PICDM2 demo board. you will find there a schamatic on how to connect a verible resistor to the PIC and also you will find some code on how to interface teh A/D. If you want findthe code, post me a message and i will send you the code.

Good luck.
 

    janlyn

    Points: 2
    Helpful Answer Positive Rating
Re: adc problem

Hi !

It is only some guesses about your problem. (i could not see your schematic).

1) The 10 bit conversion result is stored in two registers: ADRESH and ADRESL.
ADRESH contains the most significant bits and ADRESL the lesser significant bits, but it is possible to have the following division:

with bit ADFM = 0 (ADCON1 register) left justified
ADRESH = 11111111
ADRESL = 11xxxxxx

with ADFM = 1 right justified
ADRESH = xxxxxx11
ADRESL = 11111111

Check which format are you using. Maybe the error can be caused by this.

2) ADRESH is located in the Bank0 and ADRESL is located in the Bank1. Are you switching the banks properly ?

3) The source impedance. The datasheet says that the maximum impedance for analog sources is 10kohm.

4) Acquisition time. Are you giving time enough to charge the sampling capacitor (reach the measured voltage) ? See the datasheet for this calculation.

5) Have you set the adequate Tad (conversion time per bit) ? See the datasheet for more details.
 

    janlyn

    Points: 2
    Helpful Answer Positive Rating
Re: adc problem

hi gidimiz and rkodaira,

thanks for u two...

gidimiz,
i can't find the demoboard that u mentioned...
but, there are one reference manual tht teach ppl to connect pic with variable resistor..so,i connect the circuit as the manual teach.
although i do so, but the result came out exactly the same as previous one..

rkodaira,
yah,my setting is left justified and the program is just output the ADRESH..so,the two settings is not my problem..
and, in the testing program of adc, is the Acquisition time important??
i see the datasheet calculate them with internal capacitor,temperature....can u teach me how to calculate them, i'm very confused now..
the next is ...what is source impedance??the resistor connected or the source's internal resistance??

i see many ppl used this with voltage divider circuit..what is a voltage divider circuit??

i know i ask sth tht are very simple to u all...but,hope u all can teach me...
thanks~
 

Re: adc problem

Hi,

Here is the link for the PICDEM2 demo board from Microchip:
**broken link removed**
There you will find the user manual with a schamatic on how to connect to the ADC. And you will find there software samples.

If it still wont work after that, post you message and will replay.
good luck.
 

    janlyn

    Points: 2
    Helpful Answer Positive Rating
Re: adc problem

rkodaira,
yah,my setting is left justified and the program is just output the ADRESH..so,the two settings is not my problem..
and, in the testing program of adc, is the Acquisition time important??
i see the datasheet calculate them with internal capacitor,temperature....can u teach me how to calculate them, i'm very confused now..
the next is ...what is source impedance??the resistor connected or the source's internal resistance??

i see many ppl used this with voltage divider circuit..what is a voltage divider circuit??

Please refer to equation 11-1 from page 114 of 16F877 datasheet. There says that:

Tacq = Tamp + Tc + Tcoff

Tacq = acquisition time (time period between you turn the converter on ( set ADON bit ) and the start of the conversion (set GO/DONE bit)
Tamp = amplifier settling time = fixed 2us
Tc = hold capacitor charging time = Chold * Ric + Rss + Rs) * ln (1/2047)
Being
Rs = source internal impedance (it depends on your sensor, and must be <10kohm))
Ric = interconnect resistance (<1kohm);
Rss = resistance of sampling switch (=7kohm for Vdd = 5V see graphic)

Tcoff = temperature coefficient = [(Work Temperature - 25 degrees celsius)/(0.05us/celsius degree)]

See the example of calculation, which gives 19.72us. If you wait around 20us I think it is ok. Less time results in a non charged hold capacitor with the voltage to be measured. After this time the internal switch disconnects the capacitor from the voltage source to start the conversion itself, so no changes in the input will be considered from now.

The conversion time depends on the setting of Tad which has to be a minimum of 1.6us and max of 6.4us for a correct conversion. If you are using a 4MHz clock, the Tosc = 0.25us, so you should select Tad = 8 Tosc (8 * 0.25us = 2us) which can be obtained by
ADSC1 = 1 and ADSC0 = 0 (both bits from ADCON0 register)

After 12 Tad the bit GO/DONE is cleared and you can read the result on ADRESH and ADRESL (only after GO/DONE is cleared). To start a new conversion, you must wait some time to discharge the capacitor.

The impedance from the source (internal thevenin impedance) is important because the converter has resistances in series with the capacitor. If the source impedance is too high, the capacitor will spend a lot of time to be charged and the lecture will be erratic. In these cases, you should use an operational amplifier as buffer, as it has a very low output impedance. If you are using a high resistance potentiometer to simulate the variation of voltage, it can be the cause of you problem. See the datasheet of sensor to know the output impedance.


[/quote]
 

    janlyn

    Points: 2
    Helpful Answer Positive Rating
Re: adc problem

gidimiz and rkodaira,
thanks for u guys...

gidimiz,
with the connection of demoboard,now my adc works fine...
thanks a lot..

rkodaira,
thanks for ur information, it is useful to my project~
thanks a lot..
 

adc problem

Hi janlyn,

Can you pelase write what was the problem.
Thanks.
 

Re: adc problem

gidimiz,

in fact, i'm not sure where the problem caused ..
the only difference between my previous connection with the demoboard is the resistance between variable resistor and input of adc..

in the demoboard, 470 ohm is used...in previous connection,the value i used is 1kohm...after i changed to 470 ohm, all my adc from 0 to 5v can convert accurately.

is that the resistance causing my problem?
 

Re: adc problem

If changing from 1kΩ to 470Ω fixed the problem that is fine but it also means that in real circuits one should use a buffer to increase the input impedance of these AD inputs ..
 

Re: adc problem

hi IanP,

u mentioned buffer.. is buffer sth like op-amp?
can it replace voltage divider circuit if my adc input range is 5 to 24V??
hope u teach me...thanks~
 

Re: adc problem

Usually a buffer will be build based on a unity gain amplifier (opamp).
It provides high input impedance and low output impedance.
In your case if you reduce the input voltage (24V) by 6 times you will be able to operate in 0 - 5 voltage range.
And the error imposed by the high input impedance to the voltage devider will be neglegable ..
 

    janlyn

    Points: 2
    Helpful Answer Positive Rating
Re: adc problem

hi IanP,

thanks for ur explainantion, now i understand..
can u recomend me some buffer?
or the links that guide ppl to select buffer?
i confuse now how to select the best buffer...

thanks~
 


    janlyn

    Points: 2
    Helpful Answer Positive Rating
Re: adc problem

IanP,

thanks a lot...
i'll try out first ...
if have any problem i'll ask again..

regards,
janlyn
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top