Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic18f4520 a/d

Status
Not open for further replies.

yohanevindra

New Member
In setting the A/D converter, its required to set ADCON2. In that register there are two sets of bits, namely the A/D acquisition time select bits and the A/D conversion clock select bits.

At the moment I'm using only one analog input, but later on my plan is to have about 3 analog inputs which would need to communicated with the computer. what are the ideal settings?

Also, what does "20Tad" mean in terms of acquisition time?
 
20Tad = 20 instruction cycles to acquisition

Here some sample In C for 18fX chips
Code:
unsigned int ADCResult=0;
float voltage=0;

void main(void)
{        

        unsigned char channel=0x00,config1=0x00,config2=0x00,config3=0x00,portconfig=0x00,i=0;

//----- clear adc interrupt and turn off adc if in case was on prerviously---------------                
        CloseADC();        

//-------------------------initialize adc---------------------------------------------------------------
/**** ADC configured for:
        * FOSC/2 as conversion clock
        * Result is right justified
        * Aquisition time of 2 AD
        * Channel 1 for sampling
        * ADC interrupt on
        * ADC reference voltage from VDD & VSS
*/
        config1 = ADC_FOSC_2 | ADC_RIGHT_JUST | ADC_2_TAD ;
        config2 = ADC_CH0 | ADC_INT_ON | ADC_REF_VDD_VSS ;
        portconfig = ADC_15ANA ;
        OpenADC(config1,config2,portconfig);


//------------------------------initialize the adc interrupt and enable them-------------------------------------        
        ADC_INT_ENABLE(); 

//-----------------------sample and convert-------------------------------- -------------------------
        for(i=0;i<16;i++)
        {
        ConvertADC();
    while(BusyADC());        
        ADCResult += (unsigned int) ReadADC();
        }
        ADCResult /= 16;
        voltage = (ADCResult*5.0)/1024; // convert ADC count into voltage
        
//----------------------------------------------------------------------------------------------------------- 
    CloseADC();                 //turn off ADC
        
        while(1);                                                //End of program 
}
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top