• AVR Freaks

Hot!PIC24FV16KM204 ADC is not working

Author
PICStarter
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2020/01/17 14:16:27
  • Location: 0
  • Status: offline
2020/01/21 13:38:46 (permalink)
0

PIC24FV16KM204 ADC is not working

Hello community,

I am planning some projects at home. After some research I have chosen the PIC family micros for my projects.

In my first project I want to read 8 AD signals. The signals are connected on AN0, AN1, AN4, AN5, AN6, AN7, AN11 and AN18. I have chosen the PIC24FV16KM204 for this project, because I need many PWM channels. The controller runs with the internal RC oscillator at 32MHz. The clock runs fine, because timer1 ISR is configured to generate a 1ms tick, which drives a little time slice system. My idea is to initialize the ADC as free running ADC with auto scan mode, so that the 8 AD channels are scanned consecutively. When I need an AD value I want to read the last converted value in the ADC1BUFx register. In the cyclic function I read the ADC1BUF0 register and activate a LED to check if the ADC is working. But for some reason, the LED does not change its state.

I checked the datasheet and the register initialization (which I think fits my requirements) is shown below. Does anyone have a hint, why the ADC is not working here?

void ADC_Init(void)
{
    ANSA    = PORT_ANSA_INIT;    /* #define PORT_ANSA_INIT 0x0003 */
    ANSB    = PORT_ANSB_INIT;    /* #define PORT_ANSB_INIT 0x204C */
    ANSC    = PORT_ANSC_INIT;    /* #define PORT_ANSC_INIT 0x0003 */
    
    AD1CON1 = ADC_AD1CON1_INIT;  /* #define ADC_AD1CON1_INIT 0x2474 */
    AD1CON2 = ADC_AD1CON2_INIT;  /* #define ADC_AD1CON2_INIT 0x0C00 */
    AD1CON3 = ADC_AD1CON3_INIT;  /* #define ADC_AD1CON3_INIT 0x1602 */
    AD1CON5 = ADC_AD1CON5_INIT;  /* #define ADC_AD1CON5_INIT 0x9000 */
    AD1CSSH = ADC_AD1CSSH_INIT;  /* #define ADC_AD1CSSH_INIT 0x0004 */
    AD1CSSL = ADC_AD1CSSL_INIT;  /* #define ADC_AD1CSSL_INIT 0x08F3 */
    AD1CON1bits.ADON = 1;        /* turn ADC ON */
}

void ADC_Cyclic(void)
{
    TRISCbits.TRISC5 = 0;        /* Set debug port as output */
    if (ADC1BUF0 < 1000)         /* If AD value is < 1000 */
        LATCbits.LATC5 = RESET;  /* Deactivate the LED */
    else
        LATCbits.LATC5 = SET;    /* else activate the LED */
}
 

post edited by PICStarter - 2020/01/22 12:07:39
#1

5 Replies Related Threads

    Bob White
    Super Member
    • Total Posts : 336
    • Reward points : 0
    • Joined: 2010/11/06 19:52:38
    • Location: Denver, Colorado
    • Status: offline
    Re: PIC24FV16KM204 ADC is not working 2020/01/21 16:05:03 (permalink)
    0
    As many have pointed out before, the problem is probably in line 47 of your code.

    Post your entire code (using code tags!) so we have some clue as to how to help you.
    To tag your code start with [ code] (without the leading space) and end with [/code].
     
    Then your code shows up properly formatted and easy to read as shown:
     

    void main()

    #2
    PICStarter
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2020/01/17 14:16:27
    • Location: 0
    • Status: offline
    Re: PIC24FV16KM204 ADC is not working 2020/01/22 12:08:50 (permalink)
    0
    Hi Bob,
     
    thanks for the hint with the code tags. I have updated my above post and added a .zip file with the complete source code.
    #3
    Howard Long
    Super Member
    • Total Posts : 811
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC24FV16KM204 ADC is not working 2020/01/31 02:05:34 (permalink)
    0
    Here's an example for the 28 pin version of your chip.
     


    // PIC24FV16KM202 Configuration Bit Settings
    // 'C' source line config statements
    // FBS
    #pragma config BWRP = OFF // Boot Segment Write Protect (Disabled)
    #pragma config BSS = OFF // Boot segment Protect (No boot program flash segment)
    // FGS
    #pragma config GWRP = OFF // General Segment Write Protect (General segment may be written)
    #pragma config GCP = OFF // General Segment Code Protect (No Protection)
    // FOSCSEL
    #pragma config FNOSC = FRCPLL // Oscillator Select (Fast RC Oscillator with Postscaler and PLL Module (FRCDIV+PLL))
    #pragma config SOSCSRC = DIG // SOSC Source Type (Digital Mode for use with external source)
    #pragma config LPRCSEL = HP // LPRC Oscillator Power and Accuracy (High Power, High Accuracy Mode)
    #pragma config IESO = ON // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
    // FOSC
    #pragma config POSCMOD = NONE // Primary Oscillator Configuration bits (Primary oscillator disabled)
    #pragma config OSCIOFNC = CLKO // CLKO Enable Configuration bit (CLKO output signal enabled)
    #pragma config POSCFREQ = HS // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock input frequency greater than 8MHz)
    #pragma config SOSCSEL = SOSCHP // SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
    #pragma config FCKSM = CSECMD // Clock Switching and Monitor Selection (Clock Switching is enabled, Fail-safe Clock Monitor is disabled)
    // FWDT
    #pragma config WDTPS = PS32768 // Watchdog Timer Postscale Select bits (1:32768)
    #pragma config FWPSA = PR128 // WDT Prescaler bit (WDT prescaler ratio of 1:128)
    #pragma config FWDTEN = OFF // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled)
    #pragma config WINDIS = OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected(windowed WDT disabled))
    // FPOR
    #pragma config BOREN = BOR3 // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware, SBOREN bit disabled)
    #pragma config RETCFG = OFF // (Retention regulator is not available)
    #pragma config PWRTEN = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config I2C1SEL = PRI // Alternate I2C1 Pin Mapping bit (Use Default SCL1/SDA1 Pins For I2C1)
    #pragma config BORV = V18 // Brown-out Reset Voltage bits (Brown-out Reset set to lowest voltage (1.8V))
    #pragma config MCLRE = ON // MCLR Pin Enable bit (RA5 input pin disabled, MCLR pin enabled)
    // FICD
    #pragma config ICS = PGx1 // ICD Pin Placement Select bits (EMUC/EMUD share PGC1/PGD1)
    //#pragma config ICS = PGx3 // ICD Pin Placement Select bits (EMUC/EMUD share PGC3/PGD3)
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
    #include <xc.h>
    #define FCY 16000000
    #include <libpic30.h>
    int main(void)
    {
    CLKDIVbits.RCDIV=0b000; // 0b000 => div-by-1

    TRISA=0;
    TRISB=0;
    LATA=0x5555;
    LATB=0x5555;

    AD1CON1bits.FORM=0b00; // 0b00 => Absolute decimal result, unsigned, right-justified
    AD1CON1bits.MODE12=0; // 0 => 10-bit A/D operation
    AD1CON1bits.SSRC=0b0111; // 0b0111 => Internal counter ends sampling and starts conversion (auto-convert)
    AD1CON2bits.PVCFG=0b00; // 0b00 => Vref+ = AVdd
    AD1CON2bits.NVCFG0=0; // 0 => Vref- = Vss
    AD1CON2bits.BUFREGEN=1; // 1 => Conversion result is loaded into a buffer location determined by the converted channel
    AD1CON2bits.CSCNA=1; // 1 => Scans inputs (Legacy channel scan)
    AD1CON2bits.SMPI=13+6-1; // Interrupts at the completion of N-1 samples
    AD1CON2bits.ALTS=0; // 0 => Always uses channel input selects for Sample A
    AD1CON3bits.ADRC=1; // 1 => RC clock
    // AD1CON3bits.ADRC=0; // 0 => Clock is derived from the system clock
    AD1CON3bits.SAMC=31; // 31 => 31 * Tad
    // AD1CON3bits.ADCS=0b00111111; // 0b00111111 => Tad = 64 * Tcy
    AD1CON5bits.ASEN=0; // 0 => Auto-scan is disabled (Threshold scan, NOT channel scan)
    AD1CHSbits.CH0NA=0b000; // 0b000 => AVss
    AD1CSSH=0x003F;
    AD1CSSL=0xFE3F;
    AD1CON1bits.ADON=1;
    __delay_ms(10);
    IFS0bits.AD1IF=0;
    AD1CON1bits.ASAM=1; // 1 => Sampling begins immediately after the last conversion; SAMP bit is auto-set

    while (1)
    {
    while (!IFS0bits.AD1IF)
    {
    Nop();
    Nop();
    Nop();
    }
    LATA=~LATA;
    LATB=~LATB;
    IFS0bits.AD1IF=0;
    Nop();
    Nop();
    Nop();
    }

    return 0;
    }

    #4
    PICStarter
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2020/01/17 14:16:27
    • Location: 0
    • Status: offline
    Re: PIC24FV16KM204 ADC is not working 2020/02/01 12:52:17 (permalink)
    0
    Hi Howard,
     
    thanks for your sample code. It helped me a lot to find the root cause for my issue.
     
    For anybody else, having the same issue:
    In my code the ASEN bit in register AD1CON5 was set. Also the SSRC bit in the AD1CON1 register was set to 0111b.
    There is a note (note1 in AD1CON5 register description) in the datasheet that mentions, that this setting is not allowed. Setting the ASEN bit to 0 makes the ADC working as expected.
     
    Thanks a lot for your help!
    #5
    Howard Long
    Super Member
    • Total Posts : 811
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC24FV16KM204 ADC is not working 2020/02/03 05:27:12 (permalink)
    0
    The datasheet(s) aren't particularly clear. As you can probably guess by the comment in my code, I also hit the ASEN problem.
     
    From the PIC24F FRM section 51:
    Note: Legacy Auto-Scan (i.e., sequential scanning of analog channels on MUX A, without
    any comparison) is controlled by the CSCNA bit (AD1CON2<10>) and does not
    depend on the ASEN bit to function
     
    It's not immediately clear to me why, technically speaking, auto-convert wouldn't work with threshold detect, but threshold detect's not a feature I've used.
     
    I've been using these chips for some years, they're quite feature packed for a 16 bit MCU. The only thing missing I'd have liked to have seen is DMA and a faster ADC.
     
    Be aware of a number of ambiguities and contradictions in this chip's CCP modules, around the triggers, syncs, interrupts and auxiliary outputs. For example, from memory, certainly in one data sheet version they had the CCTxIF and CCPxIF swapped around in one of the modes. This seems to be resolved in the latest data sheet & FRM.
    post edited by Howard Long - 2020/02/03 05:58:26
    #6
    Jump to:
    © 2020 APG vNext Commercial Version 4.5