12f675 A/d Miseries


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2005
    Posts
    23

    Red face 12f675 A/d Miseries

    Hello all
    Running a 12F675 with 1 channel of a/d. Can someone take a look at this code and help me figure out why the a/d isn't running. (at least doesn't seem so) This thing is driving me crazy! I think it should run but it doesn't.
    Symptoms: My test board shows a/d in to pin as 0-5volts, but chip stays locked in "outrange" and led flashes on and off ok.
    What can I do to fix the code?
    Help will be appreciated-might save me from the rubber room!
    Thanks,
    Ron

    Here's the code:


    @_CONFIG_INTRC_OSC_NOCLKOUT &_WDT_ON &_PWRTE_ON &_MCLRE_OFF & BODEN_OFF
    'INTERNAL OSCILLATOR (4 MHZ)
    'ENABLE WATCH DOG TIMER
    'ENABLE POWER UP TIMER
    'DISABLE MASTER CLEAR FUNCTION

    '************************************************* ***************
    'VARIOUS OPERATION PARAMETERS

    'GPIO.0 IS A/D INPUT FROM VOLTAGE DIVIDER TO SET HIGH OR LOW
    'CHARGE. THIS FUNCTION TURNS ON 4710 MOSFET SWITCH (GPIO.4)
    'AND LED (GPIO.2) (STEADY ON) TO INDICATE FULL 3 AMP. CHARGE
    'TO BATTERY BETWEEN 12 VOLTS AND 16 VOLTS. OUTSIDE OF THESE
    'VOLTAGES, GPIO.4 IS LOW AND GPIO.2 FLASHES ON AND OFF, ONCE
    'PER SECOND. GPIO.3 IS MCLRE, PULLED TO Vcc BY 4.7K RESISTOR.
    'GPIO.0 AND GPIO.3 ARE INPUTS AND GPIO.2 AND GPIO.4
    'ARE OUTPUTS. (OTHERS COME UP AS INPUTS
    ' HARDWARE CONNECTIONS
    '====================
    'GPIO.0 A/D INPUT TO ADC CHANNEL 0
    'GPIO.1 N/C
    'GPIO.2 OUTPUT TO CASE LED(flashes on and off)
    'GPIO.3 MCLR PIN
    'GPIO.4 HIGH TO PHOTOVOLTAIC MOSFET DRIVER
    'GPIO.5 N/C
    '=====================

    DEFINE OSC 4
    DEFINE ADC_BITS 10 '10 BIT A/D CONVERSION RESULT
    DEFINE ADC_CLOCK 3 'INTERNAL A/D RC CLOCK
    DEFINE ADC_SAMPLEUS 50 'SET SAMPLE TIME IN MICROSECONDS
    CMCON = 7 'TURN COMPARITORS OFF
    adval0 VAR WORD 'Create adval0 to store result
    TRISIO = %001001 ' Set GSIO 0 & 3 TO INPUTS
    ANSEL = %01100001 ' Set GSIO 0 TO ANALOG A/D IN W/Frc
    ' OTHER PINS TO DIGITAL
    ADCON0 = %10000001 ' Vcc REF/Turn on A/D Module
    PauseUs 50 'Wait 50 MICROSECONDS
    START:
    ADCON0.1 = 1 'Start Conversion (GO/NOT DONE BIT SET)

    WHILE ADCON0.1 = 1:WEND 'MOVE ON WHEN FINISHED WITH A/D CONVERSION
    ADCIN 0, ADVAL0 'DEFINE C0NVERSION IN VAR ADVAL0

    IF (ADVAL0 <= 906) AND (ADVAL0 >= 604) Then INRANGE
    'GO TO FULL 3 AMP RATE (MOSFET TURNED ON)
    IF (ADVAL0 <= 603) OR (ADVAL0 >= 907) Then OUTRANGE
    'GO TO TRICKLE CHARGE (MOSFET OFF-NO RESISTOR SHUNT IN)
    '10 ohm IN SERIES WITH CHARGER.
    INRANGE: 'LED ON STEADY-MOSFET TURNED ON
    High GPIO.4: High GPIO.2
    GoTo START

    OUTRANGE:'LED FLASHES ON AND OFF MOSFET OPEN
    Low GPIO.4: High GPIO.2
    Pause 500: Low GPIO.2: Pause 490: GoTo START

    End

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


    Did you find this post helpful? Yes | No

    Default

    Hi Ron,

    Well there are still several questions to be able to figure out what's wrong. Such as ... what is the voltage of the charging power source? What are the voltage divider resistor values. Where is the reading being taken, before or after the 10 ohm resistor. and probably a few others.

    However, there are a few things going on in the program that are suspect, but may not really be the problem.

    You seem to be trying to use both the built-in PBP ADCIN command, and manually manipulating the A/D registers. You should pick one method or the other, not both.

    In this line...
    Code:
    ANSEL = %01100001 ' Set GSIO 0 TO ANALOG A/D IN W/Frc
                      ' OTHER PINS TO DIGITAL
    It sets the A/D oscillator to FOSC/64, which is listed as "outside of recommended range" in the datasheet.

    It then starts a conversion, waits for it to finish, then discards the result. That is followed by an ADCIN statement that changes the oscillator to FRC, (since that was specified in the ADC_CLOCK define), and takes another reading.

    Again, this won't cause the problem you're seeing, but it's still not right.

    So maybe it's something with the questions in the first paragraph. ??
    <br>
    DT

  3. #3
    Join Date
    Dec 2005
    Posts
    23


    Did you find this post helpful? Yes | No

    Red face What it is!

    Hi Darrel

    The resistors are set up to provide a range of voltages somewhere between 0 and 4.75 volts to a/d for battery voltage of 0 to 20. I am still tweaking them to push it as close to 5 volts as possible. (will change the a/d numbers in the code accordingly to get as close to 12 and 16 volts as possible)
    I am not quite sure I understand your first email! Would you mind changing the code in my example to what it should be and re post it? I would really appreciate this, since this chip seems to have caused many an otherwise good code writer to "come unglued"! I do know that its Data Sheet is based on MPASM and Microchip's language; but I have tried for 10 years, and cannot make sense of that jibberish, anyway!!

    Thanks again!
    Ron

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


    Did you find this post helpful? Yes | No

    Default

    Well, here's what I think you should have...
    Code:
    @  __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_OFF
    'INTERNAL OSCILLATOR (4 MHZ)
    'ENABLE WATCH DOG TIMER
    'ENABLE POWER UP TIMER
    'DISABLE MASTER CLEAR FUNCTION
    
    '****************************************************************
                       'VARIOUS OPERATION PARAMETERS
            
            'GPIO.0 IS A/D INPUT FROM VOLTAGE DIVIDER TO SET HIGH OR LOW
            'CHARGE. THIS FUNCTION TURNS ON 4710 MOSFET SWITCH (GPIO.4)
            'AND LED (GPIO.2) (STEADY ON) TO INDICATE FULL 3 AMP. CHARGE
            'TO BATTERY BETWEEN 12 VOLTS AND 16 VOLTS. OUTSIDE OF THESE
            'VOLTAGES, GPIO.4 IS LOW AND GPIO.2 FLASHES ON AND OFF, ONCE
            'PER SECOND. GPIO.3 IS MCLRE, PULLED TO Vcc BY 4.7K RESISTOR.
            'GPIO.0 AND GPIO.3 ARE INPUTS AND GPIO.2 AND GPIO.4 
            'ARE OUTPUTS. (OTHERS COME UP AS INPUTS
            '        HARDWARE CONNECTIONS
                    '====================
                    'GPIO.0 A/D INPUT TO ADC CHANNEL 0
                    'GPIO.1 N/C
                    'GPIO.2 OUTPUT TO CASE LED(flashes on and off)
                    'GPIO.3 MCLR PIN
                    'GPIO.4 HIGH TO PHOTOVOLTAIC MOSFET DRIVER
                    'GPIO.5 N/C                                
                    '=====================
                   
    DEFINE OSC          4
    DEFINE ADC_BITS     10  '10 BIT A/D CONVERSION RESULT
    DEFINE ADC_CLOCK    3   'INTERNAL A/D RC CLOCK
    DEFINE ADC_SAMPLEUS 50  'SET SAMPLE TIME IN MICROSECONDS
    adval0  VAR WORD		'Create adval0 to store result
    
    CMCON = 7               'TURN COMPARITORS OFF
    TRISIO = %001001        ' Set GSIO 0 & 3 TO INPUTS, others to OUTPUT
    ANSEL = %00110001 	    ' Set GSIO 0 TO ANALOG A/D IN W/Frc
    ADCON0.7 = 1	        ' Right Justify for 10-bit
    
    START:
        ADCIN 0, ADVAL0         ' Read A/D channel 0            
        IF (ADVAL0 <= 906) AND (ADVAL0 >= 604) Then   ; INRANGE
            High GPIO.4   'GO TO FULL 3 AMP RATE (MOSFET TURNED ON)
            High GPIO.2
        else                                          ; OUTRANGE
            Low GPIO.4   'GO TO TRICKLE CHARGE (MOSFET OFF-NO RESISTOR SHUNT IN)
            High GPIO.2  '10 ohm IN SERIES WITH CHARGER.
            Pause 500
            Low GPIO.2
            Pause 490
        endif    
    GoTo START
    
    End
    Also, there were some problem's with the CONFIG line. You can't see it in the post because it's all scruched up to the left. But, when you "Quote" post #1 you can see that there's no space between the @ and __CONFIG, and there's only 1 underscore before the CONFIG. There should be 2.

    Added: And there should be a space between __CONFIG and the first parameter _INTRC_OSC_NOCLKOUT.

    HTH,
    DT

  5. #5
    Join Date
    Dec 2005
    Posts
    23


    Did you find this post helpful? Yes | No

    Thumbs up Thanks Darrel!

    Darrel,
    The code you modified is working perfectly! Thanks very much! I was about to enter the proverbial "rubber room" over this one. I hope these examples will be helpful to others on the forum who have been messed over by the 12F675.
    By the way, have a happy new year!
    Ron

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


    Did you find this post helpful? Yes | No

    Default

    That's right. Bounce it off of us, before bouncing off the walls.

    Ha Ha, Happy New Year to you too!
    <br>
    DT

  7. #7
    Join Date
    Aug 2007
    Posts
    15


    Did you find this post helpful? Yes | No

    Default a/d 12f675 ,i can only use 1 and 2 not 500 to test input on a pot

    clean
    define adc_bits 10 'adc settings
    define adc_clock 3 ' adc setting
    define adc_sampleus 50 'adc setting
    intcon.6=1 'gloabal interrupt
    intcon.7=1 'gloabal interrupt
    pie1.6=1 'a/d interrupt
    pir1.6=1 'a/d interrupt

    adcon0=%10001111 'set an3 on ie gpio.4
    ansel=%00011000 ' sets fosc/8 and gpio.4[an3] as adc input
    cmcon=7 ' turn off comparator function
    trisio=%011000 'set gpio.4 as input and others as output



    'define variables
    rdg var word 'adc reading from an3
    mpin var gpio.2 ' music pin output to speaker
    led var gpio.0 'led output
    tone var word ' tone value for the sound

    'initial setting
    led=0

    'initial interrupt
    on interrupt goto testpot
    'program

    start:
    'reset led
    adcin 3,rdg 'get volt reading from an3 and place adc value in rdg

    goto start

    'isr
    disable
    testpot:

    if rdg =<1 then led=0
    if rdg =>2 then led=1 'turn on led if adc value is equal to or larger than 1


    tone=1+(rdg)*102 'converts adc value to a tone value of 1 to 120
    ' necessary to divide by 10 to avoid 65536 word limit
    'adc of 0=tone of 1, adc of 1023 = tone of 120
    sound mpin,[tone,20] 'provides a tone proportional to input volt duration = 20x12=120 min
    resume
    enable
    end

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mystified View Post
    clean
    What's that for?

    define adc_bits 10 'adc settings
    define adc_clock 3 ' adc setting
    define adc_sampleus 50 'adc setting
    Those aren't any good as they are written (unless there's something wrong with cut-and-paste, which happens sometimes)

    tone=1+(rdg)*102 'converts adc value to a tone value of 1 to 120
    ' necessary to divide by 10 to avoid 65536 word limit
    'adc of 0=tone of 1, adc of 1023 = tone of 120
    sound mpin,[tone,20] 'provides a tone proportional to input volt duration = 20x12=120 min
    Check your math...
    If rdg = 1 then
    tone = 1 + 1 * 102 = 103
    If rdg = 2 then
    tone = 1 + 2 * 102 = 205
    If rdg = 3 then
    tone = 1 + 3 * 102 = 307
    if rdg = 500 then
    tone = 1 + 500 * 102 = 51001
    Where is this 'divide by 10' you speak of?
    You declare tone as a word variable, yet the PBP manual states that values from 0-127 produce tones, 128-255 produces white noise, and therefore only values from 0-255 are legal, implying that tone should be a byte variable...
    Why not hook up an LCD to make sure your A/D is working in the first place? Then you can actually visualize your values to be output for tones.

  9. #9
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    What's that for?

    Probably wanted CLEAR, R vs N, Difference between BEAN and BEAR, or Been and Beer
    Last edited by Archangel; - 15th December 2008 at 19:02.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  10. #10
    Join Date
    Aug 2007
    Posts
    15


    Did you find this post helpful? Yes | No

    Default thanks for clear

    I thank you of your time
    The code
    define adc_bits 10 'adc settings
    define adc_clock 3 ' adc setting
    define adc_sampleus 50 'adc setting

    should set up the a the analog to digital converters (adc)
    the output from the adc converter should be 0 to 1023
    but i get just 0 to 3
    I will work on the sound after I can get the 0 to 1023to work.

  11. #11
    Join Date
    Apr 2006
    Location
    New Hampshire USA
    Posts
    298


    Did you find this post helpful? Yes | No

    Smile In CASE this helps...

    Hi mystified,
    Quote Originally Posted by mystified View Post
    I thank you of your time
    The code
    define adc_bits 10 'adc settings
    define adc_clock 3 ' adc setting
    define adc_sampleus 50 'adc setting

    should set up the a the analog to digital converters (adc)
    the output from the adc converter should be 0 to 1023
    but i get just 0 to 3
    I will work on the sound after I can get the 0 to 1023to work.
    Take a look at this informative thread by Melanie.

    Quote Originally Posted by Melanie View Post
    ... Watch the SPELLING of your DEFINEs

    Is your program not working as expected? ...

    Do you have any DEFINEs in your program? ...
    http://www.picbasic.co.uk/forum/showthread.php?t=558

    -Adam-
    Ohm it's not just a good idea... it's the LAW !

  12. #12
    Join Date
    Aug 2007
    Posts
    15


    Did you find this post helpful? Yes | No

    Default I changed clock 3 to clock 0

    cannot find clock 3 in datasheet it now works thank you

  13. #13
    Join Date
    Aug 2007
    Posts
    15


    Did you find this post helpful? Yes | No

    Default

    OK , I put clock back to 3 but used upper chase in the define statement and that is what made it work
    Thank You very much.

  14. #14
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mystified View Post
    cannot find clock 3 in datasheet it now works thank you
    Page 45 of the latest PBP manual

    and

    Page 44 of DS41190E, PIC12F675 datasheet.

    See any similarity between DEFINE ADC_CLOCK 3 and bits 6 thru 4 of ANSEL?

Similar Threads

  1. How do I use 10 bit A/D on 8 bit Pic? 12F675
    By polymer52 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st April 2020, 20:10
  2. Need help with LM34 and 12F675
    By polymer52 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 6th December 2009, 13:06
  3. 12F675 A/D and GPIO sleep interrupt
    By macinug in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th September 2008, 14:39
  4. Need advice on A/D
    By james in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 24th August 2007, 19:30
  5. A/D converter fails?
    By egberttheone in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 13th February 2006, 18:57

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