A little confusion about ADC

Discussion in 'Programmer's Corner' started by R!f@@, Oct 17, 2016.

  1. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    I always use ADC with internal reference, and use ADC_Read() function.
    I looked at the mikroC help and it shows this.

    ADC.png
    In the Note: it says "This function doesn't work with the external voltage reference source, only with the internal voltage reference."

    In my current project I set the Vref as external. GND and +5V applied to Vref Pin.

    Code (C):
    1.   ADCON1.ADFM = 1; // A/D Right Justified.
    2.   ADCON1.VCFG1 = 1; // External Vref-.
    3.   ADCON1.VCFG0 = 1; // External Vref+.
    I am confused a bit.
    Can any one explain this. Please. :(

    Another question.
    Somewhere I read ;
    by using "ADC_Read" , The ADC is first initialized and then Pin is Read.
    by using "ADC_Get_Sample" , The ADC is not initialized but just Read.So here I must use "ADC_Init(); " in main just like" LCD_Init();"
    Is my understanding correct ?
    And the time taken is smaller in "ADC_Get_Sample" than "ADC_Read" as the ADC is initialized before hand in "Main".
     
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,691
    2,756
    When you write C, unless you write your own hardware drivers, you are at the mercy of the library developers.
     
    OBW0549 likes this.
  3. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    Beginners are at the mercy of the library developers :(
    And I am not that good to write my own hardware drivers.
     
    OBW0549 likes this.
  4. OBW0549

    Well-Known Member

    Mar 2, 2015
    1,327
    890
    And library developers sometimes (often?) don't put much thought into the variety of different ways people might want-- or might need-- to operate the hardware. So you end up getting stuck with a very inflexible "virtual interface" to the hardware which, although it might make it easier to operate the hardware in some logically straightforward, easy-to-understand fashion, nevertheless severely limits your ability to do anything creative. Like here.

    Situations like this are the reason I much prefer working in assembly language. JMHO...
     
    joeyd999 likes this.
  5. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    So any thoughts on my problem guys ?
     
  6. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,999
    745
    Usually in pics, you set the 'ADCON_GO' bit, this starts the A/D, and then loop a bit test on it to see when it clears, the value will be put into the respective A/D registers..
     
  7. Picbuster

    Member

    Dec 2, 2013
    376
    50
    Here is a simple working example but you have to adjust it for your pic. (like pir1bits.adif in some pic adif is enough)
    Picbuster

    //----------------------- in interrupt routine --------------------------------

    if (PIR1bits.ADIF)
    {
    PIR1bits.ADIF = 0; // Clear A/D conversion complete interrupt flag
    Delay1KTCYx(5); // Delay for 5000 instruction cycles
    Vbatt=(float)(ADRESH*256 + ADRESL);
    }
    //---------------------------------------------------------
    //------------------- setup ------------------------------

    ADCON0bits.ADON = On; // 1 Turn A/D on
    ADCON1bits.VCFG1 = 0; // VREF- = AVSS
    ADCON1bits.VCFG0 = 0; // VREG+ = AVDD
    ADCON1bits.PCFG3 = 1; // 1Select 1 Analog channel, AN0
    ADIE=1; // set int
    GIE=1; // set all ints


    // ==== main loop ============

    ADCON0bits.GO = 1; // get value start ad

    if (Vbatt<10)
    {
    Vbatt=Vbatt*0.0357; // real value compensated with factor to comply with input voltage
    Do_Job();
    }
     
  8. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,691
    2,756
    Oh, the humanity!
     
  9. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,754
    760
    I know.
    By the way, my problem is solved. I just SET the ADCON1 vref bits after ADC Init; and it works.
     
Loading...