PIC 16F877A - ADC configuring problem.

Discussion in 'Programmer's Corner' started by BAdhi, Jul 26, 2012.

  1. BAdhi

    Thread Starter New Member

    Jul 26, 2012
    5
    0
    HI,

    I've already done some simple projects using PICs, but this is the first time I am using the ADC module. I am using mikroC as the programming software. My problem with the ADC module is the configuration (or with the external circuit related to it).

    The main configurations that I have done is listed below,

    1- before configuring the ADC module, should I set its TRIS register. (at the moment I've set the TRISA register as an input (1)).

    2- my configuration of the ADC related registers are as follows,

    ADCON0=0b10000101
    ADCON1=0b01000001

    I am giving the ADC module a reference voltage and I use a 3 diodes and a resister to set that voltage (which should give the reference voltage of 5-.7x3=2.9V).

    The problem that I have is after setting the register and providing the voltages, I checked the voltage at the reference pin in the PIC (in this case AN3 or pin number - 5). But I get a voltage of 5V. Shouldn't I get the reference voltage from it?. And the expected value is not achieved from the adc either.

    So Is there a problem in the configuration or, have I got some problems in external physical circuit?

    The link to the datasheet of PIC16F877A can be found here

    I'd really appreciate if someone can look in to my problem.

    Thanks.
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Section 11.3 of the manual:
    "The port pins that are desired as
    analog inputs must have their corresponding TRIS bits
    set (input). If the TRIS bit is cleared (output), the digital
    output level (VOH or VOL) will be converted."
    So you have that right.

    The idea of a string of diodes as a reference voltage strikes me as an extremely bad idea. But it's usable if you don't care what the reference voltage is.

    One reason why the voltage on pin 5 might be +5 would be if the diodes were the wrong way around. Is that possible? You could try checking the voltage across each of them individually.
     
    BAdhi likes this.
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    THANK YOU for linking to the device data sheet! That saves us lots of time digging it up.

    Generally I don't see any config problems (except see below).

    I agree using diode drops as a reference is a poor idea. It is MORE unstable then just using Vdd. However, using 4 diodes the "other" way gives 2.8V and at least eliminated the wiggle of Vdd (it still had temperature variations).

    If you can pull the PIC do so and see if the reference gets better. If so then there is a code problem (probably the pin being an output).

    Finally, make ADCON0=0b10000001. You are starting a conversion with your setting.
     
    BAdhi likes this.
  4. BAdhi

    Thread Starter New Member

    Jul 26, 2012
    5
    0
    Thanks for the advice and the quick reply John P and ErnieM

    Can you give a better method to get a lower stable voltage from 5V? (is a series of resisters better than this :confused: )

    Yes I've checked the diodes many times, in fact, I've removed the PIC and isolated reference voltage and checked, then I got the expected value for that. So I don't think there is a fault in the circuit.

    I will try that too.

    Thanks again for the reply :)
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    So if the voltage works without the PIC then it sure sounds like the PIC is driving that pin high. VREF+ is common to RA3. You may want to try your code in the MPLAB simulator (or in circuit debugger if you have one) and watch that port pin to see if the pin truly goes high.
     
  6. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Now that I've thought about it, what you are saying simply cannot be happening. How can there ever be 5V across 3 diodes in series? Even if the processor could supply lots of current--which it can't--the diodes would carry that current and maintain some small drop, for a total of 2.5 or 3 volts. If the current is flowing and they actually have 5V across them, they must be glowing red hot!

    So I say the current is not flowing. You must have made a mistake somewhere.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    It's not across them, he has the diodes in series with the 5V supply tied to the input, with a pull-down resistor to ground. So if the pin outputs 5V the diodes just go off, the 5V isn't across the diodes, it's across the resistor. 0V is across the diodes.

    (Yeah, it would be much clearer if there was a schematic.)
     
    BAdhi likes this.
  8. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Oh, you're right. He said he expected the Vref to be 5-.7x3=2.9V.

    Well then, is the resistor carrying any current? What is its value, anyway, if it can be in the circuit and not pull the voltage appreciably off 5V?

    I looked in the data sheet and didn't find any listing of the impedance of pin 5 when it's used as Vref. I'd be surprised if it sources appreciable current, but it would still be nice to know.
     
  9. BAdhi

    Thread Starter New Member

    Jul 26, 2012
    5
    0
    Since you've mentioned about the schematic, I've attached it with this

    pin -1 - provides 5V
    pin -2 - output the reference voltage to the PIC
    pin -3 - provides the ground
    pin -4 - provides the value from the sensor - lm35

    One thing that I couldn't mention you guys before was that I am using the lm35 sensor (to measure temperature, if you want more info about it here's the data sheet). Anyway I don't think that it affects the Vref from pin -2

    After all, since I've already mentioned that when the Vref is measured after isolating this part form the PIC, I got the expected value, I don't think there is any problem in this schematic (yet, if there is please tell me :D).

    So I think there is something problem with the PIC circuit.(could be even a short circuit)

    Anyway guys thanks for Help and the advises, I'll post again if there's any improvement
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    A schematic should show the entire project, but at least we can all see your reference. It really needs a cap (I keep a bag of 0.1 uf to sprinkle about) across the 10K for noise suppression.

    As is, any variation in Vdd will mostly come right out the reference point. If you have another diode handy the attached picture shows a slightly better way to make a reference voltage as the Vdd variations get removed, and you are just left with the slop of the diode drops as the changeable part.

    But for now leave what you have as it should work. Later you can improve it if you see the need.

    The LM35 should not affect the reference voltage.

    [​IMG]
     
    BAdhi likes this.
  11. BAdhi

    Thread Starter New Member

    Jul 26, 2012
    5
    0
    Guys, I found the mistake at last it was the TRIS register configuration. I had set the TRISA register as 1, then I just removed that statement, and then got the expected Vref value from the AN3 pin
     
Loading...