• AVR Freaks

Hot!PIC24FJ64GB106 ADC

Author
JacobTowsley
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2011/04/22 20:38:19
  • Location: 0
  • Status: offline
2020/05/13 18:39:44 (permalink)
0

PIC24FJ64GB106 ADC

I am trying to set up a simple ADC but it is multiple inputs. I have 10 Analog inputs, using TMR 3 as the timing i want to create those 10 inputs to fill buffer BUF0  to BUF 9. 
void ADC1_Init(void)
{
AD1PCFGH = 0x0000;
AD1PCFGL = 0x0003;
/*ADCON1*/
AD1CON1 = 0x004C;
/*ADCON2*/
AD1CON2 = 0x0300;
/*ADCON3*/
AD1CON3 = 0x0001;
// Enabling ADC1 interrupt.
IFS0bits.AD1IF = 0;
IEC0bits.AD1IE = 1;
}
 
void __attribute__ ( ( __interrupt__ , auto_psv, weak ) ) _ADC1Interrupt ( void )
{
if(IFS0bits.AD1IF)
{
// clear the ADC interrupt flag
IFS0bits.AD1IF = 0;
}
}
 
void TMR3_Initialize (void)
{
//TMR3 0;
TMR3 = 0x00;
//Period = 0.004096 s; Frequency = 16000000 Hz; PR3 65535;
PR3 = 0xFFFF;
//TCKPS 1:1; TON enabled; TSIDL disabled; TCS FOSC/2; TGATE disabled;
T3CON = 0x8000;
 
IFS0bits.T3IF = false;
IEC0bits.T3IE = true;


}
 
#1

9 Replies Related Threads

    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/05/13 21:45:12 (permalink)
    0
    Why not just autoscan it something like this should do the trick
     
    #include <stdbool.h>
    #include <xc.h>

    /*-[ ADC1_Init ]------------------------------------------------------------}
    . Setup the ADC, Ports and Interrupts as per this project.
    .--------------------------------------------------------------------------*/
    void ADC1_Init (bool enableIrq)
    {   
        /* SETUP PINS AS ANALOGUE IN */
        TRISB = 0b0000001111111111;     //  AN0-AN9 selected as input
        AD1PCFGL = 0b1111110000000000;  // AN0-AN9 selected as ADC
        AD1PCFGH = 0x03;
       
        AD1CON1bits.ADON = 0;           // Turn off ADC while we do this
       
        AD1CON1bits.ADSIDL = 0;         // Stops ADC module operation in Idle mode
        AD1CON1bits.FORM = 0;           // Data Output Format: un-Signed Integer
        AD1CON1bits.SSRC = 0b111;       // Sample Clock Source: Internal counter ends sampling and starts conversion (auto-convert)
        AD1CON1bits.ASAM = 1;           // ADC Sample Control: Sampling begins immediately after conversion

        AD1CON2bits.VCFG = 0b000;       // references AVDD AVSS    
        AD1CON2bits.BUFM = 0;           //  Configured as 16bit buffer
        AD1CON2bits.CSCNA = 1;          // Scan channels
     
        // number of samples taken per interrupt (+1)
        AD1CON2bits.SMPI = 1;
              
        AD1CON3bits.ADRC = 1;           // use 4Mhz ADC clock )
        //AD1CON3bits.ADRC = 0;         // use system clock (FCY = 16MHz)
        AD1CON3bits.SAMC = 31;          // Sample Time  = SAMC[4:0] * TAD
        AD1CON3bits.ADCS = 255;         // TAD = FCY / (ADCS[7:0] + 1)
                                

        //AD1CSSH/AD1CSSL: Analog-to-Digital Input Scan Selection Register
        AD1CSSH = 0x0000;
        AD1CSSL = 0b0000001111111111;  // Scan AN0-AN9
       
       if (enableIrq)
       {
            IFS0bits.AD1IF = 0;        // Clear the A/D interrupt flag bit
            IEC0bits.AD1IE = 1;        // Enable A/D interrupt
        }
        AD1CON1bits.ADON = 1;          // Turn on the A/D converter
    }

    #2
    JacobTowsley
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/04/22 20:38:19
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/05/14 17:46:58 (permalink)
    0
    It gets to the inteerupt but does not seem to fill in the buffers.
    AN0 and AN1 are connected to the PGED/PGEC and are not used for Analog conversion in the project.
     
    void ADC1_Init(void)
    {
    TRISB = 0x4FFC;
    AD1PCFGH = 0x0003;
    AD1PCFGL = 0xC003;
    /*ADCON1*/
    AD1CON1bits.ADON = 0;
    AD1CON1bits.ADSIDL= 0;
    AD1CON1bits.FORM = 0;
    AD1CON1bits.SSRC = 0b111;
    AD1CON1bits.ASAM = 1;

    /*ADCON2*/
    AD1CON2bits.VCFG = 0b000;
    AD1CON2bits.BUFM = 0;
    AD1CON2bits.CSCNA =1;
    AD1CON2bits.SMPI = 1;
    /*ADCON3*/
    AD1CON3bits.ADRC =1;
    AD1CON3bits.SAMC =31;
    AD1CON3bits.ADCS = 225;

    AD1CSSH = 0x0000;
    AD1CSSL = 0xC003;

    // Enabling ADC1 interrupt.
    IFS0bits.AD1IF = 0;
    IEC0bits.AD1IE = 1;
    AD1CON1bits.ADON = 1;
    }
     
    int main(void)
    {
    // initialize the device
    SYSTEM_Initialize();
    ADC1_Init();
    while (1)
    {
    RED_LED = 1;
    }
    }
     
    void __attribute__ ( ( __interrupt__ , auto_psv, weak ) ) _ADC1Interrupt ( void )
    {
    IFS0bits.AD1IF = 0;
    }
    #3
    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/05/14 21:16:12 (permalink)
    0
    Just FYI doesn't seem to be an issue but the ADC is supposed to go before setting the interrupts section 22.0 ... see point 1g then 2a and 2b
     
    I haven't used this particular PIC24F with it's multiplexer but the scan is running so it must be the analog selection to the pins isn't right ... one sec let me look.
     
    Found it you omitted all the channels from the actual scan ... 1 = scan channel, 0 = no scan
    Try
    AD1CSSL = 0x3FFC;

    Also remember only the channels mark to scan appear in the buffer so BUF0 will be AN2, BUF1 will AN3 etc in your case
    That includes if you put gaps in the scan they don't get allocated a buffer spot.
    post edited by LdB_ECM - 2020/05/14 22:33:26
    #4
    JacobTowsley
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/04/22 20:38:19
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/05/16 15:19:52 (permalink)
    0
    Thank you This partly solved the issue, I now have ADCBUF0 and BUF1 filling but Buf3-A do not they have Garbage data in them.
    void ADC1_Initialize (void)
    {
    TRISB = 0b0011110011111100; // AN0-AN9 selected as input
    AD1PCFGL = 0b1100001100000011; // AN0-AN9 selected as ADC
    AD1PCFGH = 0x03;

    AD1CON1bits.ADON = 0; // Turn off ADC while we do this

    AD1CON1bits.ADSIDL = 0; // Stops ADC module operation in Idle mode
    AD1CON1bits.FORM = 0; // Data Output Format: un-Signed Integer
    AD1CON1bits.SSRC = 0b111; // Sample Clock Source: Internal counter ends sampling and starts conversion (auto-convert)
    AD1CON1bits.ASAM = 1; // ADC Sample Control: Sampling begins immediately after conversion
    AD1CON2bits.VCFG = 0b000; // references AVDD AVSS
    AD1CON2bits.BUFM = 0; // Configured as 16bit buffer
    AD1CON2bits.CSCNA = 1; // Scan channels

    // number of samples taken per interrupt (+1)
    AD1CON2bits.SMPI = 1;

    AD1CON3bits.ADRC = 1; // use 4Mhz ADC clock )
    //AD1CON3bits.ADRC = 0; // use system clock (FCY = 16MHz)
    AD1CON3bits.SAMC = 31; // Sample Time = SAMC[4:0] * TAD
    AD1CON3bits.ADCS = 255; // TAD = FCY / (ADCS[7:0] + 1)
    //AD1CSSH/AD1CSSL: Analog-to-Digital Input Scan Selection Register
    AD1CSSH = 0x0000;
    AD1CSSL = 0x3FFC; // Scan AN0-AN9


    IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit
    IEC0bits.AD1IE = 1; // Enable A/D interrupt

    AD1CON1bits.ADON = 1; // Turn on the A/D converter
    }

    void __attribute__ ( ( __interrupt__ , auto_psv ) ) _ADC1Interrupt ( void )
    {

    IFS0bits.AD1IF = false;
    }
     
    #5
    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/05/16 22:53:10 (permalink)
    0
    Buffer3 would be AN4 which is also PGED3 which has priority to steal the pin
     
    Check the values of CS1:ICS0 you have set on the config
    bit 9-8ICS1:ICS0: Emulator Pin Placement Select
         11 = Emulator functions are shared with PGEC1/PGED1
         10 = Emulator functions are shared with PGEC2/PGED2
         01 = Emulator functions are shared with PGEC3/PGED3
     
    That pin and AN4 also have USB lines check what that means .. you have given no indication of what is on ports so can't help beyond that.
    post edited by LdB_ECM - 2020/05/16 22:56:43
    #6
    JacobTowsley
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/04/22 20:38:19
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/06/05 18:20:08 (permalink)
    0
    The AN0 and AN1 is working but nothing fast that. 
    The setup of PORTB is the following
     
    B0 - PGED
    B1 - PGEC
    B2/AN2- ADCBUF 0
    B3/AN3 - ADCBUF 1
    B4/AN4 - ADCBUF 2
    B5/AN5 - ADCBUF 3
    B6/AN6 - ADCBUF 4
    B7/AN7 - ADCBUF 5
    B8 - DIGI OUT
    B9 - DIGI OUT
    B10/AN10 - ADCBUF 6
    B11/AN11 - ADCBUF 7
    B12/AN12 - ADCBUF 8
    B13/AN13 - ADCBUF 9
    B14 - DIGI OUT
    B15 - DIGI OUT
     
    ADCBUF 0 = Working
    ADCBUF 1 = Working
    ADCBUF2-9 = NOT WORKING have Values like 0X3FFB, 0XF3BF 
     
     
    Here is the code 
    void ADC1_Initialize(void)
    {
    TRISB = 0x4FFC;
    AD1PCFGH = 0x0003;
    AD1PCFGL = 0xC003;
    /*ADCON1*/
    AD1CON1bits.ADON = 0;
    AD1CON1bits.ADSIDL= 0;
    AD1CON1bits.FORM = 0;
    AD1CON1bits.SSRC = 0b111;
    AD1CON1bits.ASAM = 1;
    /*ADCON2*/
    AD1CON2bits.VCFG = 0b000;
    AD1CON2bits.BUFM = 0;
    AD1CON2bits.CSCNA =1;
    AD1CON2bits.SMPI = 1;
    /*ADCON3*/
    AD1CON3bits.ADRC =1;
    AD1CON3bits.SAMC =31;
    AD1CON3bits.ADCS = 225;
    AD1CSSH = 0x0000;
    AD1CSSL = 0x3FFC;
    // Enabling ADC1 interrupt.
    IFS0bits.AD1IF = 0;
    IEC0bits.AD1IE = 1;
    AD1CON1bits.ADON = 1;
    }
    void __attribute__ ( ( __interrupt__ , auto_psv, weak ) ) _ADC1Interrupt ( void )
    {
    IFS0bits.AD1IF = 0;
    }
     
     
    #7
    JacobTowsley
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2011/04/22 20:38:19
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/06/16 18:01:58 (permalink)
    4 (1)
    So I cannot get it to fill ADCBUF2 or greater they all have a random hex number in the buffer 0X98A3 etc. 
    #8
    BroadwellConsultingInc
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2020/06/09 06:07:55
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/06/16 18:53:01 (permalink)
    0
    Are all of your inputs low impedance inputs?  You're not trying to measure, say a voltage divider with a couple of 500k's are you?
    #9
    LdB_ECM
    Super Member
    • Total Posts : 401
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: PIC24FJ64GB106 ADC 2020/07/22 18:39:46 (permalink)
    0
    Edit: I should be nice here is the code above still what you are using?
    What is the ANS value you have set? ... or perhaps just jam ADC4 so we know its set.
     _ANSA4 = 1;    // Assign RA4 as analog in

    post edited by LdB_ECM - 2020/07/22 18:57:11
    #10
    Jump to:
    © 2020 APG vNext Commercial Version 4.5