Hot!ACQT and ADCS values

Author
naeem1234
Super Member
  • Total Posts : 354
  • Reward points : 0
  • Joined: 2015/02/19 06:39:28
  • Location: 0
  • Status: offline
2017/11/13 13:03:44 (permalink)
0

ACQT and ADCS values

Hello everyone
My chip is PIC18F46K22, 40 pins DIP package, and i am using external XTAL Osc. of 20MHz.
 
I am using its ADC to read an analog signal that is connected on its channel 0.
I have read the datasheet and there are 2 parameters in ADC that i could not make sense of although my ADC code is working as expected but still i am concerned about the following 2 parameters.
1) ACQT - A/D Acquisition time select bits
2) ADCS - A/D Conversion Clock Select bits
 
How should i select the values of above parameters if my system clock is 20 MHz external crystal?
Should i select a high value or a low value for the above parameters?
 
My code is appended below for suggestions.
 
Any help is highly appreciated.
 
void setup_routine()
{
...
TRISAbits.TRISA0=1; //AN0
/*Vref+ = VDD, Vref- = VSS.*/
ADCON1bits.NVCFG0 = 0;
ADCON1bits.NVCFG1 = 0;
ADCON1bits.PVCFG0 = 0;
ADCON1bits.PVCFG1 = 0;
ADCS0 = 0;
ADCS1 = 1;
ADCS2 = 1;
ADON = 1; // ADC in turned ON
...
}


void ADC_data()
{
unsigned char acquisitiontime=100;
...
ADCChannel_0();
ADFM = 0; // left justified

while(acquisitiontime--);

GODONE=1;
while(GODONE==1);
adcvalue = ADRESH;
// only required to read high byte and ignore low byte.
...
}

 
#1

17 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 6262
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: ACQT and ADCS values 2017/11/13 15:43:54 (permalink)
    +1 (1)
    This is documented in your datasheet under "17.1.4 SELECTING AND CONFIGURING ACQUISITION TIME"
    Use the peripheral features to do it, your delay loop will be ineffective after the compiler optimiser gets to it.
     
    For ADCS, look at "TABLE 17-1: ADC CLOCK PERIOD (TAD) VS. DEVICE OPERATING FREQUENCIES" to see what value you need.
    It only shows 1/4/16/64 MHz, but 20 is only a bit faster than 16, so I'd go for "Fosc/32", which actually gives a Tad of 32/20MHz = 1.6us
     
    #2
    naeem1234
    Super Member
    • Total Posts : 354
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: ACQT and ADCS values 2017/11/13 21:11:43 (permalink)
    0
    Thanks for your reply.
     
    I tried to understand the mathematics of calculating the Acquisition time which is given under "17.4" but failed.
    Would you be kind enough to tell me what is the safe value for this parameter if we don't calculate it empirically.. should we select a higher value, 20 TAD or a smaller value, 2 TAD or is there any simple approximation for its calculation?
    #3
    qhb
    Superb Member
    • Total Posts : 6262
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: ACQT and ADCS values 2017/11/13 21:24:21 (permalink)
    +1 (1)
    The safest delay is the longest one. i.e. 20Tad.
     
    Assuming your source impedance is below 10k, you could use their calculations, which gives 7.45us.
    If your Tad is 1.6us (see above), then that equates to 7.45 / 1.6 = 4.6 Tad cycles, so a delay of 6 Tad should work fine.
    #4
    qhb
    Superb Member
    • Total Posts : 6262
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: ACQT and ADCS values 2017/11/13 21:24:22 (permalink)
    0
    -- removed double post.
    #5
    naeem1234
    Super Member
    • Total Posts : 354
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: ACQT and ADCS values 2017/11/14 21:36:05 (permalink)
    0
    Thanks for the details.
    I have changed my code and removed the software delay routine for acquisition time as you suggested. Now its as following.
    But its not giving correct value.
     
    Can you see what other issues are now in the code?
    Your help is highly appreciated.
     

    void setup_routine
    {
    ...
    PMD2bits.ADCMD = 0;
    TRISAbits.TRISA0=1; //AN0
    ANSELAbits.ANSA0=1;
     
    ADCS0 = 0; // @20MHz TAD=1.6us
    ADCS1 = 1; // freq=Fosc/32
    ADCS2 = 0; 
     
    /*Vref+ = VDD, Vref- = VSS.*/
    ADCON1bits.NVCFG0 = 0;
    ADCON1bits.NVCFG1 = 0;
    ADCON1bits.PVCFG0 = 0;
    ADCON1bits.PVCFG1 = 0;
     
    ACQT0 = 0; // Acquisition time 16 TAD = 26us
    ACQT1 = 1;
    ACQT2 = 1;
    ...
    }
     
    void ADC_data()
    {
    unsigned char adcvalue;
    ...
    ADCChannel_0();
    ADFM = 0; // left justified
    ADON = 1; // ADC turn ON

    GODONE=1;
    while(GODONE==1);
    adcvalue = ADRESH; // only high byte needed.
    ADON = 0;
     ...
    }

    #6
    qhb
    Superb Member
    • Total Posts : 6262
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: ACQT and ADCS values 2017/11/14 22:38:02 (permalink)
    +1 (1)
    Try not turning the ADC off after every reading.
    Turn it on at the start, and leave it on.
     
    #7
    naeem1234
    Super Member
    • Total Posts : 354
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 00:25:10 (permalink)
    0
    qhb
    Try not turning the ADC off after every reading.
    Turn it on at the start, and leave it on.

     
    I tried this also, still its reading zero in 'adcvalue' while there is approx. 2v at the ADC input.
    Any idea what else could be causing the problem?
    Or should i go back to using software delay for acquisition time?
    #8
    naeem1234
    Super Member
    • Total Posts : 354
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 04:38:28 (permalink)
    0
    can someone give a sample code for polling based ADC read using automatic acquisition time?
    #9
    DarioG
    humans, die, please
    • Total Posts : 53216
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 04:49:22 (permalink)
    0
    A delay wont' hurt anyway, you can remove it later.
     
    Apart from the turning-on and off that Qhb pointed oout, your code should work. Try posting your new code...

    if only every single human would die...

    italy collapses!!! :D

    #10
    naeem1234
    Super Member
    • Total Posts : 354
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 05:26:03 (permalink)
    +1 (1)
    DarioG
    A delay wont' hurt anyway, you can remove it later.
     
    Apart from the turning-on and off that Qhb pointed oout, your code should work. Try posting your new code...


    Thanks for your reply. Here is the code of ADC portion.
    Waiting for your suggestions.
     

    void ADCChannel_0() { // Fuel Input, PIN 2, RA0
    CHS0=0;
    CHS1=0;
    CHS2=0;
    CHS3=0;
    CHS4=0;
    }
     
    void ADC_init()
    {
    PMD2bits.ADCMD = 0;
    TRISAbits.TRISA0=1; //AN0
    ANSELAbits.ANSA0=1;
     
    ADCS0 = 0; // @20MHz TAD=1.6us
    ADCS1 = 1; // freq=Fosc/32
    ADCS2 = 0; //
    /*Vref+ = VDD, Vref- = VSS.*/
    ADCON1bits.NVCFG0 = 0;
    ADCON1bits.NVCFG1 = 0;
    ADCON1bits.PVCFG0 = 0;
    ADCON1bits.PVCFG1 = 0;
    ADCChannel_0();
    ADFM = 0; // left justified

    ACQT0 = 0; // Acquisition time 16 TAD
    ACQT1 = 1;
    ACQT2 = 1;
    ADON = 1; // ADC turn ON
    }
     
    void ADC_data()
    {
    unsigned char adcvalue;
     
    GODONE=1;
    while(GODONE==1){}; // it takes 1.6usec for the conversion to complete
    adcvalue = ADRESH;
    ...
    }

    #11
    DarioG
    humans, die, please
    • Total Posts : 53216
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 05:29:19 (permalink)
    0
    Sounds good enough: how are you testing the resulting value?

    if only every single human would die...

    italy collapses!!! :D

    #12
    naeem1234
    Super Member
    • Total Posts : 354
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 05:32:08 (permalink)
    +1 (1)
    DarioG
    Sounds good enough: how are you testing the resulting value?




    I am writing the 'adcvalue' in a buffer and then requesting the buffer value on my PC via serial port.
    #13
    DarioG
    humans, die, please
    • Total Posts : 53216
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 05:36:29 (permalink)
    0
    Could it be a problem with this conversion? can you post this part of code?

    if only every single human would die...

    italy collapses!!! :D

    #14
    naeem1234
    Super Member
    • Total Posts : 354
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 06:48:27 (permalink)
    0
    DarioG
    Could it be a problem with this conversion? can you post this part of code?




    I have replaced all the code with the following and connected a variable resistor on the ADC pin, but the LED on RC1 is OFF.
    Pls. suggest how can i debug this situation?

    if (adcvalue<50) PORTCbits.RC1=0;
    else if(adcvalue>=50 && adcvalue<100) PORTCbits.RC1=1;
    else if(adcvalue>=100 && adcvalue<150) PORTCbits.RC1=0;
    else if(adcvalue>=150 && adcvalue<200) PORTCbits.RC1=1;
    else if(adcvalue>=200) PORTCbits.RC1=0;
     

    #15
    DarioG
    humans, die, please
    • Total Posts : 53216
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re: ACQT and ADCS values 2017/11/15 06:50:38 (permalink)
    0
    Strange...

    if only every single human would die...

    italy collapses!!! :D

    #16
    qhb
    Superb Member
    • Total Posts : 6262
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: ACQT and ADCS values 2017/11/15 07:00:03 (permalink)
    +1 (1)
    Have you cleared TRISC1 to make that pin an output?
    We have to keep asking these questions when you don't post a complete program.
    Note, you should be writing to LATC. not PORTC to switch individual pins.
     
    #17
    naeem1234
    Super Member
    • Total Posts : 354
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: ACQT and ADCS values 2017/11/20 04:52:26 (permalink)
    +1 (1)
    sorry for the late update, the problem was not in the code but the PIC chip was faulty.
    #18
    Jump to:
    © 2018 APG vNext Trial Version 4.5