12F683 ADC problems


+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159

    Default 12F683 ADC problems

    Hi all.

    I'm having an issue that is driving me crazy. I'm trying to use a potentiometer on GPIO4 (10K linear - wiper on GP4, ends on 5V and ground).

    Originally, I tried to use:

    Code:
    duty = adval 'adval is the result of the ADC
    It worked, but it appears that I'm only getting part of the way, as the led (on GP2/CCP1) only varies from off to about half brightness. I then discovered that the result is from 0-1025, not 255 as I had assumed.
    Then I tried:

    Code:
    duty = adval >> 2
    But the led never lights.

    After some more reading, here is what I have now. It still results in the led never lighting, no matter the position of the pot.

    Code:
    ;pic12f683
    #CONFIG
    cfg = _INTOSCIO
    cfg&= _WDT_ON
    cfg&= _PWRTE_OFF
    cfg&= _MCLRE_ON
    cfg&= _CP_OFF
    cfg&= _CPD_OFF
    cfg&= _BOD_ON
    cfg&= _IESO_ON
    cfg&= _FCMEN_ON
      __CONFIG cfg
    #ENDCONFIG
    
    DEFINE OSC  8
    ' Define ADCIN parameters
    Define ADC_BITS 8 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 50 ' Set sampling time in uS
    
    OSCCON=$70
    CMCON0    = 7
    TRISIO   = %111011      ;gp2= output
    ANSEL = %00111000 ' Set AN3 analog, rest digital
    CMCON0 = 7 ' Analog comparators off
    ADCON0.7 = 1
    
    adval Var word ' Create adval to store result
    duty var byte
    result var word
     
    mainloop:
        ADCIN 3, adval ' Read channel 3 to adval
        pauseus 50 'wait 50us for read to complete 
        result = adval >> 2 'right shift by 2 bits to divide by 4
        if result > 255 then result = 255
        duty = result.BYTE0  
        ' hpwm Channel, Dutycycle, Frequency 
        hpwm     1,duty,600   ; set led brightness at 600hz
        pause 50 ;Pause for 50 milliseconds   
        goto mainloop
    Anyone got any ideas what I'm doing wrong? It's getting a little frustrating. I am planning to do a test for the GO/DONE bit in ADCON0 to make the delay after read less, but I want to get this part working first.

    I would appreciate any help anyone can offer.
    Last edited by andywpg; - 5th April 2024 at 17:48.
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  2. #2
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: 12F683 ADC problems - SOLVED

    Figured it out.

    The problem was "define ADC_BITS 8". Not sure why I did that. Changed it to 10 and it works fine now.
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,619


    Did you find this post helpful? Yes | No

    Default Re: 12F683 ADC problems


    2.15.1 DEFINEs
    Since many of the DEFINEs in PBP pass data directly to the assembler, they should
    be considered case-sensitive. All defined parameters should be written in uppercase:


    DEFINE OSC 20 ' Correct
    DEFINE osc 20 ' Incorrect, and will not generate an error message

    just " RTFM "

    Name:  RTFM.jpg
Views: 343
Size:  23.0 KB

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,569


    Did you find this post helpful? Yes | No

    Default Re: 12F683 ADC problems

    Yes Alain, go RTFM and come back with another comic ;-)
    This has been covered almost as many times as "disable analog functions when using pins as digital" but since it continues to create false statements here we go again.

    It's not the word define that's case sensitive, it's what comes after it.
    DeFinE ADC_BITS 8 will work, define ADC_BITS 8 will work, Define ADC_BITS 8 (as it was written in the original code) will work and DEFINE ADC_BITS 8 will work. However, DEFINE adc_bits 8 will not because adc_bits is not written in upper case. Or, to state it even more correct, it's not written with the same case as it was originally defined. Had the PBP authours written it as adc_bits then adc_bits would be correct and ADC_BITS would not.

    So, again, it's not the word DEFINE that's case sensetive but what comes after it.

    My suspicion with the original code is that left/right justification setting was wrong for the 8bit result (I always get that wrong).

    /Henrik.

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,619


    Did you find this post helpful? Yes | No

    Default Re: 12F683 ADC problems

    Hi, Henrik

    was indeed a "RTFM" issue ... ... and you were also right !

    The result should be configured as left-justified if you are performing an 8-bit
    conversion. It should be right-justified if more than 8-bit precision is required. This
    is also a register setting and will be found in the datasheet section on the Analog
    Converter (ADC).
    manual p 116 ...

    BTW, it looks to be what to do with other Pics too ...
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. ADC on 12F683
    By JakeMend in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 7th July 2017, 01:44
  2. pic 12f683 adc help please
    By Helgeland in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 23rd February 2012, 15:31
  3. ADC in 12F683; puzzled
    By mikebike in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 23rd September 2010, 21:27
  4. 12f683 ADC help
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th November 2009, 22:37
  5. 12F683 Comparator Problems
    By morethanenuf in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 9th September 2005, 16:36

Members who have read this thread : 18

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