#### R!f@@

Joined Apr 2, 2009
9,797
I looked at the mikroC help and it shows this.

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.

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

Another question.
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 ?

#### joeyd999

Joined Jun 6, 2011
4,473
When you write C, unless you write your own hardware drivers, you are at the mercy of the library developers.

#### R!f@@

Joined Apr 2, 2009
9,797
Beginners are at the mercy of the library developers
And I am not that good to write my own hardware drivers.

#### OBW0549

Joined Mar 2, 2015
3,566
When you write C, unless you write your own hardware drivers, you are at the mercy of the library developers.
Beginners are at the mercy of the library developers
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...

#### R!f@@

Joined Apr 2, 2009
9,797
So any thoughts on my problem guys ?

#### Dodgydave

Joined Jun 22, 2012
10,359
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..

#### Picbuster

Joined Dec 2, 2013
1,038
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 --------------------------------

{
PIR1bits.ADIF = 0; // Clear A/D conversion complete interrupt flag
Delay1KTCYx(5); // Delay for 5000 instruction cycles
}
//---------------------------------------------------------
//------------------- setup ------------------------------

ADCON1bits.VCFG1 = 0; // VREF- = AVSS
ADCON1bits.VCFG0 = 0; // VREG+ = AVDD
ADCON1bits.PCFG3 = 1; // 1Select 1 Analog channel, AN0
GIE=1; // set all ints

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

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

#### joeyd999

Joined Jun 6, 2011
4,473
Delay1KTCYx(5); // Delay for 5000 instruction cycles
Oh, the humanity!