Help with setting adcon1 for 16f877


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    jessey's Avatar
    jessey Guest

    Default Help with setting adcon1 for 16f877

    Hi

    I'm trying to get my LM336BZ-5.0 reference chip to work with my 16f877 micro and have a question regarding setting up the adcon1 register if anyone can help me. I'm using RA0 (pin 2) for an analog input of a varying voltage from a CMOS circuit & RA1 (pin 3) to read my battery voltage. I'm trying to figure out how to set the adcon1 register so I can use RA3 as a positive input from my reference chip and for the two AN0 & AN1 for my adc in's?

    I'm using a pic pin so I will have the ability to turn the reference chip on/off in my code when needed via a 1.2K resistor to supply the +volts from the pic pin to the cathode of my reference chip which is also connected to RA3. I searched the archives for code to turn on the positive reference for RA3 and found the following example below:

    > As an example, lets say you want to have a positive only reference on AN3 with the 7 other analog inputs useable and have a 10 bit result. You would set the adcon1 register as adcon1=%10000001 <

    I read the data sheet and can't make head nor tail on setting the bits I need to accomplish this, below is the code that I'm using to read my adc inputs. I tried using adcon1=10000001 in place of ADCON1 = 2 but my 2 adc inputs won't function properly. Could someone please tell me how to set up the adcon1 to be able to do this? I'm assuming I need to turn on my ad converters as they seem to be off when I just use adcon1=%10000001.

    Check_The_CMOS_Output_Voltage:
    ADCON1 = 2 'turn on the AD Converters
    ADCON0 = $41'Set A/D to Fosc/8, Channel 0, On
    Gosub getad
    Moist = ADRESH
    ADCON1 = 7 'shut off (Disable) ADC
    Return

    Read_The_Battery_Voltage:
    ADCON1 = 2 'turn on the AD Converters
    ADCON0 = $49 ' Set A/D to Fosc/8, Channel 1, On
    Gosub getad
    Battery_Voltage = ADRESH
    ADCON1 = 7 'shut off (Disable) ADC
    Return

    Any help would be greatly appreciated.
    Thanks
    jessey

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Jessey

    If you go look at the Datasheet Section 11-2 ADCON1 Register, you will find there's a table for bits 3 thru 1 (PCFG3 thru PCFG0). In the C/R column which shows the number of Analog Channels (C) viz Number of References (R). 2/1 fits your bill exactly (bit setting 0101), this gives you AN0 and AN1 for ADC, and your VRef on AN3 with everything else Digital.

    Other things... bit 7 is the ADFM bit. 1=Right Justify for 10-bit ADC mode, 0=Left Justify for 8-bit mode.

    For the 16F877A, bit 6 (along with two bits in ADCON0) set the Conversion Clock. Suppose you chose to use Fosc/8, you would leave this as zero. For the normal 16F877 (not the A version), this bit is unused and set to zero.

    Bits 5 and 4 are unused, leave as zeros.

    Therefore the setup for ADCON1 would be...

    %10000101 for 10-bit ADC
    %00000101 for 8-bit ADC

  3. #3
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    That's great Melanie

    I'll give it a try. After looking at the manual again and with your explanation, it does make some sense, starting to anyways.

    Thanks
    jessey

  4. #4
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie

    I've had some time now to play with my circuit and try a few things to try and get it working properly. I have all but exhausted all my options to figuring out what is wrong, short of trying to find another suitable voltage reference chip that might work.

    I have a question to ask now if you have the time to answer. First off, your suggested %10000101 for 10-bit ADC and %00000101 for 8-bit ADC seem to function ok for AN0 & AN1 although I get the same abnormity on ANO that I thought would be corrected by adding the voltage reference chip. The problem I was trying to rectify with the LM336BZ-5 is that when I run the circuit on the TX then the variable on AN0 is always 4 to 5 numbers less than it is when running on the batteries. I was thinking that the difference was being caused by the different voltages, as the 7805 outputs 4.98 vdc and the MIC2954-03BZ gives me 4.99 vdc output. This 5 number difference is maintained through-out the whole range that the variable changes. Another point of using the reference chip is that when I measure the voltage of the circuit when it's being run on batteries and when the micro is in the sleep mode, then the voltage fluctuates from about 5.21 vdc up to 5.43 vdc which is another reason I decided to use a voltage reference chip.

    I have my circuit wired so that when it is powered from the mains that a relay is powered up which cuts out the battery power and the pic runs off the TX. When the TX is unplugged then the relay is deactivated and the batteries take over to power the pic circuit. I figured a relay was a good option to also disconnect the voltage divider from the batteries, keeping it isolated when running off the mains to prevent draining the batteries without the need to take the batteries out whenever the mains are powering the circuit.

    I can't seem to get my reference chip to perform as anticipated, I've included a schematics for how I have it wired to the pic just in case I missed out on something obvious but I'm pretty sure I have it wired correctly.

    With the way that I have the LM336BZ-5 reference chip wired, and using a pic pin to supply the positive, it will only adjust from a minimum of 4.10 volts to a maximum of 4.33 volts. I thought I would be able to adjust it to 4.096 Volts?

    Any suggestions on what I'm doing wrong here?

    Thanks
    jessey
    Attached Images Attached Images  

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Jessey,

    Well, now that you have a stable reference, I think the problem might be in using 2 different regulators.

    The MIC2954-03BZ is a 1% LDO part, and the 7805 will only hold to 4% of the target voltage. I usually see them running at around 4.85 - 4.90V.

    Since the "CMOS circuit" is powered from VDD, any change in the voltage may be changing the "Analog Out" value as well, causing it to act as a "Ratiometric" sensor. So even with a stable ref., the readings will still change according to the supply voltage.

    I think you might be able to get rid of the 7805, and still have the relay select the power source to the MIC2954. Something like this.
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=422">

    Added: The other half of the voltage divider going to RC5 (for AC power detect ??) is 1K. I cut the picture off a little short.
    Attached Images Attached Images  
    Last edited by Darrel Taylor; - 13th August 2005 at 20:05.
    DT

  6. #6
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel

    Pretty cool, I really like your wiring savvy. It sure gets rid of a lot of components. Thanks for pointing that out to me. I'll give it a try today and see how it works out.

    Thanks
    jessey

Similar Threads

  1. OSCON setting for 18F4520
    By Dennis in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 29th November 2009, 15:18
  2. PBP 2.60 & ADCON1 problem
    By bitbangerbob in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st October 2009, 12:06
  3. PICKit2 - warning about configuration words
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 26
    Last Post: - 4th August 2009, 14:01
  4. code size VS speed optimization setting?
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 28th April 2008, 14:38
  5. Setting up ADC....
    By robertmark68 in forum General
    Replies: 1
    Last Post: - 11th August 2006, 18:08

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts