configuring AD converter in a 12F615


Closed Thread
Results 1 to 20 of 20
  1. #1
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81

    Default configuring AD converter in a 12F615

    I'm having trouble getting an analog reading on GP4/AN3 of the 12F615 chip. Been wading through the spec sheet and trying to find all the right bits in the various registers. No matter what I try, I get a value of 1023 from this:
    adcin gpio.4,reading

    Here are the register setups I'm using:
    ADCON0 = %11001101 ' select analog input for AN3
    ANSEL = %00001000 ' select analog input for AN3
    vrcon = %10000000 ' CVref = ON
    CMCON0 = %10000111 ' comparators off

    Can anyone tell me what I'm doing wrong? Must be something really dumb.
    "Do or do not, there is no try" Yoda

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


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Use the AN number in ADCIN statements instead of the PORT.pin.

    Code:
    adcin 3,reading
    And you don't need the ADCON0, VRCON or CMCON0 statements.
    But you will need ADCON0.7 = 1 ; Right Justify A/D result

    Since you are getting 1023 as the result, I assume you've set the ADC defines to 10-bit.

    Also, make sure your primary oscillator is set to _INTRC_OSC_NOCLKOUT in the configs (it's the default).
    DT

  3. #3
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Thanks Darrel! It WAS something dumb! Got it working.

    So what I'm trying to do is read the battery level of the 2 coin batteries that power the PIC so I can have a low battery warning. Now I need to figure out if that can be done. I'm guessing I have to change the voltage reference? I don't have a good intuitive grasp of A/D converters...
    "Do or do not, there is no try" Yoda

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


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Yes, that can be done with the 615.
    And you don't even need to use a PIN to do it.

    I've got a code snippet around here somewhere.
    I'll see if I can find it ... or re-write it.
    DT

  5. #5
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    That would be awesome Darrell!

    I have something working now using a 1N914 diode from the pin to ground and a 10K to plus from the pin. Sorta using the diode as a reference. But it's depending on the diode drop which may not be accurate in production.

    A no-pin solution would be a better design. Have you posted this idea to the forum in the past? If not I'm sure that many of us that design battery powered products would benefit from your genius!
    "Do or do not, there is no try" Yoda

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


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Yes, I had posted it before. But it was for a different chip, and I can't find it now.
    It's not too difficult, so I re-wrote it for the 12F615 and tested it on a LAB-X4.

    The idea is to use the internal 0.6V reference (ADC channel 5).
    The reference stays the same as VDD changes, so with VDD as the ADC's Vref, the value read changes too. You can use that change to calculate actual VDD without needing an external Analog Pin.

    Code:
    DEFINE ADC_BITS 10
    
    LCD       VAR GPIO.1              ; serial pin for LCD
    LCDbaud   CON 396                 ; serial baud rate
    
    ADval     VAR WORD
    VDD       VAR WORD
    
    ANSEL  = 0                        ; All Pins are Digital
    ADCON0.7 = 1                      ; Right Justify A/D result
    HIGH LCD : PAUSE 2                ; Initialize serial data level
    
    SEROUT2 LCD,LCDbaud,[$FE,1] : PAUSE 3 ; Clear LCD
    
    GOSUB GetVDD
    
    SEROUT2 LCD,LCDbaud,["A/D=",DEC ADval,"  ", _
                         "VDD= ",DEC VDD/10,".",DEC1 VDD]
    STOP
    ;------------------------------------------------------------------
    Vref_AD   CON 117           ; A/D reading for 0.6V Vref @ 5V VDD
    
    GetVDD:
        VRCON.4 = 1                   ; Enable 0.6V Fixed reference
        PAUSE 10                      ; allow reference to stabilize
        ADCIN 5, ADval                ; read the 0.6V reference
        VDD = (Vref_AD * 50) / ADval  ; convert to volts
        VRCON.4 = 0                   ; turn off fixed reference
    RETURN
    The VDD result has 1 decimal place, so 5.0V will be 50.

    HTH,
    DT

  7. #7
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Awesome Darrell!

    I plugged in your brilliant code and it works flawlessly!

    Is it possible to get 2 decimals of resolution using the 10bit ADC? On a 3 Volt system that drops out a 2.0 Volts it could make a difference.
    "Do or do not, there is no try" Yoda

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


    Did you find this post helpful? Yes | No

    Default

    Well as far as the math goes, you can get 2 decimals by multiplying *500 instead of 50. Then change the SEROUT2 to divide by 100 and use DEC2.

    But those references aren't really accurate enough to get that kind of resolution reliably.
    And they drift some with temperature, so you'll probably want to stick with 1 decimal place.

    You could also do the same thing with an external "Precision" reference and get better resolution. But of course that uses a PIN, which are precious on an 8-pin chip.
    DT

  9. #9
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Yeah, I though that would work. I tried X500 and got readings that varied by about .05 Volts even when I hit the PIC with a heat gun to see if there would be significant temperature drift. Not bad actually.

    This is a totally awesome way to read the battery voltage Darrell. THANKS AGAIN. You are a friggin genius!
    "Do or do not, there is no try" Yoda

  10. #10
    klantz12's Avatar
    klantz12 Guest


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Can anyone help me with configuring the AD converter in 16F877A on the labX1 board? I am using MicroCode Studio-pbp and i have tried the following code from the help button and from another post i read.


    ADCON1=132 ' 128 + 4 = %10000100 'Left just 3 analog inputs rest digital
    TrisA = 255 'all inputs for the a to d
    Define ADC_BITS 10 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (rc = 3)
    Define ADC_SAMPLEUS 50 ' Set sampling time in microseconds

    AdcIn 1, W0
    AdcIn 3, W1


    The error says that ADCIN needs a variable.

    My assignment is just to read the AD input from the three potentiometers at A0 A1 and A3.

    Pretty lost. Thanks! also, i didnt know how to start a new thread so sorry for posting in ya'lls.

    Kay

  11. #11
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    didnt know how to start a new thread so sorry for posting in ya'lls.
    Big button near the top of forum pages, "+ Post New Thread".
    Dave
    Always wear safety glasses while programming.

  12. #12
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    AdcIn 1, W0
    AdcIn 3, W1

    The error says that ADCIN needs a variable.

    Kay, I'm assuming that you have migrated to the PIC world from Basic Stamps and you may not have defined your variables - something you do not need to do in Stamps. So you need:

    W0 var byte

    to define W0 as a byte variable - these definitions usually go near the top of the page.
    "Do or do not, there is no try" Yoda

  13. #13
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    For 10-bit A/D you'll want to define W0 & W1 as WORD sized vars...;o)
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  14. #14
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Quote Originally Posted by tekart View Post
    So what I'm trying to do is read the battery level of the 2 coin batteries that power the PIC so I can have a low battery warning.
    I recently lost a NAS HDD so I don't have the code but 4-5 years back I designed a generic, battery powered ADC module. My battery monitor used a couple of pins so it probably is not applicable here but it may be useful for others. In my case, I needed only a single ADC input pin so I had a surplus of pins.

    I used two BR2032 coin cells. BR type cells maintain their rated charge until slightly before their end of life while CR type cells fall off gradually throughout their life.

    The batteries connected to Vdd and I fed an LP2980 LDO from a PIC pin. Vdd was 6V but the PIC12F675 (and PIC12F615) can handle 6.5V. The output of the LDO was used to power the attached analog sensor and also connected to Vref. A second pin can also supply the LDO if more current is needed.

    To monitor the battery, measure the output of the LDO using Vdd as reference. Initially, the reading will be something less than maximum but will approach maximum as the batteries start to drop off just before end of life.

    This wasn't my original idea but I cannot recall where I came across it.
    Last edited by dhouston; - 18th February 2011 at 00:10.

  15. #15
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    166


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Hi All,

    I am having difficulty understanding a line from Darrel's no-pin battery monitor code snippet. Can someone please explain the line

    Vref_AD CON 117 ;A/D reading for 0.6V Vref @ 5V VDD

    Is this a constant or does it vary? I am confused!

    I would like to implement this no-pin battery monitor into a project using a 12F683. The two devices appear to be pin compatible so I am hopint this will work. Am I sadly mistake?

    Cheers
    Barry

  16. #16
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Quote Originally Posted by Aussie Barry View Post
    Vref_AD CON 117 ;A/D reading for 0.6V Vref @ 5V VDD

    Is this a constant or does it vary? I am confused!
    From the manual:
    4.6. Constants

    Named constants may be created in a similar manner to variables. It can be more convenient to use a constant name instead of a constant number. If the number needs to be changed, it may be changed in only one place in the program; where the constant is defined. Variable data cannot be stored in a constant.

    Label CON Constant expression

    Some examples of constants are:

    mice con 3
    traps con mice * 1000

    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Quote Originally Posted by Aussie Barry View Post
    I would like to implement this no-pin battery monitor into a project using a 12F683. The two devices appear to be pin compatible so I am hopint this will work. Am I sadly mistake?
    The 12F683 does not have an internal 0.6V fixed reference.
    So you won't be able to do it with that chip.
    DT

  18. #18
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    166


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Thanks for the input Darrel and Dave.

    It is amazing what you can find in the manual when you know where to look

    Darrel, can you please explain how you arrived at the value 117 for the Vref_AD constant? I would have expected it to be 0.6/5*1023 = 122

    For my project I was able to use another ADC pin to monitor the supply voltage so all is good (for the time being).

    Cheers
    Barry

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


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Quote Originally Posted by Aussie Barry View Post
    Darrel, can you please explain how you arrived at the value 117 for the Vref_AD constant? I would have expected it to be 0.6/5*1023 = 122
    The internal fixed references are not very accurate, so you have to read the actual value to determine the number to use.

    For my project I was able to use another ADC pin to monitor the supply voltage so all is good (for the time being).
    Thats a bit like lifting yourself up by your own bootstraps.
    DT

  20. #20
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    166


    Did you find this post helpful? Yes | No

    Default Re: configuring AD converter in a 12F615

    Thanks Darrel.
    I am using the other ADC pin to sense the pre-regulator voltage (via an appropriate voltage divider).
    If the pre-reg voltage drops below 5.3V then the circuit will flash a "Low Battery" indicator and disable any future water pump operation.
    I am trying to save the 6V SLA battery from a harmful depletion condition.

    Cheers
    Barry

Members who have read this thread : 1

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

Tags for this Thread

Posting Permissions

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