Help with setting adcon1 for 16f877


Closed Thread
Results 1 to 9 of 9
  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 21: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

  7. #7
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel

    Your re-wiring of my schematics solved all my problems ....... good show. It now maintains the 4.99 vdc in either power mode. I thought I would have to change the code that I have to read the battery voltage but when I adjusted the LM336BZ-5 reference chip to output 4.30V it worked out perfectly to display the actual battery voltage. Another thing I noticed is that when I was using the two voltage regulators in my circuit and running the circuit on batteries & only when the micro was in the sleep mode and the milliamp draw was at about .126mA to .235 mA then the circuit battery voltage output of the MIC2954-03BZ would fluctuate from about 5.21 vdc up to as high as 5.43 vdc but now it maintains the proper 4.99 vdc in sleep as well, which is great. Any ideas what could have been causing that?

    Another question if you have the time, the reason I'm monitoring the battery voltage is so I can have a low battery alarm sound when the battery voltage drops to a certain level that is user programmable with push buttons, if that's an accepted way of doing it. I have it all coded and working good for the 4 AA batteries and it seems to be functioning great.

    My thoughts were that the user could switch to a different voltage rated battery other than just being limited to using the mains (TX) or the 4 double AA batteries to operate the circuit. Possibly to be able to switch to use a 6 or 12 volt deep cycle battery as well for remote areas. I was thinking I could have the code set up for two different receptacles, one to plug in the 4 AA batteries and another one for either a 6 or 12 volt deep cycle battery. Whichever battery that was plugged in first would disable the other receptacle and automatically configure the proper set level and voltage divider for that battery. Doable anyway, and I'm sure I could write code for that easily from what I've learned so far with picbasic pro.

    I haven't read any discussions on doing this and this is my first attempt at a battery operated circuit so I'm a little in the dark on this one. Does the voltage level alone determine when to change the batteries or battery, or is it the remaining amperage level of the batteries that determines when to change them, or is it a combination of both? I've read in the archives that there are voltage regulators that have a low battery alarm built into the chip that outputs a low signal when the battery level gets low but no discussions on how to do a home brew of it or how it determines that the battery is getting too low to operate the circuit? I've heard of load testing an automotive battery but am not sure how the procedure works or if it would apply to pic circuits in any modified form? Would you have any ideas or suggestions that could get me started or pointed in the right direction to accomplishing this or would I be better off just using a regulator with a built in alarm?

    Thanks
    jessey

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


    Did you find this post helpful? Yes | No

    Default

    WooHoo, that's great.

    That's odd about the voltage in sleep mode. The only thing I can see is the voltage divider coming from the battery. If RC4 were set to input when going to sleep, it would leave just a 10K resistor between the batteries and RA1. Since the battery voltage is higher than VDD, it would conduct through the protection diodes of the RA1 pin to VDD. With 10K and 6V you could easily push 100uA or more thru that way. With the pic in sleep mode it will draw less current than is flowing thru the diode, causing VDD to rise.

    However, I don't see any reason why it shouldn't still be doing that. Like I said ... That's odd.
    Added: Unless the battery voltage has dropped to a point where it's not pushing as much current thru the diode??

    ... there are voltage regulators that have a low battery alarm built into the chip ...
    The MIC2954-08BM is similar to what you're using but also has an ERROR output that goes low when the output voltage drops below it's regulation point. But its an 8-pin SOP package, so it may not be the best choice if you're making a through hole board.

    About the dead battery detection, hopefully somebody else here can answer that one. I don't know enough about them.
    Last edited by Darrel Taylor; - 16th August 2005 at 11:21.
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Ha ha!

    I just scrolled up and looked at the schematic again. And if you actually wired it that way, then it makes sense now.

    I put the voltage sense on VDD instead of the batteries. That way there's no leakage through the diodes. And, since you have a reference that is independant of VDD you should be able to tell when the batteries are too low to hold the regulation point. But, my previous explanation is probably what was happening before.

    What a memory, NOT!

    Darrel

Similar Threads

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